AnnotationConfigApplicationContext を使用して Spring コンテナーをインスタンス化する
次のセクションでは、Spring 3.0 で導入された Spring の AnnotationConfigApplicationContext について説明します。この汎用性の高い ApplicationContext 実装は、@Configuration クラスだけでなく、単純な @Component クラスや JSR-330 メタデータでアノテーションが付けられたクラスも受け入れることができます。
@Configuration クラスが入力として提供される場合、@Configuration クラス自体が Bean 定義として登録され、クラス内で宣言されたすべての @Bean メソッドも Bean 定義として登録されます。
@Component および JSR-330 クラスが提供されると、それらは Bean 定義として登録され、@Autowired または @Inject などの DI メタデータが必要に応じてそれらのクラス内で使用されると想定されます。
シンプルな構造
ClassPathXmlApplicationContext のインスタンス化時に Spring XML ファイルが入力として使用されるのとほぼ同じ方法で、AnnotationConfigApplicationContext のインスタンス化時に @Configuration クラスを入力として使用できます。これにより、次の例に示すように、Spring コンテナーを完全に XML なしで使用できます。
Java
Kotlin
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}import org.springframework.beans.factory.getBean
fun main() {
val ctx = AnnotationConfigApplicationContext(AppConfig::class.java)
val myService = ctx.getBean<MyService>()
myService.doStuff()
} 前述のように、AnnotationConfigApplicationContext は @Configuration クラスでのみ機能することに限定されません。次の例に示すように、@Component または JSR-330 アノテーション付きクラスは、コンストラクターへの入力として提供できます。
Java
Kotlin
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}import org.springframework.beans.factory.getBean
fun main() {
val ctx = AnnotationConfigApplicationContext(MyServiceImpl::class.java, Dependency1::class.java, Dependency2::class.java)
val myService = ctx.getBean<MyService>()
myService.doStuff()
} 上記の例では、MyServiceImpl、Dependency1、Dependency2 が @Autowired などの Spring 依存性注入アノテーションを使用することを前提としています。
register(Class<?>…) を使用してプログラムでコンテナーを構築する
引数なしのコンストラクターを使用して AnnotationConfigApplicationContext をインスタンス化し、register() メソッドを使用して構成することができます。このアプローチは、AnnotationConfigApplicationContext をプログラムで構築するときに特に役立ちます。次の例は、その方法を示しています。
Java
Kotlin
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}import org.springframework.beans.factory.getBean
fun main() {
val ctx = AnnotationConfigApplicationContext()
ctx.register(AppConfig::class.java, OtherConfig::class.java)
ctx.register(AdditionalConfig::class.java)
ctx.refresh()
val myService = ctx.getBean<MyService>()
myService.doStuff()
}scan(String…) を使用したコンポーネントスキャンの有効化
コンポーネントスキャンを有効にするには、次のように @Configuration クラスにアノテーションを付けます。
Java
Kotlin
@Configuration
@ComponentScan(basePackages = "com.acme") (1)
public class AppConfig {
// ...
}| 1 | このアノテーションにより、コンポーネントのスキャンが可能になります。 |
@Configuration
@ComponentScan(basePackages = ["com.acme"]) (1)
class AppConfig {
// ...
}| 1 | このアノテーションにより、コンポーネントのスキャンが可能になります。 |
経験豊富な Spring ユーザーは、次の例に示すように、Spring の |
前の例では、com.acme パッケージをスキャンして @Component アノテーション付きクラスを探し、それらのクラスを Spring Bean 定義としてコンテナー内に登録します。次の例に示すように、AnnotationConfigApplicationContext は scan(String…) メソッドを公開して、同じコンポーネントスキャン機能を可能にします。
Java
Kotlin
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.acme");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
}fun main() {
val ctx = AnnotationConfigApplicationContext()
ctx.scan("com.acme")
ctx.refresh()
val myService = ctx.getBean<MyService>()
}@Configuration クラスには @Component というメタアノテーションが付けられているため、コンポーネントスキャンの候補となることに注意してください。前の例では、AppConfig が com.acme パッケージ (またはそのパッケージ) 内で宣言されていると仮定すると、scan() の呼び出し中に取得されます。refresh() では、そのすべての @Bean メソッドが処理され、コンテナー内の Bean 定義として登録されます。 |
AnnotationConfigWebApplicationContext を使用した Web アプリケーションのサポート
AnnotationConfigApplicationContext の WebApplicationContext バリアントは AnnotationConfigWebApplicationContext で使用できます。この実装は、Spring ContextLoaderListener サーブレットリスナー、Spring MVC DispatcherServlet などを構成するときに使用できます。次の web.xml スニペットは、典型的な Spring MVC Web アプリケーションを構成します (contextClass context-param および init-param の使用に注意してください)。
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app> プログラムによるユースケースでは、AnnotationConfigWebApplicationContext の代わりに GenericWebApplicationContext を使用できます。詳細については、GenericWebApplicationContext javadoc を参照してください。 |