使用 new 关键字创建的 Spring bean (@Component) 中的自动装配 [英] Autowiring in Spring bean (@Component) created with new keyword
问题描述
我有两个春豆如下:
@Component("A")
@Scope("prototype")
public class A extends TimerTask {
@Autowired
private CampaignDao campaignDao;
@Autowired
private CampaignManager campManger;
A(){
init_A();
}
}
由于遗留代码,我必须使用 new 关键字创建 A 的新对象
I have to make a new object of A with new keyword, due to a legacy code
@Component("B")
@Scope("prototype")
public class B{
public void test(){
A a = new A();
}
}
运行时 -> 类 A 中的 spring bean 为空,我可以创建 spring bean 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 容器创建的,因此没有注入依赖项.但是,如果您需要支持一些遗留代码(我从您的问题中了解到),您可以使用 @Configurable
注释和构建/编译时编织:
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:
@Configurable(autowire = Autowire.BY_TYPE)
public class A extends TimerTask {
// (...)
}
然后,Spring 会将自动装配的依赖项注入到组件 A 中,无论它是由容器本身实例化的,还是由 new
在某些遗留代码中实例化的.
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
.
例如,要使用 maven 插件设置构建时编织,您必须:
For example, to set up build-time weaving with maven plugin you have to:
- 添加
到Spring应用上下文 - 配置Maven AspectJ插件:
- Add
<context:spring-configured/>
to the Spring application context - Configure Maven AspectJ plugin:
在构建插件部分:
<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>
请参阅 Spring 参考以了解更多详细信息:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable
Please consult Spring reference for more details: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable
这篇关于使用 new 关键字创建的 Spring bean (@Component) 中的自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!