繁体   English   中英

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

[英]Error in creation of bean while using @Required and @Autowired

我是spring的新手。我正在尝试在我的代码中使用@Required@Autowired ,但它给了我org.springframework.beans.factory.BeanCreationException .Below是我的代码。
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

请帮我解决这个问题。

@Required的javadoc说明

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

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

@Required方法由处理RequiredAnnotationBeanPostProcessor其中指出

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

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

典型的模式是

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方法来设置属性。 由于@Autowired它直接在Field上使用反射。 因此,未验证@Required注释。

DOC:

@需要

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


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


3.一种修复方法:
如错误日志所示: Property 'city' is required for bean 'stu' 所以,你应该在stu bean中添加一个propery标签 - 手动注入city

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM