首页 > 代码库 > 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;
    }
}
Student实体类
技术分享
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;
    }

}
Grade实体类

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>
子配置文件1
技术分享
<?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>
子配置文件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">

<!-- 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配置文件之间的关系