通过使用 'idref' 标记,您可以在部署时验证所引用的命名 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,Spring 会在部署时验证 id 为 osho 和 Paulo 的 bean。如果在配置文件中没有找到任何 bean,那么 spring 会抛出 BeanDefinitionStoreException。
<bean id="validatorBean" class="com.sample.test.BeansValidator">
<property name="author1">
<idref bean="osho" />
</property>
<property name="author2">
<idref bean="paulo" />
</property>
</bean>
以下是完整的工作应用程序。
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;
}
}
我的配置.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();
}
}
现在更新 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="Krishna" />
</property>
<property name="author2">
<idref bean="paulo" />
</property>
</bean>
</beans>
当您看到配置文件时,validatorBean 检查 id 为“Krishna”的 bean。
<bean id="validatorBean" class="com.sample.test.BeansValidator">
<property name="author1">
<idref bean="Krishna" />
</property>
<property name="author2">
<idref bean="paulo" />
</property>
</bean>
由于 id 为“Krishna”的 bean 不存在,您最终会出现以下错误。
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)