[英]Autowire a bean from spring config - Environment specific
I'm trying to have environment specific boolean flag.我正在尝试使用特定于环境的布尔标志。 In my spring config, I defined three beans which are environment specific as per below.在我的 spring 配置中,我定义了三个特定于环境的 bean,如下所示。
<bean id="validationFlag-E1" class="java.lang.Boolean.FALSE"/>
<bean id="validationFlag-E2" class="java.lang.Boolean.TRUE"/>
<bean id="validationFlag-E3" class="java.lang.Boolean.TRUE"/>
I have also "spring.profiles.active" system property defined at server level and its value is "E1 /E2 /E3" based on the environment.我还在服务器级别定义了“spring.profiles.active”系统属性,其值是基于环境的“E1/E2/E3”。
In my service, i'm trying to do autowiring as per below but it is not working and getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate.在我的服务中,我正在尝试按照以下方式进行自动装配,但它不起作用,并且没有可用的“java.lang.Boolean”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。 Please advise me.请建议我。
@Autowired
@Qualifier("validationFlag-${spring.profiles.active}")
Boolean evalFlag;
We can achieve the above requirement by having individual environment specific property file.我们可以通过拥有单独的环境特定属性文件来实现上述要求。 But I don't want to create three property files for a single flag.但我不想为单个标志创建三个属性文件。 So, trying above approach in my spring framework project.所以,在我的 spring 框架项目中尝试上述方法。
You can use environmental property ${blah.blah.flag}
(or whatever name you choose) with value of a flag ( false
or true
) -Dblah.blah.flag=true
and then inject that value as a property您可以将环境属性${blah.blah.flag}
(或您选择的任何名称)与标志值( false
或true
) -Dblah.blah.flag=true
,然后将该值作为属性注入
@Value("${blah.blah.flag}")
private Boolean flag;
Change your bean definition to:将您的 bean 定义更改为:
<bean id="validationFlag-E1" class="java.lang.Boolean">
<constructor-arg value="false"/>
</bean>
<bean id="validationFlag-E2" class="java.lang.Boolean">
<constructor-arg value="true"/>
</bean>
<bean id="validationFlag-E3" class="java.lang.Boolean">
<constructor-arg value="true"/>
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.