Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What Does @Configuration Do?  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 198
1
  • Mark post as helpful
  • send pies
  • 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.
 
L Foster
Ranch Hand
Posts: 225
12
Android AngularJS Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 198
1
  • Mark post as helpful
  • send pies
  • 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?
 
William Barnes
Ranch Hand
Posts: 1043
1
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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: 198
1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!