Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Is @Configuration optional

 
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When writing java based configuration in Spring is @Configuration optional because:

JavaConfig.java


and then this

App.java


It seems that the spring container is fine with creating the beans even if I happen to remove @Configuration from the JavaConfig.java file. If that is the case then what is the use of it?
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's useful when you want Spring to auto scan the classpath and find those configuration beans by itself, instead of explicitly registering them in code.
When you have dozens of beans - as often happens in large web applications - it's no fun registering them all in code.
Spring supports auto scanning for other types of beans too. Prefer to use auto scanning whenever possible.
 
Shouvik Bhattacharya
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:It's useful when you want Spring to auto scan the classpath and find those configuration beans by itself, instead of explicitly registering them in code.


If I have understood it correctly then by auto-scan you mean beans discovered via @ComponentScan and when your are saying explicitly registering them in code you referring to the way I have done it in JavaConfig.java file? I shall appreciate if you can explain with a code snippet.

Thanks
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is explicit registration of JavaConfig as a configuration class:


For auto scanning, you don't have to change anything other than that 1 line.
Let's say your config class package is "springtest". Then auto scanning is done this way by specifying the package instead of the class:


Run this and you'll see the same behaviour is as before, though JavaConfig.class is not explicitly mentioned. That's because Spring is looking for all classes with @Configuration annotation, and registering
them into the context.

Now try adding a new config class springtest.JavaConfig2 which creates another set of beans, annotate that too with @Configuration and see what happens

Next try removing the @Configuration on one of them and see what happens.
 
What are you doing? You are supposed to be reading this tiny ad!
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic