1. 程式人生 > >Mystring05 配置文件之間的關系

Mystring05 配置文件之間的關系

pan www. this can @value 測試 sys spa type

一:配置文件包含關系

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);
    }
    
}
技術分享

註解配置!引入需要的aop.jar

技術分享
/**
 * 學生類
 */
@Component("student")
public class Student {
    @Value("999")
    private Integer  age;
    @Value("小黑")
    private String  name;
    /**
     * 01.
     * @Autowired:
     * 默認是按照byType 類型匹配
     * @Autowired
       @Qualifier("grades")  按照byName進行匹配
       
       02.
       @Resource
           默認是按照byType 類型匹配
       @Resource(name="grades")
     */
    private  Grade grade;
    
    public Student(Integer ages, String names, Grade grades) {
        this.age = ages;
        this.name = names;
        this.grade = grades;
    }
    
    public Student(Integer age, String name) {
        this.age = age;
        this.name = name;
    }
    
    public Student() {
        
    }
    
    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + ", grade=" + grade
                + "]";
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Grade getGrade() {
        return grade;
    }
    public void setGrade(Grade grade) {
        this.grade = grade;
    }
    
    
    
}
技術分享

技術分享
/**
 * 年級類
 */
@Component("grades")
public class Grade {
    @Value("9年級")
    private  String  name;  //年級名稱

    public String getName() {
        return name;
    }

    //DI  依賴註入 
    public void setName(String name) {
        this.name = name;
    }

    public Grade(String name) {
        super();
        this.name = name;
    }

    public Grade() {
        super();
    }

    @Override
    public String toString() {
        return "Grade [name=" + name + "]";
    }
    
    
    
    

}
技術分享

技術分享
<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
        
     <!--  查詢所有註解的類  -->
      <context:component-scan base-package="cn.bdqn"/><!--掃描本包和其子包下面的所有文件  -->
      <!--<context:component-scan base-package="cn.bdqn.*"/>掃描子包下面的所有文件  -->
</beans>
技術分享

測試類

技術分享
public class StudentTest {
    
    ApplicationContext context=null;
    @Before
    public  void before(){
        context=new  ClassPathXmlApplicationContext("applicationContext.xml");
    }
    
    
    
    @Test
    public  void  test01(){
        Student student=(Student) context.getBean("student");
        System.out.println(student);
    }

}

Mystring05 配置文件之間的關系