Spring学习(五)DI依赖注入

Spring DI依赖注入

Spring DI依赖注入,看其官方文档:spring中文文档 中可以看到有两种:基于构造函数的依赖注入和基于Setter的依赖注入。

在这里插入图片描述

基于构造函数的依赖注入

这部分的内容在 Spring学习(三)Spring容器是如何创建对象的 中有提到过。具体的就不再描述了。这不是重点,可以发现构造器的依赖注入十分有限。重点还是在基于Setter的依赖注入

基于Setter的依赖注入【重点】

什么是Setter依赖注入?本质就是Set注入。

  • 依赖:bean对象的创建依赖于容器

  • 注入:bean对象中的所有属性由容器来注入

我们直接上代码:

来一个复杂类型的,首先创建一个Address类:

// 引用对象的例子
public class Address {
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

然后再创建一个十分复杂的Student类:

public class Student {
    private String name;//姓名,String类型
    private Address address;//地址,引用类型
    private String[] books;//拥有的课本,数组类型
    private List<String> hobbys;//兴趣爱好,列表类型
    private Map<String, String> card;//id卡,Map类型
    private Set<String> games;//打的游戏,Set类型
    private Properties info;
    private String wife;//空指针

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String[] getBooks() {
        return books;
    }
    public void setBooks(String[] books) {
        this.books = books;
    }
    public List<String> getHobbys() {
        return hobbys;
    }
    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }
    public Map<String, String> getCard() {
        return card;
    }
    public void setCard(Map<String, String> card) {
        this.card = card;
    }
    public Set<String> getGames() {
        return games;
    }
    public void setGames(Set<String> games) {
        this.games = games;
    }
    public Properties getInfo() {
        return info;
    }
    public void setInfo(Properties info) {
        this.info = info;
    }
    public String getWife() {
        return wife;
    }
    public void setWife(String wife) {
        this.wife = wife;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}

然后开始写对应的配置文件applicationContext.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 id="address" class="com.hj.pojo.Address">
        <property name="address" value="翻斗花园"/>
    </bean>
    <bean id="student" class="com.hj.pojo.Student">
        <!--1.普通值的注入-->
        <property name="name" value="张三"/>
        <!--2.引用的注入(Bean注入)-->
        <property name="address" ref="address"/>
        <!--3.数组的注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>三国演义</value>
                <value>西游记</value>
            </array>
        </property>
        <!--4.list的注入-->
        <property name="hobbys">
            <list>
                <value>敲代码</value>
                <value>听歌</value>
                <value>唱歌</value>
            </list>
        </property>
        <!--5.map的注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="123456"/>
                <entry key="校园卡" value="889456"/>
            </map>
        </property>
        <!--6.set的注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>王者荣耀</value>
            </set>
        </property>
        <!--这样做只是让其属性为空,并不是null!-->
        <!--<property name="wife" value=""/>-->
        <!--7.null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--8.properties注入-->
        <property name="info">
            <props>
                <prop key="学历">硕士</prop>
                <prop key="已婚"></prop>
                <prop key="性别"></prop>
                <prop key="username">root</prop>
            </props>
        </property>
    </bean>
</beans>

最后进行测试:

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student st = (Student) context.getBean("student");
        System.out.println(st.toString());//可以得到所有我们注入的值,说明注入成功!
    }
}

本小节代码:spring-05中

扩展方式注入

我们可以使用p命名空间和c命名空间来进行注入。

P命名空间注入

需要在beans里添加xmlns:p="http://www.springframework.org/schema/p"语句约束,然后就可以直接以p:属性=value的方式注入。

<?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">
    <!--p命名空间注入,可以直接注入属性的值,这里p其实就是属性的意思就是用属性注入-->
    <bean id="user" class="com.hj.pojo.User" p:name="张三" p:age="18"/>
</beans>

C命名空间注入

其实就是通过构造器来注入,所以需要类有有参构造器。同样也需要在beans里添加xmlns:c="http://www.springframework.org/schema/c"语句约束,然后就可以直接以c:属性=value的方式注入了。

<?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">
    <!--c命名空间注入,其实就是通过构造器注入,所以需要有参构造器-->
    <bean id="user2" class="com.hj.pojo.User" c:name="李四" c:age="21"/>
</beans>

测试:

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("UserBean.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.toString());//User{name='张三', age=18}
        User user2 =context.getBean("user2",User.class);
        System.out.println(user2.toString());//User{name='李四', age=21}
    }
}

本小节代码:spring-06中

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值