Spring 3 exemple de JavaConfig @Import

Exemple JavaConfig @Import de Spring 3

Normalement, vous serezsplit a large Spring XML bean files dans plusieurs petits fichiers, groupés par module ou par catégorie, pour rendre les choses plus maintenables et modulaires. Par exemple,



    
        

Dans Spring3 JavaConfig, la fonctionnalité équivalente est@Import.

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ CustomerConfig.class, SchedulerConfig.class })
public class AppConfig {

}

Exemple @Import

Voir un exemple complet d'utilisation de JavaConfig@Import.

1. Structure du répertoire

Structure de répertoire de cet exemple.

directory structure of this example

2. Haricots de printemps

Deux haricots de printemps simples.

Fichier: CustomerBo.java

package com.example.core;

public class CustomerBo {

    public void printMsg(String msg) {

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

}

Fichier: SchedulerBo.java

package com.example.core;

public class SchedulerBo {

    public void printMsg(String msg) {

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

}

3. Exemple de @Configuration

Maintenant, utilisez JavaConfig@Configuration pour déclarer les beans ci-dessus.

Fichier: CustomerConfig.java

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.core.CustomerBo;

@Configuration
public class CustomerConfig {

    @Bean(name="customer")
    public CustomerBo customerBo(){

        return new CustomerBo();

    }
}

Fichier: SchedulerConfig.java

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.core.SchedulerBo;

@Configuration
public class SchedulerConfig {

    @Bean(name="scheduler")
    public SchedulerBo suchedulerBo(){

        return new SchedulerBo();

    }

}

4. Exemple @Import

Utilisez@Import pour charger plusieurs fichiers de configuration.

Fichier: AppConfig.java

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ CustomerConfig.class, SchedulerConfig.class })
public class AppConfig {

}

5. Exécuter

Chargez le fichier de configuration principal et testez-le.

package com.example.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.config.AppConfig;

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

        ApplicationContext context = new AnnotationConfigApplicationContext(
                AppConfig.class);

        CustomerBo customer = (CustomerBo) context.getBean("customer");
        customer.printMsg("Hello 1");

        SchedulerBo scheduler = (SchedulerBo) context.getBean("scheduler");
        scheduler.printMsg("Hello 2");

    }
}

Sortie

CustomerBo : Hello 1
SchedulerBo : Hello 2

Télécharger le code source

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