春天@Required& @Mandatory注释

时间:2014-12-09 15:55:35

标签: java spring ioc-container

这是 BeanClass.java

package com.practice.spring;

import org.springframework.beans.factory.annotation.Required;

import com.apress.springrecipes.sequence.Mandatory;

public class BeanClass {

    private int count;
    private String prefix;

    public BeanClass() {
        System.out.println("Default Constructor");
    }

    public BeanClass(int count, String prefix) {
        this.count = count;
        this.prefix = prefix;
        System.out.println(prefix+count);
    }

    @Required
    public void setCount(int count) {
        this.count = count;
    }

    @Mandatory
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public String toString() {
        return prefix+count;
    }

}

这是 beans.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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />

    <bean id="beanClass1"
          class="com.practice.spring.BeanClass">
        <property name="count" value="1" />
        <property name="prefix" value="Bean" />
    </bean>
    <bean id="beanClass2"
          class="com.practice.spring.BeanClass">
        <constructor-arg value="2" />
        <constructor-arg value="Bean" />
    </bean>
    <bean id="beanClass3"
          class="com.practice.spring.BeanClass">
        <constructor-arg>
            <value>3</value>
        </constructor-arg>
        <constructor-arg>
            <value>Bean</value>
        </constructor-arg>
    </bean>
</beans>

这就是我实例化上下文的方式。

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

问题:

需要计算属性&amp;前缀是强制性的。如果您看到 beanClass2 ,我将通过构造函数设置属性,并在 beanClass3

中设置属性

但是当我执行代码时,它会抛出异常。令我困惑的重要事情是:

  

线程中的异常&#34; main&#34;   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; beanClass2&#39;的bean在类路径资源中定义   [beans.xml]:bean的初始化失败;嵌套异常是   org.springframework.beans.factory.BeanInitializationException:   财产&#39;&#39;&#39; bean是bean&#39; beanClass2&#39;

所必需的

它打印默认构造函数&amp; Bean2。我也在这里粘贴整个堆栈跟踪:

  

2014年12月9日下午8:47:33   org.springframework.context.support.AbstractApplicationContext   prepareRefresh INFO:刷新   org.springframework.context.support.ClassPathXmlApplicationContext@1a71e93:   启动日期[Tue Dec 09 20:47:33 PKT 2014];上下文层次结构的根   2014年12月9日下午8:47:33   org.springframework.beans.factory.xml.XmlBeanDefinitionReader   loadBeanDefinitions INFO:从类路径加载XML bean定义   资源[beans.xml] 2014年12月9日8:47:33 PM   org.springframework.beans.factory.support.DefaultListableBeanFactory   preInstantiateSingletons信息:预先实例化单例   org.springframework.beans.factory.support.DefaultListableBeanFactory@4aed64:   定义bean   [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,beanClass1,beanClass2,beanClass3];   工厂层级的根

     

默认构造函数

     

Bean2

     

2014年12月9日   晚上8:47:33   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry   destroySingletons信息:摧毁单身人士   org.springframework.beans.factory.support.DefaultListableBeanFactory@4aed64:   定义bean   [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,beanClass1,beanClass2,beanClass3];   工厂层次结构的根目录线程中的异常&#34; main&#34;   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; beanClass2&#39;的bean在类路径资源中定义   [beans.xml]:bean的初始化失败;嵌套异常是   org.springframework.beans.factory.BeanInitializationException:   财产&#39;&#39;&#39; bean和beanClass2&#39;是必需的。在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:291)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)     在com.practice.spring.Main.main(Main.java:13)引起:   org.springframework.beans.factory.BeanInitializationException:   财产&#39;&#39;&#39; bean和beanClass2&#39;是必需的。在   org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:149)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)     ......还有11个

1 个答案:

答案 0 :(得分:3)

@Required意味着您使用的是setter注入,而不是构造函数注入。这些设计有两种选择 - 不要同时做两种选择。

请注意Spring的博客 -

  

@ Required允许您指示Spring检查是否需要   对你的依赖。如果您无法使用   构造函数注入,或者出于其他原因,您更喜欢   setter injection,@ Required是要走的路。

Setter Injection vs Constructor Injection

最近,

  

@ Required将方法(通常是JavaBean setter方法)标记为   被“需要”:也就是说,必须将setter方法配置为   依赖注入值。

RequiredAnnotation

因此,您将其标记为必须依赖注入 - 而不是构造函数注入。