Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

@Profile and @Primary should be used together?  RSS feed

 
Ranch Hand
Posts: 1730
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I configure two beans in a class annotated with @Configuration:
   
   @Bean
   @Profile("english")
   
   public HelloWorldService helloWorldEnglish(){
       ....
   }

   @Bean
   @Profile("spanish")
   @Primary
   public HelloWorldService helloWorldServiceSpanish(){
     ...
   }
My application.properties define spring.profiles.active = english
But it still complains that more than one bean is found.
I have to put @Primary to the helloWorldEnglish bean to make it work.
My question is why should I put @Primary to autowire the right bean even though my profile has been set to english ?
 
Greenhorn
Posts: 17
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default the name of Bean is same as method name.

Try giving them same name with this code:



Also, post the code you are using to fetch the bean.
 
Himai Minh
Ranch Hand
Posts: 1730
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jain,
My toy code is https://github.com/aCodeRancher/diexample/tree/advanced-autowire









So, in this case, the english profile is active. The container can simply choose the @Profile("english") bean. But it does not work because there are two helloWorldService beans. I have to set at least one of them @Primary.
My question is if the bean has been annotated with @Profile ("english"), why the container still does not know which bean to choose?
 
Himai Minh
Ranch Hand
Posts: 1730
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have a gut's feeling that @Profile is only used for the method/class when a particular profile is set to be active. It has nothing to do with the bean.
For example,

When the active profile is english, then the first method is chosen.

But since the method registers a bean, that is a different story. The container needs to choose one of them. In this particular case, both beans have the same priority. The container will choose either one depends on the active profile.


Any other comments ?  Correct me if I am wrong.
Thanks.
 
SaurabhRc Jain
Greenhorn
Posts: 17
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I executed your code after removing the @Primary annotation.
I got the error

expected single matching bean but found 5: helloWorldServiceEnglish,french,helloWorldServiceGerman,helloWorldServicePolish,helloWorldServiceRussian

There was no bean registered with "helloWorldServiceSpanish".

Then, I added name to the @Bean annotation:


It worked successfully for both english and spanish profiles.
Give it a try.
 
Himai Minh
Ranch Hand
Posts: 1730
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jain. Thanks for your suggestion.
It seems to me that we can draw these conclusion:
1. With beans of different name, same type , use @Primary and @Profile
2. With beans of the same name, same type, omit @Primary, @Profile is good enough.
 
Himai Minh
Ranch Hand
Posts: 1730
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I add one more test case with two beans with @Profile ("english"). And in this case, I have to set one of them as @Primary; otherwise, the container does not know which one to inject.

It seems like in any case when the container cannot determine which bean to inject, we have to use @Primary.
The source code is committed here https://github.com/aCodeRancher/diexample/tree/advanced-autowire/src

 
Trust God, but always tether your camel... to this tiny ad.
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!