- In previous blog, we saw Dependency Injection in Spring using Annotation.
- Here we will see how we can do the same using Annotation but without defining Autoscan in XML.
- We will make use of @Configuration and @Bean to define configuration the same as we did for XML in above case.
- 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.
- In the main class, we are using AnnotationConfigApplicationContext instead of ClassPathXmlApplicationContext as done for XML based injection.
- AnnotationConfigApplicationContext is also an implementation of ApplicationContext.
- We are passing AppConfiguration.class in AnnotationConfigApplicationContext Object to tell spring about our configuration class.
You can get the code used above here. Just look for com.main.spring.alljava package.