@Inject annotation was introduced as JSR - 330 specification for the Dependency Injection. Its very much similar to @Autowired annotation. It doesn't have @Required attribute as @Autowired.
Program to demonstrate how to use @Inject annotation in Spring Framework for Dependency Injection
1. A simple POJO class as Employee having Address POJO reference variable injected :
2. Address POJO as Injected into Employee
3. Test class for testing the application :
4. Spring configuration file for the @Inject annotation :
Output of the program :
Program to demonstrate how to use @Inject annotation in Spring Framework for Dependency Injection
1. A simple POJO class as Employee having Address POJO reference variable injected :
package com.hubberspot.spring.inject;
import javax.inject.Inject;
// Create a POJO class Employee which has a
// Address Object reference as instance variable
public class Employee {
private String name;
private int age;
@Inject
private Address address;
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
2. Address POJO as Injected into Employee
package com.hubberspot.spring.inject;
public class Address {
private String street;
private String city;
private String state;
public Address() {
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
3. Test class for testing the application :
package com.hubberspot.spring.inject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// ApplicationContext is a Spring interface which
// provides with the configuration for an application.
// It provides us with all the methods that BeanFactory
// provides. It loads the file resources in a older
// and generic manner. It helps us to publish events to the
// listener registered to it. It also provides quick support
// for internationalization. It provides us with the object
// requested, it reads the configuration file and provides
// us with the necessary object required.
// We are using concrete implementation of ApplicationContext
// here called as ClassPathXmlApplicationContext because this
// bean factory reads the xml file placed in the classpath of
// our application. We provide ClassPathXmlApplicationContext
// with a configuration file called as inject.xml placed
// at classpath of our application.
ApplicationContext context =
new ClassPathXmlApplicationContext(("inject.xml"));
// In order to get a object instantiated for a particular bean
// we call getBean() method of ClassPathXmlApplicationContext
// passing it the id for which the object is to be needed.
// Here getBean() returns an Object. We need to cast it back
// to the Employee object. Without implementing new keyword we
// have injected object of Employee just by reading an xml
// configuration file.
Employee employee = (Employee)context.getBean("employee");
if(employee.getAddress()==null){
System.out.println("The Employee Name : " + employee.getName());
System.out.println("The Employee Age : " + employee.getAge());
System.out.println("The Employee Address : " + "is not provided");
}
else{
System.out.println("The Employee Name : " + employee.getName());
System.out.println("The Employee Age : " + employee.getAge());
System.out.println("The Employee Address : " +
employee.getAddress().getStreet() + " " +
employee.getAddress().getCity() + " " +
employee.getAddress().getState());
}
}
}
4. Spring configuration file for the @Inject annotation :
|
Output of the program :
