Spring系列(三)IOC 之 DI(依赖注入)详解

18 篇文章 3 订阅
13 篇文章 1 订阅

扩展:Spring系列学习汇总


一、构造器注入
  • 构造器注入其实在上一篇文章中讲的比较详细了,在这里就不在赘述,只举个最简单的例子来吧。
    ①实体类:
public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
    
    public Person(String name, int age, String like, String high) 	{
        this.name = name;
        this.age = age;
        this.like = like;
        this.high = high;
    }
    //set、get、tostring方法因为篇幅原因省略,请手动加上!
}

②xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大1"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼1"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

③测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("person4", Person.class);
        System.out.println(person);
    }
}

④执行结果:
在这里插入图片描述
⑤总结:

  • 无参构造器不用多说,无法注入值
  • 可以通过有参构造器来注入值
二、Set 注入(重点!!!!!)
  • 前提: 实体类:
public class Pojo {
    private String name;
    private Person person;
    private int[] intArr;
    private List<String> list;
    private Map<String,Object> map;
    private Set<String> set;
    private String like;
    private Properties info;
}
//set、get、tostring方法因为篇幅原因省略,请手动加上!
2.01、常量注入

①beans.xml:

	<bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="name" value="丁大大"/>
    </bean>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        System.out.println(testPojo.getName());
    }
}

③执行结果:
在这里插入图片描述

2.02、Bean注入

①beans.xml:

  • 这里要注意property
    • value 是用来赋值常量的
    • ref 可以用来赋值bean
	<!--先将实体bean的值通过构造器注入进去-->
	<bean name="testPerson" class="entity.Person">
        <constructor-arg index="0" value="丁大大2"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼2"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
	<!--再将实体bean注入到Pojo中去-->
    <bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="name" value="丁大大"/>
        <property name="person" ref="testPerson"/>
    </bean>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        System.out.println(testPojo.getName());
        System.out.println(testPojo.getPerson());
    }
}

③执行结果:
在这里插入图片描述

2.03、数组注入

①beans.xml:

  • 使用 array 来加值
	<bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="intArr">
            <array>
                <value>7</value>
                <value>5</value>
                <value>9</value>
            </array>
        </property>
    </bean>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        System.out.println(testPojo.getName());
        System.out.println(testPojo.getPerson());
        int[] intArr = testPojo.getIntArr();
        for (int i : intArr) {
            System.out.print(i+"  ");
        }
    }
}

③执行结果:
在这里插入图片描述

2.04、List注入

①beans.xml:

	<bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="list">
            <list>
                <value>钓鱼</value>
                <value>捕鱼</value>
                <value>吃鱼</value>
            </list>
        </property>
    </bean>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        List<String> list = testPojo.getList();
        for (String s : list) {
            System.out.print(s+"  ");
        }
    }
}

③执行结果:
在这里插入图片描述

2.05、Map注入

①beans.xml:

	<property name="map">
            <map>
                <entry key="name" value="丁大大"/>
                <entry key="age" value="23"/>
                <entry key="like" value="钓鱼"/>
            </map>
        </property>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        Map<String,Object> map = testPojo.getMap();
        for (Object value : map.values()){
            System.out.print(value+"  ");
        }
    }
}

③执行结果:
在这里插入图片描述

2.06、set注入

①beans.xml:

	<property name="set">
            <set>
                <value>英雄联盟</value>
                <value>吃鸡</value>
                <value>王者荣耀</value>
                <value>原神</value>
            </set>
        </property>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        Set<String> set = testPojo.getSet();
        for (String s : set) {
            System.out.println(s);
        }
    }
}

③执行结果:
在这里插入图片描述

2.07、Null注入

①beans.xml:

		<property name="like">
            <null></null>
        </property>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        System.out.println(testPojo.getLike());
    }
}

③执行结果:
在这里插入图片描述

2.08、Properties注入

①beans.xml:

		<property name="info">
            <props>
                <prop key="userName">773530472</prop>
                <prop key="passWord">123456</prop>
                <prop key="验证码">Tg3O</prop>
            </props>
        </property>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        System.out.println(testPojo.getInfo().toString());
    }
}

③执行结果:
在这里插入图片描述

2.09、p命名注入

①beans.xml:注意这里要在头文件引入外部约束

  • xmlns:p=“http://www.springframework.org/schema/p”
  • P(属性: properties)命名空间 , 属性依然要设置set方法
  • 不能有有参构造函数
    在这里插入图片描述
    在这里插入图片描述
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="Person" class="entity.Person" p:name="丁大大" p:age="23"/>
</beans>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person testPojo = Context.getBean("Person", Person.class);
        System.out.println(testPojo);
    }
}

③执行结果:
在这里插入图片描述

2.10、c命名注入

①beans.xml:需要在头文件中加入约束文件

  • xmlns:c=“http://www.springframework.org/schema/c”
  • C(构造: Constructor)命名空间 , 属性依然要设置set方法
  • 构造器中有几个参数就得写几个c的参数,不然会报错!
    在这里插入图片描述
    在这里插入图片描述
<?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: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 id="Person" class="entity.Person" c:name="丁大大" c:age="23" c:like="哈哈哈" c:high="123"></bean>
</beans>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person testPojo = Context.getBean("Person", Person.class);
        System.out.println(testPojo);
    }
}

③执行结果:
在这里插入图片描述

路漫漫其修远兮,吾必将上下求索~
如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧**转载请注明出处哦**

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值