Autowire a bean from spring config - Environment specific











up vote
0
down vote

favorite












I'm trying to have environment specific boolean flag. In my spring config, I defined three beans which are environment specific as per below.



    <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.



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. Please advise me.



@Autowired
@Qualifier("validationFlag-${spring.profiles.active}")
Boolean emailInUseValidationFlag;


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 and I'm using spring mvc.










share|improve this question
























  • Are you using Boot?
    – chrylis
    Nov 10 at 0:44










  • No. Using spring mvc
    – user3858442
    Nov 10 at 1:15

















up vote
0
down vote

favorite












I'm trying to have environment specific boolean flag. In my spring config, I defined three beans which are environment specific as per below.



    <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.



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. Please advise me.



@Autowired
@Qualifier("validationFlag-${spring.profiles.active}")
Boolean emailInUseValidationFlag;


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 and I'm using spring mvc.










share|improve this question
























  • Are you using Boot?
    – chrylis
    Nov 10 at 0:44










  • No. Using spring mvc
    – user3858442
    Nov 10 at 1:15















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to have environment specific boolean flag. In my spring config, I defined three beans which are environment specific as per below.



    <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.



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. Please advise me.



@Autowired
@Qualifier("validationFlag-${spring.profiles.active}")
Boolean emailInUseValidationFlag;


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 and I'm using spring mvc.










share|improve this question















I'm trying to have environment specific boolean flag. In my spring config, I defined three beans which are environment specific as per below.



    <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.



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. Please advise me.



@Autowired
@Qualifier("validationFlag-${spring.profiles.active}")
Boolean emailInUseValidationFlag;


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 and I'm using spring mvc.







java spring spring-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 1:20

























asked Nov 10 at 0:37









user3858442

106




106












  • Are you using Boot?
    – chrylis
    Nov 10 at 0:44










  • No. Using spring mvc
    – user3858442
    Nov 10 at 1:15




















  • Are you using Boot?
    – chrylis
    Nov 10 at 0:44










  • No. Using spring mvc
    – user3858442
    Nov 10 at 1:15


















Are you using Boot?
– chrylis
Nov 10 at 0:44




Are you using Boot?
– chrylis
Nov 10 at 0:44












No. Using spring mvc
– user3858442
Nov 10 at 1:15






No. Using spring mvc
– user3858442
Nov 10 at 1:15














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Change your bean definition to:



<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>





share|improve this answer





















  • Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
    – user3858442
    Nov 10 at 15:28










  • In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
    – Ajish Thankachan
    Nov 10 at 16:07












  • Thanks a lot. It is working now.
    – user3858442
    Nov 10 at 20:17


















up vote
1
down vote













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



@Value("${blah.blah.flag}")
private Boolean flag;





share|improve this answer























  • We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
    – user3858442
    Nov 10 at 1:18






  • 1




    What do you mean by specific property file? Instead of spring.profiles active you pass that flag
    – Ivan
    Nov 10 at 1:44










  • flag-E1.properties flag-E2.properties , flag-E3.properties
    – user3858442
    Nov 10 at 3:41










  • As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
    – Ivan
    Nov 10 at 3:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234986%2fautowire-a-bean-from-spring-config-environment-specific%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Change your bean definition to:



<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>





share|improve this answer





















  • Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
    – user3858442
    Nov 10 at 15:28










  • In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
    – Ajish Thankachan
    Nov 10 at 16:07












  • Thanks a lot. It is working now.
    – user3858442
    Nov 10 at 20:17















up vote
0
down vote



accepted










Change your bean definition to:



<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>





share|improve this answer





















  • Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
    – user3858442
    Nov 10 at 15:28










  • In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
    – Ajish Thankachan
    Nov 10 at 16:07












  • Thanks a lot. It is working now.
    – user3858442
    Nov 10 at 20:17













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Change your bean definition to:



<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>





share|improve this answer












Change your bean definition to:



<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>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 6:57









Ajish Thankachan

612




