我是新来spring.I正在试图利用@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
请帮我解决这个问题。
@Required
状态的Javadoc
将一个方法(通常是JavaBean setter方法)标记为“必需”:即,必须将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
方法来设置属性。Field
由于,它直接使用反射@Autowired
。因此,@Required
注释未通过验证。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句