008_多配置文件的引入
生活随笔
收集整理的這篇文章主要介紹了
008_多配置文件的引入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 多配置文件
1.1. 在分模塊開發的時候, 我們可能使用多個配置文件。
1.2. 在一個配置文件中引入多個配置文件
1.3. 在類加載的時候加載多個配置文件
2. 多配置文件實例
2.1. 新建一個名為MultiXml的Java工程, 拷入Spring相關包
2.2. 創建Teacher.java
package com.lywgames.bean;public class Teacher {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + "]";}}2.3. 創建Student.java
package com.lywgames.bean;public class Student {private int id;private String name;public Student(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + "]";}}2.4. 創建Clazz.java
package com.lywgames.bean;public class Clazz {private Teacher teacher;private Student student;public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}@Overridepublic String toString() {return "Clazz [teacher=" + teacher + ", student=" + student + "]";}}2.5. 創建Test.java
package com.lywgames;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lywgames.bean.Clazz; import com.lywgames.bean.Student; import com.lywgames.bean.Teacher;public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml", "applicationContext2.xml", "com/lywgames/bean/applicationContext3.xml");System.out.println(context.getBean(Teacher.class).toString());System.out.println(context.getBean(Student.class).toString());System.out.println(context.getBean(Clazz.class).toString());context.close();} }2.6. 在src目錄下創建applicationContext2.xml
<?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 id="teacher" class="com.lywgames.bean.Teacher"><property name="id" value="#{333}"></property><property name="name" value="#{'lvbu'}"></property></bean> </beans>2.7. 在com.lywgames.bean目錄下創建applicationContext3.xml
<?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 id="student" class="com.lywgames.bean.Student"> <constructor-arg name="id" value="101"></constructor-arg><constructor-arg name="name" value="zhangsan"></constructor-arg></bean> </beans>2.8. 在src目錄下創建applicationContext.xml
<?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 id="clazz" class="com.lywgames.bean.Clazz"> <property name="teacher" value="#{teacher}"></property><property name="student" value="#{student}"></property></bean><!-- 導入根路徑下配置文件 --><import resource="applicationContext2.xml"/> <!-- 導入包下配置文件 --><import resource="com/lywgames/bean/applicationContext3.xml"/> </beans>2.9. 運行項目
總結
以上是生活随笔為你收集整理的008_多配置文件的引入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 007_SpEL表达式
- 下一篇: 009_IOC注解开发