@Configuration & @Bean Annotations
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
Here is the content of HelloWorldConfig.java file:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
} }
Here is the content of HelloWorld.java file: |
package com.tutorialspoint; private String message; public void setMessage(String message){ } public void getMessage(){ } } |
Following is the content of the MainApp.java file: |
package com.tutorialspoint; import org.springframework.context.annotation.*; public class MainApp { ApplicationContext ctx = HelloWorld helloWorld = ctx.getBean(HelloWorld.class); helloWorld.setMessage("Hello World!"); helloWorld.getMessage(); } |
Once you are done with creating all the source filesand adding required additional libraries, let us run the application. You should note that there is no configuration file required. If everything is fine with your application, this will print the following message:
Your Message : Hello World!
ge com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
}
@Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
} }
Here is the content of TextEditor.java file: package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
Injecting Bean Dependencies
When @Beans have dependencies on one another, expressing that dependency is as simple as having one bean method calling another as follow
Here is the content of TextEditorConfig.java file:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
}
@Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
} }
Here is the content of TextEditor.java file: package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker; public void spellCheck(){ } } |
Following is the content of another dependent class file SpellChecker.java: |
package com.tutorialspoint; public class SpellChecker { System.out.println("Inside SpellChecker constructor." ); |
} public void checkSpelling(){ } } |
Following is the content of the MainApp.java file: |
package com.tutorialspoint; import org.springframework.context.annotation.*; public class MainApp { ApplicationContext ctx = TextEditor te = ctx.getBean(TextEditor.class); } } |
Once you are done with creating all the source files and adding required additional libraries, let us run the application. You should note that there is no configuration file required. If everything is fine with your application, this will print the following message:
Inside SpellChecker constructor. |
Inside TextEditor constructor. |
The @Import Annotation
The @Import annotation allows for loading @Bean definitions from another configuration class. Consider a ConfigA class as follows: |
@Configuration public class ConfigA { |
public A a() {
return new A();
} }
You can import above Bean declaration in another Bean Declaration as follows:
@Configuration @Bean public B a() { } |
} |
Now, rather than needing to specify both ConfigA.class and ConfigB.class when instantiating the context, only ConfigB needs to be supplied as follows:
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}