java 使用 spring 的条件 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3141130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
conditional beans using spring
提问by Shekhar
I am trying to write a ValidatorFactorywhich 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 beans定义来写
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.
豆定义。
回答by guido
you can do conditional bean injection with plain xml. The "ref" attribute can be triggered by property values from a property file and thus create conditional beans depending on property values. This feature is not documented but it works perfect.
您可以使用纯 xml 进行条件 bean 注入。“ref”属性可以由属性文件中的属性值触发,从而根据属性值创建条件bean。此功能没有记录在案,但它完美无缺。
<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
验证器类型=验证器类型一
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" />
回答by jplandrain
For complex cases (more complex than the one exposed), Spring JavaConfigcould be your friend.
对于复杂的情况(比公开的情况更复杂),Spring JavaConfig可能是您的朋友。
回答by Lee Chee Kiam
If you are using annotation (@Autowired, @Qualifieretc) instead of xml, you are not able to make conditional beans work (at least at current version 3). This is due to @Qualifier does notsupport expression
如果您使用的注解(@Autowired,@Qualifier等),而不是XML,你是不是能够做出有条件豆的工作(至少在目前的第3版)。这是由于@Qualifier并不能支持表达
@Qualifier(value="${validatorType}")
More information is at https://stackoverflow.com/a/7813228/418439
回答by SHOJAEI BAGHINI
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. This is how I managed to achieve that:
我的要求略有不同。就我而言,我想在生产中编码密码,但在开发中使用纯文本。另外,我无权访问 parent bean parentEncoder。这就是我设法实现这一目标的方式:
<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>
<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.encodingin a property file with possible values as shaor plainText.
当然,我password.encoding在属性文件中定义了可能的值为shaor plainText。
回答by gpeche
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 FactoryBeanunderneath but you avoid any Spring dependency in your code.
当然,它使用FactoryBean下面的自动配置,但您可以避免代码中的任何 Spring 依赖项。

