Spring configuration using java:
Here we are going to see how to do spring configuration using java , without spring configuration xml . There are some annotations used for configuring the Beans . You can have one or many configuration classes . The configuration classes should be annotated with @Configuration and all the beans should be annotated with @Bean .
Lets understand this by a sample configuration .
1 2 3 4 5 6 7 8 9 10 11 |
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class mySpringConf { @Bean public HelloWorld helloWorld() { return new HelloWorld(); // instantiate helloWorld object } } |
1 2 3 4 5 6 7 8 9 10 11 |
public class HelloWorld { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } |
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringConfDemo { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(mySpringConf.class); HelloWorld helloWorld = ctx.getBean(HelloWorld.class); helloWorld.setMessage("Welcome to j2eereference.com"); System.out.println(helloWorld.getMessage()); } } |
Output
May 30, 2017 5:17:28 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue May 30 17:17:28 IST 2017]; root of context hierarchy
Welcome to j2eereference.com
Here you can notice the following:
- Created mySpringConf class for configuring the beans ,Each beans should be annotated as @Bean.
- Created the AnnotationConfigApplicationContext object by providing the configuration class name ( mySpringConf .class) in the SpringConfDemo class
- Got the Helloworld object using the getBean method of AnnotationConfigApplicationContext
Bean aliasing
We can have multiple names for a bean and which can be configured as below.
@Bean(name = { “name-1”, “name-2”, “name-3” })
or
@Bean( { “name-1”, “name-2”, “name-3” })
1 2 3 4 5 6 7 8 9 10 11 |
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class mySpringConf { @Bean({"name-1", "name-2", "name-3"}) public HelloWorld helloWorld() { return new HelloWorld(); } } |
and the bean can be accessed by calling getBean() method with bean name as parameter . It could be name-1 ,name-2 or name-3 as below
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringConfDemo { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(mySpringConf.class); HelloWorld helloWorld = (HelloWorld) ctx.getBean("name-1"); helloWorld.setMessage("Welcome to j2eereference.com"); System.out.println(helloWorld.getMessage()); } } |
Bean description
We can configure the bean description with annotation. This is particularly useful when beans are exposed (Example: via JMX) for monitoring purposes.
@Bean
@Description(“Description about the bean”)
[…] the previous post we have explained how to configure spring with java . Here we are going to see how to configure the bean scope using Java […]