本文共 4070 字,大约阅读时间需要 13 分钟。
如果你有多个配置文件该怎么办呢?很简单,初始化容器时允许我们传入多个文件,下面是一个简单的例子。
首先,定义两个类。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