Spring 3 exemple JavaConfig

Exemple JavaConfig de Spring 3

Depuis Spring 3, les fonctionnalités deJavaConfig sont incluses dans le module Spring principal, cela permet au développeur de déplacer la définition du bean et la configuration Spring du fichier XML vers la classe Java.

Mais, vous êtes toujours autorisé à utiliser la méthode XML classique pour définir les beans et la configuration, leJavaConfig n'est qu'une autre solution alternative.

Voir la différence entre la définition XML classique et JavaConfig pour définir un bean dans le conteneur Spring.

Fichier XML Spring:



    

Configuration équivalente dans JavaConfig:

package com.example.config;

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

@Configuration
public class AppConfig {

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

}

Spring JavaConfig Hello World

Maintenant, voyez un exemple complet de JavaConfig Spring.

1. Structure du répertoire

Voir la structure du répertoire de cet exemple.

directory structure of this example

2. Bibliothèque de dépendances

Pour utiliser JavaConfig (@Configuration), vous devez inclure la bibliothèqueCGLIB. Voir les dépendances:

    
    
        org.springframework
        spring-core
        ${spring.version}
    

    
        org.springframework
        spring-context
        ${spring.version}
    

    
    
        cglib
        cglib
        2.2.2
    

3. Haricot de printemps

Un haricot simple.

package com.example.hello;

public interface HelloWorld {

    void printHelloWorld(String msg);

}
package com.example.hello.impl;

import com.example.hello.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

    @Override
    public void printHelloWorld(String msg) {

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

}

4. Annotation JavaConfig

Annotez avec@Configuration pour indiquer à Spring qu'il s'agit du fichier de configuration Spring principal, et définissez le bean via@Bean.

package com.example.config;

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

@Configuration
public class AppConfig {

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

}

5. Exécuter

Chargez votre classe JavaConfig avecAnnotationConfigApplicationContext.

package com.example.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.config.AppConfig;
import com.example.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");

    }
}

Sortie

Hello : Spring3 Java Config

Télécharger le code source

Téléchargez-le -Spring3-JavaConfig-Example.zip (6 Ko)