package com.zyiz.netmon; import org.springframework.beans.factory.annotation.Required; public class Customer { private Person person; private int type; private String action; public Person getPerson() { return person; } @Required public void setPerson(Person person) { this.person = person; } }
<beans ... xmlns:context="http://www.springframework.org/schema/context" ... http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> ... <context:annotation-config /> ... </beans>
完整的实例,
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="CustomerBean" class="com.zyiz.netmon.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
<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-2.5.xsd"> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> <bean id="CustomerBean" class="com.zyiz.netmon.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
org.springframework.beans.factory.BeanInitializationException: Property 'person' is required for bean 'CustomerBean'