コンポーネント「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