使用@Required和@Autowired时创建bean时出错

时间:2014-01-14 13:57:45

标签: java spring

我是春天的新手。我正在尝试在我的代码中使用@Required@Autowired,但它会给我org.springframework.beans.factory.BeanCreationException。以下是我的代码。
1)StudentAuto.java

public class StudentAuto
{

@Autowired
private String name;
@Autowired
private  String city;
public String getCity() {
    return city;
}
@Required
public void setCity(String city) {
    this.city = city;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

2)spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config></context:annotation-config>
<bean id='stu' class='com.bean.StudentAuto' >
</bean>

<bean name='name' class='java.lang.String'>
 <constructor-arg value="nm"></constructor-arg> 
 </bean> 

<bean name='city' class='java.lang.String'>
<constructor-arg value="ci"></constructor-arg>
</bean>
</beans>

3)TestApp.java

public class TestApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
StudentAuto auto=context.getBean("stu", StudentAuto.class);
System.out.println(auto.getCity());
System.out.println(auto.getName());
    }
}

并且错误堆栈跟踪在下面。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stu' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'city' is required for bean 'stu'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
    at com.bean.TestApp.main(TestApp.java:14)
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'city' is required for bean 'stu'
    at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:149)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 7 more

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

@Required州的javadoc

  

将方法(通常是JavaBean setter方法)标记为   'required':即必须将setter方法配置为   依赖注入值。

请注意,带注释的方法不一定是setter,但通常就是这样。

@Required方法由RequiredAnnotationBeanPostProcessor处理,表明

  

这巧妙地将这种检查的责任推到容器上   (它可以说属于),并且消除了(部分)a的需要   开发人员编写一个简单检查所有必需的方法   实际上已经设置了属性。

因此,目的是通过检查容器是否实际调用了该方法来保证设置属性。

典型的模式是

class Foo {
    private String value;
    @Required
    public void setValue(String value) {
        this.value = value;
    }
}

使用bean定义

<bean class="Foo" id="fooBean">
    <property name="value" value="some value"/>
</bean>

如果你没有添加<property>,容器会抱怨并抛出异常,就像你的配置一样

<bean id='stu' class='com.bean.StudentAuto' >
</bean>

此处,容器未使用@Required方法设置属性。由于Field,它直接在@Autowired上使用反射。因此,@Required注释未经过验证。

答案 1 :(得分:0)

<强> 1。 DOC:

  

<强> @Required

     

此注释仅表示必须在配置时通过an explicit property value in a bean definitionautowiring填充受影响的bean属性。


2。请注意:
@Required注释用于验证检查,而不是依赖注入。


第3。一种解决方法:
由于错误日志显示:Property 'city' is required for bean 'stu'。因此,您应该在stu bean中添加一个标签 - 手动注入city

<bean id="stu" class="com.bean.StudentAuto">
    <property name="city" value="London"/>
</bean>