spring_JavaConfig

从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。

interface:

package spring_config;

/**
 * Created by luozhitao on 2017/8/10.
 */
public interface hello0810 {

    public void printMsg(String msg);
}

imp:

package spring_config;

/**
 * Created by luozhitao on 2017/8/10.
 */
public class hello0810imp implements hello0810{
    public void printMsg(String msg) {

        System.out.println("0810"+msg);

    }
}

使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。

package spring_config;

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

/**
 * Created by luozhitao on 2017/8/10.
 */
@Configuration
public class APPconfig {

    @Bean(name = "hellobean")
    public hello0810 hello(){

        return new hello0810imp();
    }
}

main:

package spring_config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by luozhitao on 2017/8/10.
 */
public class APP_main {


    public static void main(String [] args){


        ApplicationContext context=new AnnotationConfigApplicationContext(APPconfig.class);

        hello0810 ho=(hello0810)context.getBean("hellobean");

        ho.printMsg("spring_config");





    }

}

 -----------------------

使用@Import加载多个配置文件。

package com.yiibai.config;

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

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

}

 

posted on 2017-08-10 10:46  猫儿爹  阅读(127)  评论(0编辑  收藏  举报

导航