首页 > 代码库 > spring05配置文件之间的关系
spring05配置文件之间的关系
一:配置文件包含关系
1.创建对应的实体类
public class Student { //学生实体类 private String name; //姓名 private Integer age; //年龄 private Grade grade; //年级 @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]"; } public Student() { super(); } public Student(String name, Integer age, Grade grade) { super(); this.name = name; this.age = age; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } }
public class Grade { //年级实体类 private String name; //年级名称 @Override public String toString() { return "Grade [name=" + name + "]"; } public Grade() { super(); } public Grade(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.创建配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--年级的Bean --> <bean id="grade" class="cn.bdqn.bean.Grade" p:name="1年级"/> </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" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--学生的Bean 属性grade 在这个容器中没有对应的bean --> <bean id="student" class="cn.bdqn.bean.Student" p:name="小马哥" p:age="50" p:grade-ref="grade"/> </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" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 01.把其他的子文件包含进来 <import resource="spring-grade.xml"/> <import resource="spring-student.xml"/> --> <!-- 02.把其他的子文件包含进来 当前的这个主的配置文件不能命名成spring-* 这种格式 --> <import resource="spring-*.xml"/> </beans>
3.创建对应的测试类
public class StudentTest {
//配置文件的包含关系
@Test
public void test01(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=(Student) context.getBean("student");
System.out.println("student信息:"+student);
}
}
效果图如下
二:配置文件平级关系
1.删除上面练习中的总配置文件,只剩下两个平级的xml文件
2.书写测试类
public class StudentTest { //01.配置文件的平级关系 @Test public void test01(){ //推荐使用 保证配置文件名称格式统一 ApplicationContext context= new ClassPathXmlApplicationContext("spring-*.xml"); Student student=(Student) context.getBean("student"); System.out.println("student信息:"+student); } //02.配置文件的平级关系 @Test public void test02(){ ApplicationContext context= new ClassPathXmlApplicationContext("spring-student.xml","spring-grade.xml"); Student student=(Student) context.getBean("student"); System.out.println("student信息:"+student); } //03.配置文件的平级关系 @Test public void test03(){ String resource1="spring-student.xml"; String resource2="spring-grade.xml"; String [] resources={resource1,resource2}; ApplicationContext context= new ClassPathXmlApplicationContext(resources); Student student=(Student) context.getBean("student"); System.out.println("student信息:"+student); } }
spring05配置文件之间的关系
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。