Spring @Configuration annotation helps in creating Java based configuration in Spring rather than relying on XML based configuration. Any class annotated with @Configuration indicates that a class declares one or more @Bean methods that are processed by the Spring container to generate bean definitions and manage the overall bean lifecycle.
How to use @Configuration annotation in Spring
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
Here AppConfig class is annotated with @Configuration indicating that it declares bean methods. In the class that method is myBean() annotated with @Bean annotation. Spring container creates and manages this bean instance.
How to bootstrap @Configuration classes
@Configuration classes are typically bootstrapped using either AnnotationConfigApplicationContext or its web-capable variant, AnnotationConfigWebApplicationContext.
If we take the Configuration as used above for AppConfig and try to bootstrap it then it can be done as given below-
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
MyBean myBean = ctx.getBean("myBean", MyBean.class);
myBean.myMethod();
ctx.close();
}
}
MyBean.java
public class MyBean {
public void myMethod() {
System.out.println("In MyMethod of MyBean class");
}
}
Spring @Configuration annotation example
Here is another example showing how to use @Configuration along with @Autowired and @Component annotations. This example shows the layered architecture having Service and DAO layers.
@Service
public class UserService {
@Autowired
UserDAO userDAO;
public void getUsers() {
userDAO.getUsers();
}
}
UserDAO Interface
public interface UserDAO {
public void getUsers();
}
UserDAOImpl.java
@Repository
public class UserDAOImpl implements UserDAO {
public void getUsers() {
System.out.println("In getUsers method, connect to DB and get data");
}
}
Configuration class
Appconfig class is annotated with @Configuration indicating that it is the class providing bean configuration. There is also a @ComponentScan annotation with the package to scan for bean definitions. Because of component scanning classes annotated with @Repository, @Service, @Component are automatically registered as beans.
@Configuration
@ComponentScan(basePackages="com.knpcode")
public class AppConfig {
}
Now you can run the example using the following class.
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = ctx.getBean("userService", UserService.class);
userService.getUsers();
ctx.close();
}
}
Output
14:21:11.584 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userDAO' 14:21:11.586 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 14:21:11.891 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor' 14:21:11.909 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userService' In getUsers method, connect to DB and get data
That's all for the topic Spring @Configuration Annotation. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment