@Configurableをつけると普通にnewしてるクラスにも依存性を注入できるとな

Springで、普通にnewしてるクラスをDI管理下に入れてしまう方法があるらしい。

以下、githubで見つけたサンプルです。

https://github.com/kenyattaclark/Spring-Configurable-Example

DIコンテナの管理下のクラスをnewしてみる

DI管理下に入れたいクラス
package com.brightdome.sample.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable
public class HelloWorldClient {

@Autowired
private HelloWorldService service;

public void sayHello() {
// Used injected instance of service
service.sayHello();
}
}

@Configurableをつけると、@Autowiredも@Trasactionalとか、Springの恩恵にあやかる。

HelloWorldClientを普通にnewする
package com.brightdome.sample.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorld {

public static void main(String[] args) {
// Initialize Spring Context
new ClassPathXmlApplicationContext("/META-INF/applicationContext.xml" );

// Instantiate class by new'ing it up. i.e., Do not obtain from Spring context
HelloWorldClient client = new HelloWorldClient();
client.sayHello();
}
}

HelloWorldClient側の依存性が解決されます。

application-context.xmlの設定

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    <context:spring-configured/>
    <context:component-scan base-package="com.brightdome"/>
    <context:annotation-config />
</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brightdome</groupId>
<artifactId>spring-configurable-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Configurable Sample</name>
<description>
Sample project to show how to work with Spring's @Configurable capability
to inject dependencies into classes not instantiated by Spring.
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aspectj.version>1.6.12</aspectj.version>
        <spring.version>3.0.7.RELEASE</spring.version>
</properties>

<developers>
<developer>
<id>kclark</id>
<name>Kenyatta Clark</name>
<email>kenyatta.clark@brightdome.com</email>
<url>http://www.brightdome.com</url>
<organization>Bright Dome L.L.C.</organization>
<organizationUrl>http://www.brightdome.com</organizationUrl>
</developer>
</developers>

<licenses>
<license>
<name>Apache 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

コンパイル時にゴニョゴニョしてる。

eclipseにAJDTのプラグインを導入

AspectJ Development Tools (AJDT)

入れないと、eclipseから実行できない。WTPとか。