2016-08-19 105 views
1

我想使用XMl Spring配置来配置下面示例中显示的条件bean,而不是通过注释。是否可以通过XML配置来实现条件bean?如何使用XML配置文件配置Spring 4中提供的条件bean

public class TestCondition1 implements Condition 
{ 
    @Override 
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) 
    { 
    return context.getEnvironment().getProperty("os.name").contains("Windows"); 
    } 
} 

@Configuration 
public class BeanTestConfiguration 
{ 
    @Bean(name="TesService") 
    @Conditional(TestCondition1.class) 
    public BeanTestConditionService getTestService() 
    { 
    return new BeanTestConditionService(); 
    } 
} 

public class BeanTestConditionService 
{ 
    public BeanTestConditionService() 
    { 
    System.out.println("I am in test setvice"); 
    } 
} 

回答

0

Not supported yet,并且可能不会对某个

最终收盘这是因为 真的被设计时考虑注释全状态模型不会解决,一般是 更适合那里。如果XML配置仍存在任何常见需求,我们宁愿微调已经以XML形式提供的声明性配置文件模型 。

+1

当然可以通过使用某种形式的工厂bean来完成整个决策,然后从工厂方法配置bean ......但是这当然只能解决xml配置限制,而不能用于xml配置。 –

+0

https://jira.spring.io/browse/SPR-10969 – programmer

相关问题