栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第二章IOC控制反转,DI注入分类(byName,byType自动注入,多个spring配置文件)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第二章IOC控制反转,DI注入分类(byName,byType自动注入,多个spring配置文件)

2.3.2 引用类型属性自动注入            对于引用类型属性的注入,也可不在配置文件中显示的注入。可以通过为 标签 设置 autowire 属性值,为引用类型属性进行隐式自动注入( 默认是不自动注入引用类型属 性 )。根据自动注入判断标准的不同,可以分为两种:      byName :根据名称自动注入      byType : 根据类型自动注入 ( 1 ) byName 方式自动注入            当配置文件中被调用者 bean 的 id 值与代码中调用者 bean 类的属性名相同时,可使用 byName 方式,让容器自动将被调用者 bean 注入给调用者 bean 。容器是通过调用者的 bean 类的属性名与配置文件的被调用者 bean 的 id 进行比较而实现自动注入的。

 项目结构

项目展示

1.创建Student实体类

package com.it.bao4;

public class Student {
    private int sno;
    private String sname;
    private int sage;
    private School school;

  public Student(){
      System.out.println("spring调用了无参构造方法");
  }

    public Student(int sno, String sname, int sage, School school) {
        this.sno = sno;
        this.sname = sname;
        this.sage = sage;
        this.school = school;
        System.out.println("spring调用了有参构造方法!");
    }


    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", sname='" + sname + ''' +
                ", sage=" + sage +
                ", school=" + school +
                '}';
    }
}

2.创建School实体类
package com.it.bao4;

public class School {
    private String schName;
    private String schAddress;

    public String getSchName() {
        return schName;
    }

    public void setSchName(String schName) {
        this.schName = schName;
    }

    public String getSchAddress() {
        return schAddress;
    }

    public void setSchAddress(String schAddress) {
        this.schAddress = schAddress;
    }

    @Override
    public String toString() {
        return "School{" +
                "schName='" + schName + ''' +
                ", schAddress='" + schAddress + ''' +
                '}';
    }
}

3.创建applicationContext.xml文件




    
    
        
        
        

    
    
        
        
    
    
        
        
    



4.创建测试文件
package com.it;

import com.it.bao4.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppTest4
{
@Test
    public void test1(){
    String config="bao4/applicationContext.xml";
    ApplicationContext ac= new ClassPathXmlApplicationContext(config);
    //从容器中获取student对象
    Student myStudent = (Student) ac.getBean("myStudent");
    System.out.println("student对象:"+myStudent);
}



}

项目结果展示

( 2 ) byType 方式自动注入           使用 byType 方式自动注入,要求:配置文件中被调用者 bean 的 class 属性指定的类, 要与代码中调用者 bean 类的某引用类型属性类型同源。即要么相同,要么有 is-a 关系(子 类,或是实现类)。但这样的同源的被调用 bean 只能有一个。多于一个,容器就不知该匹配 哪一个了。

 项目结构

同上

项目展示

1.创建Student实体类

同上 2.创建School实体类 同上 3.创建applicationContext.xml文件













    
    
        
        
        
        
    
    
        
        
    










4.创建测试文件
package com.it;

import com.it.bao4.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppTest4
{
@Test
    public void test1(){
    String config="bao4/applicationContext.xml";
    ApplicationContext ac= new ClassPathXmlApplicationContext(config);
    //从容器中获取student对象
    Student myStudent = (Student) ac.getBean("myStudent");
    System.out.println("student对象:"+myStudent);
}



}
项目结果展示

2.3.3 为应用指定多个 Spring 配置文件           在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件变 得非常庞大、臃肿。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将 Spring 配置文件分解成多个配置文件。         包含关系的配置文件:         多个配置文件中有一个总文件,总配置文件将各其它子文件通过 引入。在 Java 代码中只需要使用总配置文件对容器进行初始化即可。 代码:

 Spring 配置文件:

也可使用通配符 * 。但,此时要求父配置文件名不能满足 * 所能匹配的格式,否则将出现 循环递归包含。就本例而言,父配置文件不能匹配 spring-*.xml 的格式,即 不能起名为 spring-total.xml 。

total.xml

 

项目结构

同上

项目展示

1.创建Student实体类

同上 2.创建School实体类 同上 3.创建spring-student.xml文件




    
    
        
        
        

    




4.创建spring-school.xml文件





    
        
        
    
    
        
        
    



5.创建total.xml文件










    




4.创建测试文件 同上 项目结果展示

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938962.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号