gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:19:50 25 4
gpt4 key购买 nike

我是 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

请帮助我解决这个问题。

最佳答案

@Required 的 javadoc州

Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.

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

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

This neatly pushes responsibility for such checking onto the container (where it arguably belongs), and obviates the need (in part) for a developer to code a method that simply checks that all required properties have actually been set.

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

典型的模式是

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注释未经过验证。

关于java - 使用@Required和@Autowired创建bean时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21115354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com