当前位置: 首页 > article >正文

Spring入门篇3 --- 依赖注入(DI)方式、集合注入

目录

1.依赖注入方式

依赖注入(DI:dependence injection):在容器中建立bean与bean之间的依赖关系的整个过程。

向一个bean中传递数据有三种方法:setter注入、构造器注入自动装配

/src/main/java/com/itheima/dao/BookDao.java

package com.itheima.dao;

public interface BookDao {
    public void save();
}

/src/main/java/com/itheima/dao/impl/BookDaoImpl.java

package com.itheima.dao.impl;
import com.itheima.dao.BookDao;

public class BookDaoImpl implements BookDao {

    public void save() {
        System.out.println("book dao save...");
    }
}

/src/main/java/com/itheima/service/BookService.java

package com.itheima.service;

public interface BookService {
    public void save();
}

/src/main/java/com/itheima/App.java

package com.itheima;
import com.itheima.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App{
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) ctx.getBean("bookService");
        bookService.save();
    }
}

①setter注入

/src/main/java/com/itheima/service/impl/BookServiceImpl.java

package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;

public class BookServiceImpl implements BookService {
    private BookDao bookDao;
    private int connectionNumber;

    // 在bean中定义引用类型属性并提供可访问的set方法
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    // 在bean中定义简单类型属性并提供可访问的set方法
    public void setConnectionNumber(int connectionNumber) {
        this.connectionNumber = connectionNumber;
    }

    public void save() {
        System.out.println("book service save ..." + connectionNumber);
        bookDao.save();
    }
}

/src/main/resources/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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" />
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
        <!--配置中使用property标签的ref属性注入引用类型对象-->
        <property name="bookDao" ref="bookDao" />
        <!--配置中使用property标签的value属性注入简单类型数据-->
        <property name="connectionNumber" value="10" />
    </bean>
</beans>

②构造器注入

/src/main/java/com/itheima/service/impl/BookServiceImpl.java

package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;

public class BookServiceImpl implements BookService {
    private BookDao bookDao;
    private int connectionNumber;

    public BookServiceImpl(BookDao bookDao, int connectionNumber) {
        this.bookDao = bookDao;
        this.connectionNumber = connectionNumber;
    }

    public void save() {
        System.out.println("book service save ..." + connectionNumber);
        bookDao.save();
    }
}

/src/main/resources/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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" />
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
        <constructor-arg name="connectionNumber" value="10"/>
    </bean>
</beans>

测试结果

③自动装配

依赖自动装配:IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配。

/src/main/java/com/itheima/service/impl/BookServiceImpl.java

package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;

public class BookServiceImpl implements BookService {
    private BookDao bookDao;

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
    }
}

/src/main/resources/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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" />
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>
</beans>

2.集合注入

/src/main/java/com/itheima/dao/impl/BookDaoImpl.java

package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
import java.util.*;

public class BookDaoImpl implements BookDao {

    private int[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

    public void setArray(int[] array) {
        this.array = array;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void save() {
        System.out.println("book dao save ...");
        System.out.println("遍历数组:" + Arrays.toString(array));
        System.out.println("遍历List" + list);
        System.out.println("遍历Set" + set);
        System.out.println("遍历Map" + map);
        System.out.println("遍历Properties" + properties);
    }
}

/src/main/resources/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="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
        <!--数组注入-->
        <property name="array">
            <array>
                <value>100</value>
                <value>200</value>
            </array>
        </property>
        <!--list集合注入-->
        <property name="list">
            <list>
                <value>itcast</value>
                <value>itheima</value>
            </list>
        </property>
        <!--set集合注入-->
        <property name="set">
            <set>
                <value>itcast</value>
                <value>itheima</value>
            </set>
        </property>
        <!--map集合注入-->
        <property name="map">
            <map>
                <entry key="country" value="china"/>
                <entry key="province" value="henan"/>
            </map>
        </property>
        <!--Properties注入-->
        <property name="properties">
            <props>
                <prop key="country">china</prop>
                <prop key="province">henan</prop>
            </props>
        </property>
    </bean>
</beans>

/src/main/java/com/itheima/App.java

package com.itheima;

import com.itheima.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main( String[] args ) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }
}

测试


http://www.kler.cn/a/2483.html

相关文章:

  • 【VulnOSv2靶场渗透】
  • 【每日学点鸿蒙知识】Webview加载内容、router返回问题、Webview定制错误页面、html格式字符串、Toggle高
  • SVM分类-支持向量机(Support Vector Machine)
  • 使用VS Code开发ThinkPHP项目
  • 深入理解 Java 中的接口(Interface)
  • yolov3算法及其改进
  • 网络技术与应用概论(上)——“计算机网络”
  • 第29次CCFCSP认证经验总结
  • C语言 结构体进阶 结构体、枚举、联合详解(2)
  • AWS白皮书总结
  • 计算机网络管理 TCP三次握手的建立过程,Wireshark抓包分析并验证TCP三次握手建立连接的报文
  • I2C模块理解
  • Linux系统下gdb调试
  • 【Go】K8s 管理系统项目[Jenkins Pipeline K8s环境–应用部署]
  • Python 项目之实现文件内容的反转再输入(一)完全反转
  • react中渲染企业微信的表情
  • 使用shell 脚本,批量解压一批zip文件,解压后的文件放在以原zip文件名前10个字符的文件夹中的例子
  • Java stream性能比较
  • java基础面试题(四)
  • TU-95 strategic bomber气动布局分析
  • 蓝桥杯训练day3
  • 深入理解JVM虚拟机(六)
  • 梳理LVM逻辑卷管理,
  • JDBC
  • C++虚函数与多态
  • ChatGPT推出第四代GPT-4!不仅能聊天,还可以图片创作!