Today, I got familiarized with Dependency Injection in springs, collection injection, inheritance relation in springs and autowiring. Now, I am going to discuss what I learnt about them.
Dependency Injection-
I will take an example of two classes Person and Address to explain what is dependency injection.
Address class-
public class Address {
String area;
String city;
String state;
int zipCode;
//constructors
//getters and setters
}
Person class-
public class Person {
String name;
String email;
int age;
Address address;
public Person() {
address = new Address();
}
//getters and setters
Person class has reference to Address class as an attribute. When Person object gets created, then only object of Address type is created as constructor of Person will be called then only. So, clearly we can see that creation of Address object is highly coupled to creation of Person object. The method for object construction of Address, you saw above lead to many problems. So, we should rather implement loosely coupled way for this. That is done by dependency injection. It can be done by two ways:
This says, create constructor of Person and pass reference of already created Address object to it.
public Person(Address address) {
this.address = address; }
XML for constructor based DI- Write the bean attributes for both the class in the same way. Just add the following line in Bean for Person as specified below. ‘adrs’ is the id of bean of Address.
<constructor-arg ref="adrs1" />
This says, create a setter method in Person and pass reference of already created Address object to it.
public void setAddress(Address address) {
this.address = address; }
XML for setter based DI- Write the bean attributes for both the class in the same way. Just add another property in Bean for Person as specified below. ‘adrs’ is the id of bean of Address.
<property name="address" ref="adrs" />
Injecting Collection:
Let say, we have an attribute in class which is not primitive datatype; rather it is a collection. Ok, let’s take an example of Manager class. A manager can have certificates(0 or more). So, it possesses a list of certificates.
public class Manager {
String name;
String email;
int salary;
List<String> certificates;
//getters and setters
}
In XML file, the code for bean of Manager is as under-
<bean id="mngr" class="com.spring2.Manager">
<property name="name" value="Sheena"/>
<property name="email" value="sheena@ex.com"/>
<property name="salary" value="50000"/>
<property name="certificates">
<list>
<value>AWS</value>
<value>OCJP</value>
<value>CCNA</value>
</list>
</property>
</bean>
Inheritance Relation in springs:
The concept is same as java class inheritance. In XML configuration, we just introduce a new attribute parent, specifying the parent bean as the value of this attribute.
I will demonstrate this by an example. Let say, there’s a class named Father having attributes as:
public class Father {
String fname;
String lname;
int wealth;
}
There’s another class Child which extends Father. Here, we have those attributes which are unique to Child.
public class Child extends Father{
String hobby;
int vehicles;
}
Now, coming to XML file. Define the bean configuration for both the classes. In Child bean, we can define the properties of Father class(as Child extends Father), which are fname, lname, wealth and obviously it’s own properties as well. Notice, there’s a parent attribute in Child bean whose value is same as the id of Father class.
<bean id="father" class="com.spring2.Father">
<property name="fname" value="Rakesh"/>
<property name="lname" value="Sharma"/>
<property name="wealth" value="100000"/>
</bean>
<bean id="child" class="com.spring2.Child" parent="father">
<property name="fname" value="Rohan"/>
<property name="hobby" value="Singing"/>
<property name="vehicles" value="2"/>
</bean>
Now, you can simply fetch the object in main method and display that.
public static void main(String[] args) {
ApplicationContext cxt = new
ClassPathXmlApplicationContext("container.xml");
Child child = cxt.getBean("child", Child.class);
System.out.println("Firstname: "+child.getFname());
System.out.println("Lastname: "+child.getLname());
System.out.println("Wealth: "+child.getWealth());
System.out.println("Hobbies: "+child.getHobby());
System.out.println("Vehicles: "+child.getVehicles());
}
You will get output as:
Firstname: Rohan
Lastname: Sharma
Wealth: 100000
Hobbies: Singing
Vehicles: 2
AutoWiring:
Dependencies can be injected internally by autowiring. It makes the development faster since we need not define the dependency explicitly. We have two ways to do this- Autowiring through XML and @Autowired annotation. I will discuss about annotation way. If one has to see the XML way, visit this.
For this, let’s consider the Person-Address relation we saw above and inject dependency of Address in respective attribute in Person class using autowiring.
Come to Person class. Add the @Autowire annotation on the setter method, constructor or field itself. I have added this on setter method for Address.
public class Person {
//other attributes
Address address;
@Autowired
public void setAddress(Address address) {
this.address = address;
}
//constructor
//other getters and setters
}
In XML file, you need to register AutowiredAnnotationBeanPostProcessor. For registering, just include ” line as shown below in XML file.
<context:annotation-config />
<bean id="prsn" class="com.spring2.Person">
<!--Define properties and values of attributes of Person except the attribute for Address type -->
</bean>
<bean id="address" class="com.spring2.Address">
<!-- Define the properties and values of all attributes of Address -->
</bean>
Now, simply add the code in main method to fetch the Person object, then you will see the Address fields are also initialized as written in bean definition.
For finding more information on Autowiring through annotation, go through the link I provided below. 🙂
http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/