博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 多个配置文件
阅读量:4049 次
发布时间:2019-05-25

本文共 4070 字,大约阅读时间需要 13 分钟。

– Start


如果你有多个配置文件该怎么办呢?很简单,初始化容器时允许我们传入多个文件,下面是一个简单的例子。

首先,定义两个类。

package shangbo.spring.core.example15;public class OutPutService {	public void outPut(String msg) {		System.out.println(msg);	}}
package shangbo.spring.core.example15;public class InPutService {	public String getMessage() {		return "Hello World";	}}

然后定义两个 XML 配置文件,用来告诉 Spring 需要创建哪些对象以及如何创建对象。

最后定义一个客户端测试类。

package shangbo.spring.core.example15;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器,一次读取多个配置文件		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "example1.xml", "example2.xml" }, InPutService.class);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		InPutService input = context.getBean(InPutService.class);		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut(input.getMessage());	}}

除此之外,你还可以使用 import 标签。这样我们只需要传给容器一个配置文件。

package shangbo.spring.core.example16;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器		ApplicationContext context = new ClassPathXmlApplicationContext("example1.xml", InPutService.class);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		InPutService input = context.getBean(InPutService.class);		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut(input.getMessage());	}}

如果使用 Java 配置文件该怎么办呢?

package shangbo.spring.core.example17;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class AppConfig1 {	@Bean	public OutPutService outPutService() {		return new OutPutService();	}}
package shangbo.spring.core.example17;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class AppConfig2 {	@Bean	public InPutService inPutService() {		return new InPutService();	}}
package shangbo.spring.core.example17;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器,一次读取多个Java配置文件		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig1.class, AppConfig2.class);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		InPutService input = context.getBean(InPutService.class);		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut(input.getMessage());	}}

Java 也可以使用 @import 注解。

package shangbo.spring.core.example18;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;// @Import 导入一个配置类@Configuration@Import(value = AppConfig2.class)public class AppConfig1 {	@Bean	public OutPutService outPutService() {		return new OutPutService();	}}
package shangbo.spring.core.example18;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class AppConfig2 {	@Bean	public InPutService inPutService() {		return new InPutService();	}}
package shangbo.spring.core.example18;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig1.class);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		InPutService input = context.getBean(InPutService.class);		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut(input.getMessage());	}}

– 声 明:转载请注明出处
– Last Updated on 2017-06-17
– Written by ShangBo on 2017-05-21
– End

你可能感兴趣的文章
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql truncate (清除表数据)
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt 创建异形窗体
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
wpa_supplicant控制脚本
查看>>
gstreamer相关工具集合
查看>>