Spring 多个配置文件

– 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 需要创建哪些对象以及如何创建对象。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean class="shangbo.spring.core.example15.OutPutService"/>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean class="shangbo.spring.core.example15.InPutService"/>

</beans>

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

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 标签。这样我们只需要传给容器一个配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
     
    <!-- 
    	import 表示导入一个配置文件
     -->
    <import resource="example2.xml"/>

	<bean class="shangbo.spring.core.example16.OutPutService"/>

</beans>
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;

@Configuration
public 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;

@Configuration
public 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;

@Configuration
public 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());
	}
}

更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-06-17
– Written by ShangBo on 2017-05-21
– End

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值