5、DI依赖注入

DI

  • 依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。
  • 为什么需要DL?
    • 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。
    • ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。
    • 那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。
    • 简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

1、构造函数注入(必须为所有参数赋值):

  详情请参考我的上一篇博客:IOC创建对象的方式(基于构造函数的依赖注入)

2、set方法注入(不需要为所有成员赋值):

  只使用setter方法

pojo实体类:

/**
 * @author zhangzhixi
 */
public class Student {
    private String id;
    private String name;
    private List<String> hobbies;
    private Map<String,String> location;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

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

    public Map<String, String> getLocation() {
        return location;
    }

    public void setLocation(Map<String, String> location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", hobbies=" + hobbies +
                ", location=" + location +
                '}';
    }
}

bens.xml:

    <bean id="student" class="com.zhixi.pojo.Student">
        <property name="id" value="2018021506"/>
        <property name="name" value="张志喜"/>
    </bean>

测试类:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bens.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getId()); // 2018021506
        System.out.println(student.getName()); // 张志喜
    }
}

2.1、注入List、Map、Properties

bens.xml:

 

<bean id="student" class="com.zhixi.pojo.Student">
        <property name="hobbies">
            <list>
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </list>
        </property>
<property name="location"> <map> <entry key="464300" value="息县"/> <entry key="464000" value="信阳"/> <entry key="450000" value="郑州"/> </map> </property>

<property name="properties"> <props> <prop key="driver">com.mysql.jdbc.Driver</prop> <prop key="url">jdbc:mysql://localhost:3306/mybatis</prop> <prop key="username">root</prop> <prop key="password">zhixi158</prop> </props> </property> </bean>

 

测试类:

 

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bens.xml");
        Student student = (Student) context.getBean("student");

        List<String> hobbies = student.getHobbies();
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        Map<String, String> location = student.getLocation();
        System.out.println(location.get("464300"));

        Properties properties = student.getProperties();
        System.out.println(properties.getProperty("driver"));
    }
}

输出结果:

抽烟
喝酒
烫头
息县
com.mysql.jdbc.Driver

 或者直接在测试类中对student.toString

Student{id='null', name='null', hobbies=[抽烟, 喝酒, 烫头], location={464300=息县, 464000=信阳, 450000=郑州}, properties={password=zhixi158, url=jdbc:mysql://localhost:3306/mybatis, driver=com.mysql.jdbc.Driver, username=root}}

 

posted @ 2021-01-02 15:07  Java小白的搬砖路  阅读(125)  评论(0编辑  收藏  举报