从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的更多相关文章

随机推荐

  1. ASP.NET CORE MVC 2.0 发布到IIS 配置问题

    装完.NET CORE 2.0和IIS , 配置好网站, 报500.19 配置文件错误. 解决方法: 1) 安装.NET Core Windows Server Hosting  :  https:/ ...

  2. Spring AOP(4)

  3. geoserver源码maven编译相关问题

    1.登陆失败跳转404错误 登陆失败后指向的路径为: http://192.168.15.97:8080/hgisserver/web/wicket/bookmarkable/org.geoserve ...

  4. Pandas重建索引

    重新索引会更改DataFrame的行标签和列标签.重新索引意味着符合数据以匹配特定轴上的一组给定的标签. 可以通过索引来实现多个操作 - 重新排序现有数据以匹配一组新的标签. 在没有标签数据的标签位置 ...

  5. js中object的copy

    一.场景 在js中一个对象(Object)或者是一个数组(Array)在复制的过程中往往不是特别的简单,一般情况下我们会直接将其赋值给另外一个变量名下,就像这样: var a = [1,2,3]; v ...

  6. mysql 修改编码格式

    下载了mysql的客户端,一般其默认的编码格式是gbk,为了方便后续使用,想要将其编码格式改为utf8. 这时候的方法是: 1.进入mysql的安装目录,找到my.ini文件. 2.以txt文件的格式 ...

  7. ArcGIS API For Silverlight使用在线地图的多种方法总结

    引自:http://www.cnblogs.com/meimao5211/p/3283969.html ArcGIS API For Silverlight使用在线地图的多种方法总结 本人也正在学习A ...

  8. 处理跨线程操作问题(使用Action和delegate或lambda表达式)

    方法A: Action f = () =>                    {                       txtProcess.Text = "开始更新程序.. ...

  9. Tiny210用户手册笔记

                     核心板 CPU 处理器:  Samsung S5PV210,基于 CortexTM-A8,运行主频 1GHz                       内置 P ...

  10. LeetCode OJ :Remove Linked List Elements (移除链表元素)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...