Wednesday 22 February 2017

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,
  <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Osho" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

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

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.
 <bean id="validatorBean" class="com.sample.test.BeansValidator">
  <property name="author1">
   <idref bean="osho" />
  </property>

  <property name="author2">
   <idref bean="paulo" />
  </property>
 </bean>

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
<?xml version="1.0" encoding="UTF-8"?>
<beans 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">


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

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

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

  <property name="author2">
   <idref bean="paulo" />
  </property>
 </bean>

</beans>

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.
<?xml version="1.0" encoding="UTF-8"?>
<beans 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">


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

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

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

  <property name="author2">
   <idref bean="paulo" />
  </property>
 </bean>

</beans>


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

  <property name="author2">
   <idref bean="paulo" />
  </property>
 </bean>


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.<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
 at com.sample.test.HelloWorld.main(HelloWorld.java:8)




Previous                                                 Next                                                 Home

No comments:

Post a Comment