Overview
In previous blog we saw Dependency Injection in Spring using XML.
Here we will see how we can do same using Annotation
Construction Injection using Annotation
xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan for Annotations -->
<context:component-scan base-package="com.main.spring.annotation"></context:component-scan>
</beans>
Paint class
public interface Paint1 {
public void getColor();
}
@Component // for autoscan
public class RedPaint implements Paint {
@Override
public void getColor() {
System.out.println("I will paint Red color");
}
}
Car class
public interface Vehicle {
public void getType();
public void getPaint();
}
@Component
public class Car implements Vehicle{
private Paint paint;
// Constructor Injection
@Autowired // Note if we comment this autowired it will still work as of Spring 4.3 assuming only single constructor is defined
Car(Paint pa){
paint = pa;
}
@Override
public void getType() {
System.out.println("This is a Car");
}
@Override
public void getPaint() {
paint.getColor();
}
}
Main class
public class MainApp {
public static void main(String[] args) {
// load the spring configuration file
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("annotationContext.xml");
System.out.println("Loading Config Done");
Vehicle veh = context.getBean("car", Vehicle.class);
veh.getPaint();
veh.getType();
context.close();
}
}
OUTPUT:
Aug 05, 2018 10:12:47 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dcf3e99: startup date [Sun Aug 05 22:12:47 IST 2018]; root of context hierarchy
Aug 05, 2018 10:12:47 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [annotationContext.xml]
Loading Config Done
I will paint Red color
This is a Car
Aug 05, 2018 10:12:47 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@dcf3e99: startup date [Sun Aug 05 22:12:47 IST 2018]; root of context hierarchy
Explanation
- In xml we are telling Spring to look for a package to scan for Annotation based configurations like in this case @Component and @Autowired .
- @Autowired above Car is way of injecting Bean as a Constructor
Setter Injection using Annotation
- Only change will be in Car class, the way we inject object.
- Instead of autowiring Constructor we are using a setter method autowiring.
- Other piece of code remains same.
// Setter Injection
@Autowired
public void setPaint(Paint mypaint){
System.out.println("Setter injection called. Setting Paint");
paint = mypaint;
}
Summary
- To use annotation based injection we need to define base package full path of class in xml.
- UseĀ @Component, @Autowired for Objects we need to inject.
- You can get the code used above here. Just look for com.main.spring.annotationĀ package.
0 Comments