问题描述
我想自动装配一个组件(仍然使用@Autowired注释),但不要求它有@Component(或其他类似的注释)。我该怎么做?
I would like to autowire a component (still using the @Autowired annotation), but not require it to have the @Component (or other similar annotations) on it. How would I do this?
public class A {
@Autowired
private class B b;
}
@Component
public class B {
}
为了允许自动装配A类而不需要创建A,这将是很方便的,除非我们需要它(否则通过使用类名的反射来实现) )。
This would be convenient in order to allow autowiring of class A without requiring the creation of A, unless we needed it (in otherwise on the fly by reflection using the class name).
推荐答案
我不确定,如果我正确理解你的问题。但是如果你想通过一些注释或xml定义注入bean B
而不用标记bean A
,你可以使用 SpringBeanAutowiringSupport
I don't sure, If I correctly understood to your question. But if you want inject bean B
without marking bean A
via some annotation, or xml definition, you can use SpringBeanAutowiringSupport
public class A {
@Autowired
private class B b;
public A{
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
}
这篇关于Spring如何在不使用@Component或其他衍生产品的情况下自动装配组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!