612












  • Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
    – user3858442
    Nov 10 at 15:28










  • In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
    – Ajish Thankachan
    Nov 10 at 16:07












  • Thanks a lot. It is working now.
    – user3858442
    Nov 10 at 20:17


















  • Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
    – user3858442
    Nov 10 at 15:28










  • In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
    – Ajish Thankachan
    Nov 10 at 16:07












  • Thanks a lot. It is working now.
    – user3858442
    Nov 10 at 20:17
















Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
– user3858442
Nov 10 at 15:28




Looks like bean definition is correct. I don't see any XML model errors. But getting No qualifying bean of type 'java.lang.Boolean' available: expected at least 1 bean which qualifies as autowire candidate. How can we resolve this?
– user3858442
Nov 10 at 15:28












In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
– Ajish Thankachan
Nov 10 at 16:07






In your @Qualifier not getting value for the placeholder -${spring.profiles.active} because of that you are getting this error if you want profile specific bean means you have to define like this <beans profile="E1"> <bean id="validationFlag" class="java.lang.Boolean.False"/> </beans> <beans profile="E2"> <bean id="validationFlag" class="java.lang.Boolean.True"/> </beans>
– Ajish Thankachan
Nov 10 at 16:07














Thanks a lot. It is working now.
– user3858442
Nov 10 at 20:17




Thanks a lot. It is working now.
– user3858442
Nov 10 at 20:17












up vote
1
down vote













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



@Value("${blah.blah.flag}")
private Boolean flag;





share|improve this answer























  • We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
    – user3858442
    Nov 10 at 1:18






  • 1




    What do you mean by specific property file? Instead of spring.profiles active you pass that flag
    – Ivan
    Nov 10 at 1:44










  • flag-E1.properties flag-E2.properties , flag-E3.properties
    – user3858442
    Nov 10 at 3:41










  • As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
    – Ivan
    Nov 10 at 3:46















up vote
1
down vote













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



@Value("${blah.blah.flag}")
private Boolean flag;





share|improve this answer























  • We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
    – user3858442
    Nov 10 at 1:18






  • 1




    What do you mean by specific property file? Instead of spring.profiles active you pass that flag
    – Ivan
    Nov 10 at 1:44










  • flag-E1.properties flag-E2.properties , flag-E3.properties
    – user3858442
    Nov 10 at 3:41










  • As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
    – Ivan
    Nov 10 at 3:46













up vote
1
down vote










up vote
1
down vote









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



@Value("${blah.blah.flag}")
private Boolean flag;





share|improve this answer














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



@Value("${blah.blah.flag}")
private Boolean flag;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 1:17

























answered Nov 10 at 0:57









Ivan

4,4891720




4,4891720












  • We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
    – user3858442
    Nov 10 at 1:18






  • 1




    What do you mean by specific property file? Instead of spring.profiles active you pass that flag
    – Ivan
    Nov 10 at 1:44










  • flag-E1.properties flag-E2.properties , flag-E3.properties
    – user3858442
    Nov 10 at 3:41










  • As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
    – Ivan
    Nov 10 at 3:46


















  • We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
    – user3858442
    Nov 10 at 1:18






  • 1




    What do you mean by specific property file? Instead of spring.profiles active you pass that flag
    – Ivan
    Nov 10 at 1:44










  • flag-E1.properties flag-E2.properties , flag-E3.properties
    – user3858442
    Nov 10 at 3:41










  • As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
    – Ivan
    Nov 10 at 3:46
















We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
– user3858442
Nov 10 at 1:18




We can do that way but it would end up creating environment specific property file in total 3 files. I'm not interested in creating multiple files only for this single flag. So, trying the above approach.
– user3858442
Nov 10 at 1:18




1




1




What do you mean by specific property file? Instead of spring.profiles active you pass that flag
– Ivan
Nov 10 at 1:44




What do you mean by specific property file? Instead of spring.profiles active you pass that flag
– Ivan
Nov 10 at 1:44












flag-E1.properties flag-E2.properties , flag-E3.properties
– user3858442
Nov 10 at 3:41




flag-E1.properties flag-E2.properties , flag-E3.properties
– user3858442
Nov 10 at 3:41












As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
– Ivan
Nov 10 at 3:46




As I've said you do not need a properties file to pass properties ro Spring. You can use system and environmental properties for that
– Ivan
Nov 10 at 3:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234986%2fautowire-a-bean-from-spring-config-environment-specific%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff