- In previous blog we saw Dependency Injection in Spring using Annotation.
- Here we will see how we can do same using Annotation but without defining Autoscan in xml.
- We will make use of @Configuration and @Bean to define configuration 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 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.