码迷,mamicode.com
首页 > 编程语言 > 详细

spring_JavaConfig

时间:2017-08-10 11:51:52      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:ann   blog   static   tao   --   conf   new   lob   bsp   

从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 {

}

 

spring_JavaConfig

标签:ann   blog   static   tao   --   conf   new   lob   bsp   

原文地址:http://www.cnblogs.com/luo-mao/p/7338009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!