package com.h3.common; 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.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="personbean" class="com.h3.common.person"> <property name="name" value="h3" /> <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.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="personbean" class="com.h3.common.person"> <property name="name" value="h3" /> <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'