Last updated on February 28th, 2024
The “@Autowired” annotation is given to perform the constructor mode of auto wiring. This annotation can be applied at the field level, constructor level, and arbitrary, method level. We can use “@Component” and “@Autowired” annotations. The “@Compotent” annotation is used for registering a java class as Spring Bean and the “@Autowired” annotation to inject a Dependent class to the Target class by using the constructor.
In the example, we will avoid <property> and <bean id= “bean_id_name” class= “ package”/> to configure in the XML configuration file. We can use the “@Autowired” annotation for the constructor injection and then the Spring container injects the dependent bean class. The “@Component” annotation for Spring bean configuration then the Spring container automatically registers the class which is annotated with this annotation. The “<context:component-scan base-package=”base_package_name”/>” for configuring the base into the XML configuration file. So, the Spring container scans the base package of the application.
In this example, the “CricketCoach” POJO class is a dependent class that is annotated “@Component” annotation. This class contains a 0-param constructor which is executed when this class is instantiated by the Spring Container and a method that returns a message when this method is accessed by us. The “CricketTeam” POJO class is a target class that is annotated with “@Component” and “@Autowired” annotations. This class contains parameterized constructor which is executed when this class is instantiated. The “applicationContext.xml” file contains this tag “<context:component-scan base-package=”com.poc”/>” to enable scanning component annotated class as Spring bean class of the application. In the main class i.e., ClientApp.java we can get the Spring Bean class i.e., CricketTeam.java which injects CricketCoach.java class to this class with the @Autowired by calling its parameterized constructor and instantiated with parameterized constructor by the Spring Container. So, thus we will access the method of the Spring Bean class and print output on the console.
Development Process:
1. Keep eclipse IDE ready
2. Project Structure
3. Enable component scanning in the Spring XML config file
4. Add the jar file to the Build Path of the Project for the example of the constructor injection with annotation
5. Create a Dependent Bean Class(@Component)
6. Create a Target Bean Class(@Component and @Autowired)
7. Create Main Class
8. Run the App
1. Keep eclipse IDE ready
2. Project Structure
3. Enable component scanning in the Spring XML config file
applicationContext.xml:
<?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">
<!-- enable component scanning -->
<context:component-scan base-package="com.poc"/>
</beans>
4. Add the jar file to the Build Path of the Project for the example of the constructor injection with annotation
Right Click on Project(Constructor_Injection_With_Annotation) ⇒ Build Path ⇒ Java Build Path ⇒ libraries ⇒ Add External jars
Commons-logging-<version>.jar
spring-beans-<version>.jar
spring-context-<version>.jar
spring-context-support-<version>.jar
spring-core-<version>.jar
spring-expression-<version>.jar
spring-aop-<version>.jar
5. Create a Dependent Bean Class(@Component)
CricketCoach.java(Pojo Class):
package com.poc.bean;
import org.springframework.stereotype.Component;
@Component
public class CricketCoach {
public CricketCoach() {
System.out.println("Constructor: 0-param constructor call of CricketCoach");
}
public String getDailyWorkout() {
return "4 hours of Practice";
}
}
→ This “@Component” annotation is used to configure the “CricketCoach” class as Spring Bean.
6. Create a Target Bean Class(@Component and @Autowired)
CricketTeam.java(Pojo class):
package com.poc.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CricketTeam {
//dependent class
private CricketCoach teamCoach;
@Autowired
public CricketTeam(CricketCoach coach) {
System.out.println("Constructor: parameterized constructor call Cricket Team");
this.teamCoach = coach;
}
public String getInstruction() {
return "Coach Instruction:" + teamCoach.getDailyWorkout();
}
}
→ This “@Component” annotation is used to configure the “CricketTeam” class as Spring Bean.
→ In this class “@Autowired” annotation, the Spring container creates the “CricketCoach” class object using a 0-param constructor and creates the “Cricket Team” class object using the parameterized constructor for using constructor injection.
7. Create Main Class
ClientApp.java:
package com.poc.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.poc.bean.CricketTeam;
public class ClientApp {
public static void main(String[] args) {
//load the spring Configuration file
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
//retrieve bean from spring container
CricketTeam theTeam=context.getBean("cricketTeam",CricketTeam.class);
// call the method on bean
System.out.println(theTeam.getInstruction());
context.close();
}
}
8. Run the App
Output:
Constructor:0-param constructor call of CricketCoach
Constructor: parameterized constructor call Cricket Team
Coach Instruction:4 hours of Practice
Conclusion:
This example is explained How to configure Constructor Injection with Annotation? How to use @Autowired in Constructor Injection? How to use @Component in Constructor Injection?