您的组件“A”不是由 Spring 容器创建的,因此不会注入依赖项。但是,如果您需要支持一些遗留代码(正如我从您的问题中了解到的那样),您可以使用@Configurable
注释和构建/编译时编织:
@Configurable(autowire = Autowire.BY_TYPE)
public class A extends TimerTask {
// (...)
}
然后,Spring 将向组件 A 注入自动装配的依赖项,无论它是由容器本身实例化的,还是由new
.
例如,要使用 maven 插件设置构建时编织,您必须:
- 添加
<context:spring-configured/>
到 Spring 应用程序上下文
- 配置Maven AspectJ 插件:
在构建插件部分:
<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>
...和依赖项部分:
<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