简体   繁体   English

使用spring的条件bean

[英]conditional beans using spring

I am trying to write a ValidatorFactory which will give me a validator based on its type 我正在尝试编写一个ValidatorFactory ,它将根据其类型为我提供验证器

public Validator getNewValidator(ValidatorType type){

    switch:
         case a : new Validator1();
         break;
          case b : new Validator2();
        break;

}

I want to write using spring xml beans definition 我想用spring xml bean定义来编写

I can use method injection but it will let me create only one object and the method does 我可以使用方法注入,但它只允许我创建一个对象,而方法可以

not take any arguments. 不接受任何争论。

I don't want to use FactoryBean .. I am just looking whether we can do this using spring xml 我不想使用FactoryBean ..我只是想看看我们是否可以使用spring xml来做到这一点

bean definition. bean的定义。

you can do conditional bean injection with plain xml. 你可以使用普通的xml进行条件bean注入。 The "ref" attribute can be triggered by property values from a property file and thus create conditional beans depending on property values. “ref”属性可以由属性文件中的属性值触发,从而根据属性值创建条件bean。 This feature is not documented but it works perfect. 此功能未记录,但它完美无缺。

<bean id="validatorFactory" class="ValidatorFactory">
<property name="validator" ref="${validatorType}" />
</bean>

<bean id="validatorTypeOne" class="Validator1" lazy-init="true" />
<bean id="validatorTypeTwo" class="Validator2" lazy-init="true" />

And the content of the property file would be: 并且属性文件的内容将是:

validatorType=validatorTypeOne validatorType = validatorTypeOne

To use the property file in your xml just add this context to the top of your spring config 要在xml中使用属性文件,只需将此上下文添加到spring配置的顶部即可

<context:property-placeholder location="classpath:app.properties" />

对于复杂的情况(比暴露的情况更复杂), Spring JavaConfig可能是你的朋友。

If you are using annotation ( @Autowired , @Qualifier etc) instead of xml, you are not able to make conditional beans work (at least at current version 3). 如果您使用注释(@ @Autowired ,@ @Qualifier等)而不是xml,则无法使条件bean工作(至少在当前版本3中)。 This is due to @Qualifier does not support expression 这是由于@Qualifier 支持表达

@Qualifier(value="${validatorType}")

More information is at https://stackoverflow.com/a/7813228/418439 有关更多信息, 访问https://stackoverflow.com/a/7813228/418439

I had an slightly different requirements. 我的要求略有不同。 In my case I wanted to have encoded password in production but plain text in development. 在我的情况下,我想在生产中编码密码,但在开发中使用纯文本。 Also, I didn't have access to parent bean parentEncoder . 此外,我没有访问父bean parentEncoder This is how I managed to achieve that: 这就是我设法实现的目标:

 <bean id="plainTextPassword" class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder"/> <bean id="shaPassword" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"> <constructor-arg type="int" value="256"/> </bean> <bean id="parentEncoder" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="targetSource"> <bean class="org.springframework.aop.target.HotSwappableTargetSource"> <constructor-arg ref="${password.encoding}Password"/> </bean> </property> </bean> 

Of course, I defined password.encoding in a property file with possible values as sha or plainText . 当然,我在属性文件中定义了password.encoding ,其可能的值为shaplainText

You should be able to do this: 你应该能够做到这一点:

<bean id="myValidator" factory-bean="validatorFactory" factory-method="getNewValidator" scope="prototype">
    <constructor-arg><ref bean="validatorType"/></constructor-arg>
</bean>

<bean id="validatorType" ... />

Of course, it uses an automatically configured FactoryBean underneath but you avoid any Spring dependency in your code. 当然,它在下面使用自动配置的FactoryBean ,但您可以避免代码中的任何Spring依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM