This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

@Profile and overloading methods  RSS feed

 
Ranch Hand
Posts: 1729
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
According to the Spring 5 documentation section 1.13.1


With @Profile on @Bean methods, a special scenario may apply: In the case of overloaded @Bean methods of the same Java method name (analogous to constructor overloading), an @Profile condition needs to be consistently declared on all overloaded methods.
If the conditions are inconsistent, only the condition on the first declaration among the overloaded methods will matter.


If methods are overloaded, that means the methods return beans of the same type. The container does not know which bean to inject or autowire in this situation even if the active profile is set.
I think using @Primary on one of the bean methods can tell the container which bean to inject. The order of methods' declaration does not matter as the document describes.

Reference:
https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/core.html#beans-definition-profiles
 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could use @Qualifier
 
Himai Minh
Ranch Hand
Posts: 1729
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I tried it.
I have two overloading methods annotated with @Bean. They have different @Profile. To my surprise, the second bean in the file is autowired, not the first one.
 
Squanch that. And squanch this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!