Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
Let's get him boys! We'll make him read this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic