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

飞行机

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

请帮我解决这个问题。

Sotirios Delimanolis

@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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

创建bean时出错

来自分类Dev

创建bean时出错

来自分类Dev

创建Spring bean时出错

来自分类Dev

在春季创建bean时出错

来自分类Dev

创建“服务” Bean时出错

来自分类Dev

创建 AnnotationActionEndpointMapping bean 时出错

来自分类Dev

JHipster和mongodb:创建名称为“ mongobee”的bean时出错

来自分类Dev

尝试学习Spring:BeanCreationException:使用名称创建bean时出错

来自分类Dev

使用 Redis 创建名为“enableRedisKeyspaceNotificationsInitializer”的 bean 时出错

来自分类Dev

在springboot中使用@Autowired时出错

来自分类Dev

创建名称为'conversionService'的bean时出错

来自分类Dev

创建名称为defaultServletHandlerMapping的bean时出错

来自分类Dev

创建名称为“ springSecurityFilterChain”的bean时出错

来自分类Dev

创建名称为'defaultServletHandlerMapping的bean时出错

来自分类Dev

Spring Lemon BeanCurrentlyInCreationException:创建bean时出错

来自分类Dev

创建名称为'requestMappingHandlerAdapter'的bean时出错

来自分类Dev

创建名称为“ enableRedisKeyspaceNotificationsInitializer”的bean时出错

来自分类Dev

创建名称为'bookMasterController'的bean时出错。

来自分类Dev

创建名称为AuthenticationManager的bean时出错

来自分类Dev

在Spring中创建bean时出错

来自分类Dev

创建名称为“ dao”的bean时出错

来自分类Dev

创建名为“sessionFactory”的 bean 时出错

来自分类Dev

创建 bean 名称“springApplication”时出错

来自分类Dev

使用spring-boot-admin-server时创建名为`conversionServicePostProcessor`的bean时出错

来自分类Dev

Spring Security:创建bean时出错/未定义bean

来自分类Dev

Spring-boot和spring-data-jpa Mysql:创建名称为'entityManagerFactory'的bean时出错

来自分类Dev

使用@Autowired注解注入Bean时出现空指针异常

来自分类Dev

在spring-boot中使用kotlin暂停功能创建名称为'requestMappingHandlerMapping'的bean时出错

来自分类Dev

创建在类路径资源中定义的名称为“ tilesConfigurer”的bean时出错[使用shopizer的知识]

Related 相关文章

  1. 1

    创建bean时出错

  2. 2

    创建bean时出错

  3. 3

    创建Spring bean时出错

  4. 4

    在春季创建bean时出错

  5. 5

    创建“服务” Bean时出错

  6. 6

    创建 AnnotationActionEndpointMapping bean 时出错

  7. 7

    JHipster和mongodb:创建名称为“ mongobee”的bean时出错

  8. 8

    尝试学习Spring:BeanCreationException:使用名称创建bean时出错

  9. 9

    使用 Redis 创建名为“enableRedisKeyspaceNotificationsInitializer”的 bean 时出错

  10. 10

    在springboot中使用@Autowired时出错

  11. 11

    创建名称为'conversionService'的bean时出错

  12. 12

    创建名称为defaultServletHandlerMapping的bean时出错

  13. 13

    创建名称为“ springSecurityFilterChain”的bean时出错

  14. 14

    创建名称为'defaultServletHandlerMapping的bean时出错

  15. 15

    Spring Lemon BeanCurrentlyInCreationException:创建bean时出错

  16. 16

    创建名称为'requestMappingHandlerAdapter'的bean时出错

  17. 17

    创建名称为“ enableRedisKeyspaceNotificationsInitializer”的bean时出错

  18. 18

    创建名称为'bookMasterController'的bean时出错。

  19. 19

    创建名称为AuthenticationManager的bean时出错

  20. 20

    在Spring中创建bean时出错

  21. 21

    创建名称为“ dao”的bean时出错

  22. 22

    创建名为“sessionFactory”的 bean 时出错

  23. 23

    创建 bean 名称“springApplication”时出错

  24. 24

    使用spring-boot-admin-server时创建名为`conversionServicePostProcessor`的bean时出错

  25. 25

    Spring Security:创建bean时出错/未定义bean

  26. 26

    Spring-boot和spring-data-jpa Mysql:创建名称为'entityManagerFactory'的bean时出错

  27. 27

    使用@Autowired注解注入Bean时出现空指针异常

  28. 28

    在spring-boot中使用kotlin暂停功能创建名称为'requestMappingHandlerMapping'的bean时出错

  29. 29

    创建在类路径资源中定义的名称为“ tilesConfigurer”的bean时出错[使用shopizer的知识]

热门标签

归档