簡體   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