Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring: idref: Validate bean existance

By using ‘idref’ tag, you can validate at deployment time that the referenced, named bean actually exists or not.

For Example,
  id="osho" class="com.sample.pojo.Author">
name="firstName" value="Osho" />
name="lastName" value="Jain" />
name="dateOfBirth" value="11 December 1931" />
name="country" value="India" />


id="paulo" class="com.sample.pojo.Author">
name="firstName" value="Paulo" />
name="lastName" value="Coelho" />
name="dateOfBirth" value="24 August 1947" />
name="country" value="India" />


If you define your validator bean like below, Spring validates the beans with ids osho and Paulo at deployment time. If any bean not found in the configuration file, then spring throws BeanDefinitionStoreException.
 id="validatorBean" class="com.sample.test.BeansValidator">
name="author1">
bean="osho" />


name="author2">
bean="paulo" />



Following is the complete working application.


Author.java
package com.sample.pojo;

public class Author {
private String firstName;
private String lastName;
private String dateOfBirth;
private String country;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
.append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
return builder.toString();
}

}

BeansValidator.java
package com.sample.test;

public class BeansValidator {
private String author1;
private String author2;

public String getAuthor1() {
return author1;
}

public void setAuthor1(String author1) {
this.author1 = author1;
}

public String getAuthor2() {
return author2;
}

public void setAuthor2(String author2) {
this.author2 = author2;
}

}

myConfiguration.xml

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


id="osho" class="com.sample.pojo.Author">
name="firstName" value="Osho" />
name="lastName" value="Jain" />
name="dateOfBirth" value="11 December 1931" />
name="country" value="India" />


id="paulo" class="com.sample.pojo.Author">
name="firstName" value="Paulo" />
name="lastName" value="Coelho" />
name="dateOfBirth" value="24 August 1947" />
name="country" value="India" />


id="validatorBean" class="com.sample.test.BeansValidator">
name="author1">
bean="osho" />


name="author2">
bean="paulo" />





HelloWorld.java
package com.sample.test;

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

public class HelloWorld {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

((ClassPathXmlApplicationContext) context).close();
}
}

Run HelloWorld.java, you don’t get any exceptions.


Now update myConfiguration.xml like below.

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


id="osho" class="com.sample.pojo.Author">
name="firstName" value="Osho" />
name="lastName" value="Jain" />
name="dateOfBirth" value="11 December 1931" />
name="country" value="India" />


id="paulo" class="com.sample.pojo.Author">
name="firstName" value="Paulo" />
name="lastName" value="Coelho" />
name="dateOfBirth" value="24 August 1947" />
name="country" value="India" />


id="validatorBean" class="com.sample.test.BeansValidator">
name="author1">
bean="Krishna" />


name="author2">
bean="paulo" />






As you see the configuration file, validatorBean check for the bean with id ‘Krishna’.
id="validatorBean" class="com.sample.test.BeansValidator">
name="author1">
bean="Krishna" />


name="author2">
bean="paulo" />




Since the bean with id ‘Krishna’ don’t exists, you will end up in following error.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validatorBean' defined in class path resource [myConfiguration.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'Krishna' in bean reference for bean property 'author1'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)
at com.sample.test.HelloWorld.main(HelloWorld.java:8)

Reference
http://stackoverflow.com/questions/14607142/spring-idref-usage



Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring: idref: Validate bean existance

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×