DI依赖注入

本文详细介绍了Spring框架中的依赖注入(DI)原理,包括Set注入的方式,展示了如何配置XML文件来注入复杂类型的属性,如Address、String数组、List、Map、Set、Properties等。此外,还提到了使用p和c命名空间简化注入的语法,并讨论了Bean的作用域,如单例和原型模式。最后,通过测试类验证了注入的有效性。
摘要由CSDN通过智能技术生成

依赖注入:Set注入
依赖:bean对象的创建依赖于容器!
注入:bean对象中的所有属性,由容器来注入!

【环境搭建】

复杂类型

package com.linfeng.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

真实测试对象

package com.linfeng.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    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> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    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 String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

beans.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="student" class="com.linfeng.pojo.Student">
    <property name="name" value="林峰"/>
</bean>
</beans>

测试类

import com.linfeng.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        String name = student.getName();
        System.out.println(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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.linfeng.pojo.Address">
        <property name="address" value="南京"    />
    </bean>

   <bean id="student" class="com.linfeng.pojo.Student">
    <property name="name" value="林峰"/>
       <property name="address" ref="address"/>
       <property name="books" >
           <array>
               <value>三国</value>
               <value>水浒</value>
               <value>西游记</value>
           </array>
       </property>
       <property name="hobbies">
           <list>
               <value>代码</value>
               <value>滑雪</value>
               <value>游泳</value>
           </list>
       </property>
       <property name="card">
           <map>
               <entry key="身份证" value="12345324"/>
               <entry key="银行卡" value="124325123"/>
           </map>
       </property>
<property name="games">
    <set>
        <value>LOL</value>
        <value>CF</value>
    </set>
</property>
       <property name="wife" >
           <null/>
       </property>
<property name="info">
    <props>
        <prop key="driver">1241234</prop>
        <prop key="url">1241324234</prop>
    </props>
</property>
   </bean>

</beans>

在这里插入图片描述

拓展方式注入

我们可以使用p命名空间和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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.kuang.pojo.User" p:name="黑心白莲" p:age="20"/>

    <!--c命名空间注入,通过构造器注入:constructor-args-->
    <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22"/>

</beans>

测试

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

        User user = context.getBean("user",User.class);
        System.out.println(user);

        User user2 = context.getBean("user2",User.class);
        System.out.println(user2);
    }

注意点:p命名和c命名空间不能直接使用,需要导入xml约束!

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

或者 使用XML自动生成约束的可以在上边直接敲xmlns:p/c=就自动提示了
在这里插入图片描述

Bean的作用域(Scopes)
在这里插入图片描述
单例模式(Spring默认机制)

<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="singleton"/>

原型模式:每次从容器中get的时候,都会产生一个新对象!

<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>

其余的request、session、application、这些只能在web开发中使用到!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值