java 使用 new 关键字创建的 Spring bean (@Component) 中的自动装配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10997092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autowiring in Spring bean (@Component) created with new keyword
提问by abdelhady
I have two spring beans as follows:
我有两个春豆如下:
@Component("A")
@Scope("prototype")
public class A extends TimerTask {
@Autowired
private CampaignDao campaignDao;
@Autowired
private CampaignManager campManger;
A(){
init_A();
}
}
I have to make a new object of A with new keyword, due to a legacy code
由于遗留代码,我必须使用 new 关键字创建 A 的新对象
@Component("B")
@Scope("prototype")
public class B{
public void test(){
A a = new A();
}
}
when Running -> the spring beans in the class A are null, can i create a new instance of the spring bean A and still use autowiring in it ?
当运行 -> 类 A 中的 spring bean 为空时,我可以创建 spring bean A 的新实例并仍然在其中使用自动装配吗?
回答by omnomnom
Yours component "A" is not created by Spring container, thus, dependencies are not injected. However, if you need to support some legacy code (as I understand from your question), you can use @Configurable
annotation and build/compile time weaving:
您的组件“A”不是由 Spring 容器创建的,因此不会注入依赖项。但是,如果您需要支持一些遗留代码(正如我从您的问题中了解到的),您可以使用@Configurable
注释和构建/编译时编织:
@Configurable(autowire = Autowire.BY_TYPE)
public class A extends TimerTask {
// (...)
}
Then, Spring will inject autowired dependencies to component A, no matter if it's instantiated by container itself, or if it's instantiated in some legacy code by new
.
然后,Spring 会将自动装配的依赖项注入到组件 A 中,无论它是由容器本身实例化的,还是由new
.
For example, to set up build-time weaving with maven plugin you have to:
例如,要使用 maven 插件设置构建时编织,您必须:
- Add
<context:spring-configured/>
to the Spring application context - Configure Maven AspectJ plugin:
- 添加
<context:spring-configured/>
到 Spring 应用程序上下文 - 配置Maven AspectJ 插件:
in the build plugins section:
在构建插件部分:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
<encoding>UTF-8</encoding>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<!--
Xlint set to warning alleviate some issues, such as SPR-6819.
Please consider it as optional.
https://jira.springsource.org/browse/SPR-6819
-->
<Xlint>warning</Xlint>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...and the dependencies section:
...以及依赖项部分:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
Please consult Spring reference for more details: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable
有关更多详细信息,请参阅 Spring 参考:http: //static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable
回答by Japan Trivedi
Because you are creating the object of class A yourself using new operator, you are not getting the autowired fields in that object and finding them null. Try to get the bean from spring container.
因为您正在使用 new 运算符自己创建类 A 的对象,所以您不会获得该对象中的自动装配字段并发现它们为空。尝试从 spring 容器中获取 bean。
Hope this helps you. Cheers.
希望这对你有帮助。干杯。