spring dependency checking with @Required Annotation Example

The Main Aim of spring dependency checking is make sure all the properties of spring bean have been set.

If we want to apply dependency checking for particular property then we need to apply @Required Annotation.

How to apply @Required Annotation?

The @Required annotation applies to setter methods of bean property like following.


public class Person{
private String personName;

@Required
public void setPersonName(String personName) {
this.personName = personName;
}
}

In Above we have applied @Required on top of setter method of personName property of Person Bean.That means we must need to set the value of personName property in spring configuration file.



what is mean by @Required Annotation?

If We apply @Required Annotation on top of any setter method of bean property then that property have been must be set in configuration file,through an explicit property value in a bean definition.

what changes need to in spring configuration file(applicationContext.xml)?

1.we need to add to spring configuration file.

2.To Enable the @Required annotation, in spring configuration file we need to register RequiredAnnotationBeanPostProcessor like following



Lets see An Example For Spring dependency checking with @Required Annotation.

The Project Structure:

1.in src/main/java contains all java model classes.

2.src/main/resources contains all configuration files and properties files.

3.src/main/test contains all Test classes.


pom.xml



4.0.0
Test2
Test2
0.0.1-SNAPSHOT


org.springframework
spring-core
3.2.6.RELEASE


org.springframework
spring-beans
3.2.6.RELEASE


org.springframework
spring-context
3.2.6.RELEASE





Person.java


package com.javalschool.dependencycheck;

import org.springframework.beans.factory.annotation.Required;

public class Person {
private int personId;
private String personName;
private Address address;
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
@Required
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 + "]";
}


}


In Above we have applied @Required annotation on top of personName property of Person Bean.so we must need to set the value of personName in spring configuratioin File.


Address.java


package com.javalschool.dependencycheck;

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;
}
@Override
public String toString() {
return "Address [flatNo=" + flatNo + ", street=" + street + "]";
}

}


applicationContext.xml


<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />
&lbean
class="org.springframework.beans.factory.annotation.
RequiredAnnotationBeanPostProcessor" />
<bean id="personBean" class="com.javalschool.dependencycheck.Person">

<property name="personId" value="12345" />
<property name="personName" value="ABCDE" />
<property name="address" ref="addressBean" />
</bean>
<bean id="addressBean" class="com.javalschool.dependencycheck.Address">
<property name="flatNo" value="2-3/45" />
<property name="street" value="xxxxxxx" />
</bean>
</beans>

Create Test class and Run it


package com.javalschool.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javalschool.dependencycheck.Person;

public class Test {

@SuppressWarnings("resource")
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=ABCDE, address=Address [flatNo=2-3/45, street=xxxxxxx]]

No comments:

Post a Comment