• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

What Does @Configuration Do?

 
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I've made it to chapter 3 of Spring in Action, by Craig Walls, and on pages 66 and 67 he has two classes:

and

As I understand it, each @Bean annotation creates and registers a bean. I think I also understand what @Profile does. My question is, what does @Configuration do? The book may have explained this somewhere in the previous two chapters, but I'm at a loss right now as to what it does. If someone could tell me what it does, I'd really appreciate it.
 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an excerpt from not one but 2 books on Spring (identical!).

Starting with Spring 3.0, you can configure Spring metadata purely inside Java classes, completely avoiding any XML configuration while enhancing annotation-based metadata. You annotate any Java class with @Configuration annotation at the class level and have methods annotated as @Configuration annotation on a factory method that instantiates an @Component annotation, or any other specialized bean, to define your application context. Let's see a simple example:



from Spring Essentials, and also from Spring MVC: Designing Real World Web Applications.

I am omitting the example, but trying very hard to mention all the "diverse" sources I have read. ;-)

I have not used Spring since before annotations were introduced, but it looks like @Configuration is used to declare that something can later be used as configuration.

Here's a third excerpt:

The @Configuration annotation hints that the contained class declares one or more @Bean definitions. The @Configuration annotation is meta-annotated with @Component; therefore, it is a candidate for component scanning.



from Spring Microservices that seems a bit more succinct.

I hope this helps.
 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Foster wrote:Here is an excerpt from not one but 2 books on Spring (identical!).

Starting with Spring 3.0, you can configure Spring metadata purely inside Java classes, completely avoiding any XML configuration while enhancing annotation-based metadata. You annotate any Java class with @Configuration annotation at the class level and have methods annotated as @Configuration annotation on a factory method that instantiates an @Component annotation, or any other specialized bean, to define your application context. Let's see a simple example:



from Spring Essentials, and also from Spring MVC: Designing Real World Web Applications.

I am omitting the example, but trying very hard to mention all the "diverse" sources I have read. ;-)

I have not used Spring since before annotations were introduced, but it looks like @Configuration is used to declare that something can later be used as configuration.

Here's a third excerpt:

The @Configuration annotation hints that the contained class declares one or more @Bean definitions. The @Configuration annotation is meta-annotated with @Component; therefore, it is a candidate for component scanning.



from Spring Microservices that seems a bit more succinct.

I hope this helps.


Thanks! So the @Configuration annotation simply lets the compiler know the code "is a candidate for component scanning"? And that's all it does?
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That confuses me.   I always considered having all the configurations in xml was one of Springs main themes.  So having @Configuration hard code your configurations is really confusing.  
 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Barnes wrote:That confuses me.   I always considered having all the configurations in xml was one of Springs main themes.  So having @Configuration hard code your configurations is really confusing.  


One of the themes the book Spring in Action keeps hammering home is that now there are multiple ways to do it; one can still do the configurations in XML if one wants to, or one can use the @Configuration annotation, or one can do a mix.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic