Spring - Java Based Configuration
Spring - Java Based Configuration
@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. The simplest possible @Configuration class would be as follows −
package JavaBasedConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloworldConfig {
@Bean
public HelloWorld helloWorld()
{
return new HelloWorld();
}
}
The above code will be equivalent to the following XML configuration −
<beans> <bean id = "helloWorld" class = "JavaBasedConfiguration.HelloWorld" /> </beans>
Here, the method name is annotated with @Bean works as bean ID and it creates and returns the actual bean. Your configuration class can have a declaration for more than one @Bean. Once your configuration classes are defined, you can load and provide them to Spring container using AnnotationConfigApplicationContext as follows.
package JavaBasedConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new AnnotationConfigApplicationContext(HelloworldConfig.class);
HelloWorld helloWorld = (HelloWorld)context.getBean(HelloWorld.class);
helloWorld.setMessage("Hello Hyy Bye");
helloWorld.setAge(20);
helloWorld.setName("shiva");
System.out.println(helloWorld);
}
}
Output :
HelloWorld [name=shiva, age=20, message=Hello Hyy Bye, getName()=shiva, getAge()=20, getClass()=class JavaBasedConfiguration.HelloWorld, hashCode()=1105322512,
Example
Let us have a working Eclipse IDE in place and take the following steps to create a Spring application −
Steps | Description |
---|---|
1 | Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Because you are using Java-based annotations, so you also need to add CGLIB.jar from your Java installation directory and ASM.jar library which can be downloaded from asm.ow2.org. |
4 | Create Java classes TextEditorConfig, TextEditor, SpellChecker and MainApp under the com.tutorialspoint package. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
Here is the content of TextEditorConfig.java file
package JavaBasedConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@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 JavaBasedConfiguration;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
Following is the content of another dependent class file SpellChecker.java
package JavaBasedConfiguration;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
Following is the content of the MainApp.java file
package JavaBasedConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(TextEditorConfig.class);
TextEditor te = ctx.getBean(TextEditor.class);
te.spellCheck();
}
}
Once you are done creating all the source files and adding the 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, it will print the following message −
Inside SpellChecker constructor. Inside TextEditor constructor. Inside checkSpelling.
Comments
Post a Comment