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 @   Java小白的搬砖路  阅读(128)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示