Spring 3の

JavaConfig

機能はコアのSpringモジュールに含まれているため、開発者はXMLファイルからBean定義とSpring設定をJavaクラスに移動できます。

しかし、古典的なXML方法を使ってBeanと設定を定義することはできますが、

JavaConfig

はもう一つの代替ソリューションです。

古典的なXML定義とJavaConfigの違いを見て、SpringコンテナでBeanを定義してください。


Spring XMLファイル:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="helloBean" class="com.mkyong.hello.impl.HelloWorldImpl">

</beans>

JavaConfigの

Equivalent設定:

package com.mkyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mkyong.hello.HelloWorld;
import com.mkyong.hello.impl.HelloWorldImpl;

@Configuration
public class AppConfig {

    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }

}

Spring JavaConfig Hello World

今度は、SpringのJavaConfigの完全な例を参照してください。

1.ディレクトリ構造

この例のディレクトリ構造を参照してください。


この例のディレクトリ構造、title = "spring-javaconfig-folder"、width = 554、height = 377

2.依存関係ライブラリ

JavaConfig(

@ Configuration

)を使用するには、

CGLIB

ライブラリをインクルードする必要があります。依存関係を参照してください:

    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- JavaConfig need this library -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

3.春の豆

単純なbean。

package com.mkyong.hello;

public interface HelloWorld {

    void printHelloWorld(String msg);

}

package com.mkyong.hello.impl;

import com.mkyong.hello.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

    @Override
    public void printHelloWorld(String msg) {

        System.out.println("Hello : " + msg);
    }

}

4. JavaConfigアノテーション


@Configuration`を注釈してSpringにコアSpring設定ファイルであることを通知し、

@ Bean`を介してBeanを定義します。

package com.mkyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mkyong.hello.HelloWorld;
import com.mkyong.hello.impl.HelloWorldImpl;

@Configuration
public class AppConfig {

    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }

}

5.それを実行する

JavaConfigクラスに `AnnotationConfigApplicationContext`をロードします。

package com.mkyong.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.mkyong.config.AppConfig;
import com.mkyong.hello.HelloWorld;

public class App {
    public static void main(String[]args) {

            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        HelloWorld obj = (HelloWorld) context.getBean("helloBean");

        obj.printHelloWorld("Spring3 Java Config");

    }
}


出力

Hello : Spring3 Java Config

ソースコードをダウンロードする

ダウンロードする – リンク://wp-content/uploads/2011/06/Spring3-JavaConfig-Example.zip[Spring3-JavaConfig-Example.zip](6 KB)