Spring5总述(三)—— IOC操作Bean管理(Xml注入其他类型属性)

文章目录

  • 1、注入属性—外部Bean
  • 2、注入属性—内部Bean
  • 3、注入属性—级联赋值
  • 4、注入属性—集合属性
  • 5、提取集合注入部分

了解更多关于Spring的基础知识

1、注入属性—外部Bean

实现步骤:

  1. 创建两个类Service和Dao,在Service类中以Dao作为属性
public class Dao {
    
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void print(){
        System.out.println("my name is "+name);
    }
}
public class Service {
	//以Dao类型创建属性
    private Dao dao;

    public void setDao(Dao dao) {
        this.dao = dao;
    }
    
    public void printf(){
        dao.print();
    }
}
  1. 在Spring配置文件中配置对象创建,配置属性注入
    <bean id="service" class="Test_bean.Service">
        
        <property name="dao" ref="dao">property>
    bean>
    <bean id="dao" class="Test_bean.Dao">
        <property name="name" value="offer">property>
    bean>
  1. 创建测试类,进行测试
public class Springtest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Service service = context.getBean("service", Service.class);
        service.printf();
    }
}
  1. 测试结果
my name is offer

2、注入属性—内部Bean

实现步骤:

  1. 创建两个类Service和Dao,在Service类中以Dao作为属性(同上)
  2. 在Spring配置文件中配置对象创建,配置属性注入,结合上面的外部Bean共同学习
    <bean id="service" class="Test_bean.Service">
        <property name="dao">
            <bean id="dao" class="Test_bean.Dao">
                <property name="name" value="offer">property>
            bean>
        property>
    bean>
  1. 创建测试类,进行测试(测试结果和测试代码同上)

3、注入属性—级联赋值

  1. 创建两个类Service和Dao,在Service类中以Dao作为属性
    Dao类同上,Service类中需要添加一个GetDao方法
public class Service {
	//以Dao类型创建属性
    private Dao dao;

    public void setDao(Dao dao) {
        this.dao = dao;
    }
    //与级联赋值配合使用,必须加上get方法
    public Dao getDao() {
        return dao;
    }
    
    public void printf(){
        dao.print();
    }
}
  1. 在Spring配置文件中配置对象创建,配置属性注入,结合上面的外部Bean共同学习
    <bean id="service" class="Test_bean.Service">
        
        <property name="dao" ref="dao">property>
        <property name="dao.name" value="offer">property>
    bean>
    <bean id="dao" class="Test_bean.Dao">bean>
  1. 创建测试类,进行测试(测试结果和测试代码同上)

4、注入属性—集合属性

在Spring配置文件中通过XML文件来配置集合属性,如数组,List集合,set集合,Map集合

  1. 创建Service类,在Service类中定义上述四种类型的属性,并生成相应的set方法
public class Service {

    private String[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;

    public void setArray(String[] array) {
        this.array = array;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "Service{" +
                "array=" + Arrays.toString(array) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                '}';
    }
}
  1. 注入数组属性
    <property name="array">
        <array>
            <value>AAAvalue>
            <value>BBBvalue>
        array>
    property>
  1. 注入list集合属性
    <property name="list">
        <list>
            <value>CCCvalue>
            <value>DDDvalue>
        list>
    property>
  1. 注入set集合属性
    <property name="set">
        <set>
            <value>EEEvalue>
            <value>FFFvalue>
        set>
    property>
  1. 注入map集合属性
    <property name="map">
        <map>
            <entry key="ab" value="GGG">entry>
            <entry key="cd" value="HHH">entry>
        map>
    property>
  1. 创建测试类,进行测试
public class Springtest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Service service = context.getBean("service", Service.class);
        System.out.println(service);
    }
}
  1. 测试结果
Service{array=[AAA, BBB], list=[CCC, DDD], set=[EEE, FFF], map={ab=GGG, cd=HHH}}

5、提取集合注入部分

目的:如果只能将集合对象配置在某个bean内部,则这个集合的配置将不能重用。我们需要将集合bean的配置拿到外面,供其他bean引用。

1、在Spring配置文件引入名称空间util


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

2、使用util标签完成list集合提取

    <util:list id="list">
        <value>AAAvalue>
        <value>BBBvalue>
    util:list>
    <bean id="service" class="Test_bean.Service">
        <property name="list" ref="list">property>
    bean>

❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
若对Spring基础知识感兴趣的可以关注一下博主,我会持续更新Spring基础知识(一边学习一边记录),一起进步,有错误的地方也可以在评论区指出来喔,谢谢大家啦!!!

你可能感兴趣的:(Spring5学习,spring,java,ioc)