@Configuration – @Configuration as a replacement to the XML based configuration for configuring spring beans.So instead of an xml file we write a class and annotate that with @Configuration and define the beans in it using @Bean annotation on the methods.It is just another way of configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
Use @Configuration annotation on top of any class to declare that this class provides one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
AppConfig.java
@Configuration public class AppConfig { @Bean(name="demoService") public DemoClass service() { } }
pom.xml
The following dependency should be added to pom.xml before using @configuration annotation to get the bean from the context
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version> </dependency>
public class VerifySpringCoreFeature { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); DemoManager obj = (DemoManager) context.getBean("demoService"); System.out.println( obj.getServiceName() ); } }
What if I want to ensure the beans are already loaded even before requested and fail-fast
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MySpringApp { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(MyConfiguration.class); ctx.refresh(); MyBean mb1 = ctx.getBean(MyBean.class); MyBean mb2 = ctx.getBean(MyBean.class); ctx.close(); } }
In the above example Spring loads beans into it’s context before we have even requested it. This is to make sure all the beans are properly configured and application fail-fast if something goes wrong.
Now what will happen if we uncomment the @Configuration annotation in the above class. In this case, if we make a call to myBean() method then it will be a plain java method call and we will get a new instance of MyBean and it won’t remain singleton.
@Configurble – How to inject dependencies into objects that are not created by Spring
public class CarSalon { //... public void testDrive() { Car car = new Car(); car.startCar(); } } @Component public class Car { @Autowired private Engine engine; @Autowired private Transmission transmission; public void startCar() { transmission.setGear(1); engine.engineOn(); System.out.println("Car started"); } } @Component public class Engine { //... } @Component public class Transmission { //... }
In the above example
- We try to create a Object for Car class using new operator
- How ever objects created using new operator are not container managed rather Java Runtime Managed
- Trying to access the startCar method using car object will throw NullPointerException
- However using @Configurable annotation will tell Spring to inject dependencies into the object before the constructor is run
- You need to have these JAR files in pom.xml inorder to make it work.aspectj-x.x.x.jar, aspectjrt.jar, aspectjveawer-x.x.x.jar
@Configurable(preConstruction = true) @Component public class Car { @Autowired private Engine engine; @Autowired private Transmission transmission; public void startCar() { transmission.setGear(1); engine.engineOn(); System.out.println("Car started"); } }