Spring Dependency Checking

Spring Dependency Checking

The Main Aim of dependency checking in spring to make sure the required properties have been set or injected.

To define the spring dependency check In Spring Configuration File We need to declare one attribue "dependency-check".

In Spring dependency checking works only if a property has been injected through the Setter Method.

dependency check values:

  1. none
  2. simple
  3. objects
  4. all

none:

it is default depency check mode.No Dependency checking.

simple:

Dependency checking is applied on primitive types(int,float,double..) and collection types(list,set,map..). if any properties of primitive type(int,long,double...) and collections(list,set,map...) have not been set,UnsatisfiedDependencyException will be thrown.

objects:

Dependency checking applied on obejct(secondary) types. If any properties of Object(Secondary) type have not been set,UnsatisfiedDependencyException will be thrown.

all:

If any properties either primitive types or object types have not been set,UnsatisfiedDependencyException will be thrown. to make all primitives and secondary both are mandatory.

Lets see an example For Spring Dependency checking.

Person.java


package com.javalschool.dependencyExample;

public class Person {
private String personId;
private String personName;
private Address address;
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [personId=" + personId + ", personName=" + personName
+ ", address=" + address + "]";
}

}

Address.java


package com.javalschool.dependencyExample;

public class Address {
private String flatNo;
private String street;
public String getFlatNo() {
return flatNo;
}
public void setFlatNo(String flatNo) {
this.flatNo = flatNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}



}


dependency checking:none

The following code for when the dependency checking is none.In this mode we no need to call any setter methods.


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id = "personBean" class = "com.javalschool.dependencyExample.Person" dependency-check="none">

<property name = "personId" value= "12345"/>
<property name = "personName" value = "AAAAAA"/>
</bean>
</beans>

Test.java


package com.javalschool.dependencyExample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean("personBean");
System.out.println(person.toString());


}

}


output:


log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Person [personId=12345, personName=AAAAAA, address=null]

dependency checking:simple

The Following code for when the dependency checking is simple. In This Mode We must need to call primitive and collection setter methods.For Dependency checking simple mode we need to "dependency-check= simple" attribute to bean tag.

In above application we need to change like Following.


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "personBean" class = "com.javalschool.dependencyExample.Person" dependency-check="simple">

<property name = "personId" value= "12345"/>
<property name = "personName" value = "AAAAAA"/>
</bean>
</beans>

output:


log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Person [personId=12345, personName=AAAAAA, address=null]


Note: In above if we not call any primitive setter method we will get following Exception

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personBean' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through bean property 'personId': Set this property value or disable dependency checking for this bean. will be thrown.


dependency checking:objects

The Following code for when the dependency checking id objects..In This Mode We must need to call object type setter methods.


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "personBean" class = "com.javalschool.dependencyExample.Person" dependency-check="objects">
<!-- we need to must call Object type setter methods -->
<property name = "address" ref = "addressBean"/>
</bean>
<bean id = "addressBean" class = "com.javalschool.dependencyExample.Address">
<property name = "flatNo" value = "2-3/45"/>
<property name = "street" value = "xxxxxxx"/>
</bean>
</beans>

Output:


log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Person [personId=null, personName=null, address=Address [flatNo=2-3/45, street=xxxxxxx]]


Note:If We not call any objct type setter method we will get UnsatisfiedDependencyException

-->