Spring客户端不是SpringBean,但需要自动连接的bean字段

Spring客户端不是SpringBean,但需要自动连接的bean字段,spring,dependency-injection,Spring,Dependency Injection,我有一个类希望使用一些@Autowired服务类作为字段,但该类本身不是@service或@Component,因为它需要一个非默认构造函数来正确使用它 是否有注释声明“请扫描这个类以查找@Autowiring,但它本身不是Springbean”?对我来说,这似乎是一个真正的用例(客户端想要自动连接并使用一些bean,但它本身不是Springbean)?您需要手动注入bean并使用context:annotation-config。如果你有如下的课程 public class ABD{ @Au

我有一个类希望使用一些@Autowired服务类作为字段,但该类本身不是@service或@Component,因为它需要一个非默认构造函数来正确使用它


是否有注释声明“请扫描这个类以查找@Autowiring,但它本身不是Springbean”?对我来说,这似乎是一个真正的用例(客户端想要自动连接并使用一些bean,但它本身不是Springbean)?

您需要手动注入bean并使用context:annotation-config。如果你有如下的课程

public class ABD{

@Autowired
public Bean2 b2;    
}

如果您通过构造函数注入将ABD注入到其他bean,@Autowired将应用,否则spring将无法恢复您的bean,因为它没有无参数构造函数

另一方面,如果您的对象不是bean,请参阅上的讨论。代码是这样的

        context.getAutowireCapableBeanFactory().autowireBean(this);

要在非SpringBean中获取上下文,您可能需要使用ContextSingletonBeanFactoryLocator。但是,如果您使用构造函数注入将其冷化为Springbean,那么就容易多了。

您需要手动注入bean并使用context:annotation-config。如果你有如下的课程

public class ABD{

@Autowired
public Bean2 b2;    
}

如果您通过构造函数注入将ABD注入到其他bean,@Autowired将应用,否则spring将无法恢复您的bean,因为它没有无参数构造函数

另一方面,如果您的对象不是bean,请参阅上的讨论。代码是这样的

        context.getAutowireCapableBeanFactory().autowireBean(this);

要在非SpringBean中获取上下文,您可能需要使用ContextSingletonBeanFactoryLocator。但是,如果您使用构造函数注入将其冷化为Springbean,则会容易得多

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
    <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
    <constructor-arg type="int"><value>1</value></constructor-arg>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }
}

1.
公共类ExampleBean{
私家菜豆豆;
私家菜豆豆豆二;
私人互联网i;
公共示例bean(AnotherBean AnotherBean,YetAnotherBean YetAnotherBean,int i){
this.beanOne=另一个bean;
this.beanTwo=yetAnotherBean;
这个。i=i;
}
}

对于必须自动连接的类。有关详细信息,请参阅。

您可以尝试以下方法

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
    <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
    <constructor-arg type="int"><value>1</value></constructor-arg>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }
}

1.
公共类ExampleBean{
私家菜豆豆;
私家菜豆豆豆二;
私人互联网i;
公共示例bean(AnotherBean AnotherBean,YetAnotherBean YetAnotherBean,int i){
this.beanOne=另一个bean;
this.beanTwo=yetAnotherBean;
这个。i=i;
}
}
对于必须自动连接的类。有关更多信息,请参阅