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

interface:

  1. package spring_config;
  2.  
  3. /**
  4. * Created by luozhitao on 2017/8/10.
  5. */
  6. public interface hello0810 {
  7.  
  8. public void printMsg(String msg);
  9. }

imp:

  1. package spring_config;
  2.  
  3. /**
  4. * Created by luozhitao on 2017/8/10.
  5. */
  6. public class hello0810imp implements hello0810{
  7. public void printMsg(String msg) {
  8.  
  9. System.out.println("0810"+msg);
  10.  
  11. }
  12. }

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

  1. package spring_config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. /**
  7. * Created by luozhitao on 2017/8/10.
  8. */
  9. @Configuration
  10. public class APPconfig {
  11.  
  12. @Bean(name = "hellobean")
  13. public hello0810 hello(){
  14.  
  15. return new hello0810imp();
  16. }
  17. }

main:

  1. package spring_config;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. /**
  7. * Created by luozhitao on 2017/8/10.
  8. */
  9. public class APP_main {
  10.  
  11. public static void main(String [] args){
  12.  
  13. ApplicationContext context=new AnnotationConfigApplicationContext(APPconfig.class);
  14.  
  15. hello0810 ho=(hello0810)context.getBean("hellobean");
  16.  
  17. ho.printMsg("spring_config");
  18.  
  19. }
  20.  
  21. }

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

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

  1. package com.yiibai.config;
  2.  
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Import;
  5.  
  6. @Configuration
  7. @Import({ CustomerConfig.class, SchedulerConfig.class })
  8. public class AppConfig {
  9.  
  10. }

spring_JavaConfig的更多相关文章

随机推荐

  1. discuz对PHP7不支持mysql的兼容性处理

    PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql,discuz对原生mysql函数做了如下处理,通过mysqli代替原mysql函数 http://blog ...

  2. Spring_使用 JdbcTemplate和JdbcDaoSupport

  3. 关于阿里云Symantec免费DV证书部署HTTPS

    获取阿里云Symantec免费DV证书: 官方文件说明: 证书文件214188487290026.pem,包含两段内容,请不要删除任何一段内容. 如果是证书系统创建的CSR,还包含:证书私钥文件214 ...

  4. Python基础笔记系列四:工具的安装与配置

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 一开始是没有打算写工具这篇的,后来发现在某些情况下会遇到一些奇怪的问题,这 ...

  5. BZOJ 1185 [HNOI2007]最小矩形覆盖:凸包 + 旋转卡壳

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 题意: 给出二维平面上的n个点,问你将所有点覆盖的最小矩形面积. 题解: 先找出凸 ...

  6. 在oracle中插入数据报错:ORA-00984列在此处不允许

    这里报错的原因就是当数据类型varchar2时没有使用单引号. 没写单引号,不管是双引号还是什么都没写都会报这个错误.

  7. DatePicker日期与时间控件

    DatePicker日期与时间控件 一.简介 二.方法 最日常的使用方法了 日期控件DatePicker 时间控件TimePicker 月份从0开始 三.代码实例 效果图: 代码: fry.Activ ...

  8. 绝对布局absoluteLayout

    绝对布局absoluteLayout 一.简介 二.实例 绝对布局我们是指定的横纵坐标,所以可以这样直接拖 绝对布局实际中用的少

  9. selenium学习笔记(selenium IDE下载安装)

    今天自己一直在瞎捣鼓 最后这里整理下 selenium IDE 这个录制工具的下载安装 首先这个工具只支持火狐浏览器firefox.使用火狐浏览器进入selenium官网: http://www.se ...

  10. Jexus部署Asp.Net Core项目

    在之前的我的博客项目中,我将.net Core发布到Cent OS 上,使用的Nginx代理以及Supervisor进程守护,看过我的博客的童鞋,也会发现,这种方式比较麻烦,光命令行就看的头大,总共部 ...