• DI依赖注入和第三方bean管理以及核心容器


    1.依赖注入方式

    • setter 注入方式

      • 简单类型
      • 引用类型
    • 构造器注入

      • 简单类型
      • 引用类型
    1.1 setter 注入简单类型

    在实现类中添加简单数据类型,并提供 setter 方法

    public class BookDaoImpl implements BookDao {
    
        //注入简单类型
        private int num;
    
        public void setNum(int num) {
            this.num = num;
        }
    
        @Override
        public void save() {
            System.out.println("book dao save......"+num);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    配置文件中添加 bean

        <bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
            <property name="num" value="10"/>
        bean>
    
    • 1
    • 2
    • 3

    测试运行结果

        public static void main(String[] args) {
    
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
    
            BookDao bookDao = (BookDao) context.getBean("bookDao");
    
            bookDao.save();
            //book dao save......10
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1.2 setter 注入引用数据类型

    在实现类中添加引用数据类型,并提供 setter 方法

    public class BookServiceImpl implements BookService {
        //注入引用类型
        private BookDao bookDao;
    
        @Override
        public void save() {
            System.out.println("book service save......");
            
            bookDao.save();
        }
        //提供对应的set方法
        public void setBookDao(BookDao bookDao) {
            this.bookDao = bookDao;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    配置文件中添加 bean

        <bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
            <property name="connectionNum" value="10"/>
        bean>
    
        <bean id="bookService" class="com.my.service.impl.BookServiceImpl">
            //引用类型
            <property name="bookDao" ref="bookDao"/>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    测试运行结果

        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
    
            BookService bookService = (BookService) context.getBean("bookService");
    
            bookService.save();
            //输出结果为: book service save......
            //book dao save......10
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1.3 构造器注入简单数据类型

    在实现类中,添加构造方法

    public class BookDaoImpl implements BookDao {
    
        private int num;
    
        public BookDaoImpl(int num) {
            this.num = num;
        }
    
        @Override
        public void save() {
            System.out.println("book dao save......"+num);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    配置文件中配置 bean

        <bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
            <constructor-arg name="num" value="10"/>
        bean>
    
    • 1
    • 2
    • 3

    测试运行结果

        public static void main(String[] args) {
    
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
    
            BookDao bookDao = (BookDao) context.getBean("bookDao");
    
            bookDao.save();
            //输出结果为:book dao save......10
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1.4构造器注入引用类型

    在实现类中,添加构造方法

    public class BookServiceImpl implements BookService {
    
        //注入引用类型
        private BookDao bookDao;
    
        public BookServiceImpl(BookDao bookDao, UserDao userDao) {
            this.bookDao = bookDao;
        }
    
        @Override
        public void save() {
            System.out.println("book service save......");
    
            bookDao.save();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    配置文件中配置 bean

        <bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
            <constructor-arg name="connectionNum" value="10"/>
        bean>
    
        <bean id="bookService" class="com.my.service.impl.BookServiceImpl">
            <constructor-arg name="bookDao" ref="bookDao"/>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    测试运行结果

        public static void main(String[] args) {
    
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
    
            BookService bookService = (BookService) context.getBean("bookService");
    
            bookService.save();
            //输出结果为:book service save......
            //book dao save......10
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1.5集合注入

    创建一个实现类,添加集合:数组、List、Set、Map、Properties 。 并提供 setter 方法

    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;
        }
    
        @Override
        public void save() {
    
            System.out.println("array数组:"+ Arrays.toString(array));
            System.out.println("list数组:"+list);
            System.out.println("set数组:"+set);
            System.out.println("map数组:"+map);
            System.out.println("properties数组:"+properties);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    配置文件,注入集合类型数据

       <bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
    
            <property name="array">
                <array>
                    <value>1value>
                    <value>2value>
                    <value>3value>
                array>
            property>
    
            <property name="list">
                <list>
                    <value>avalue>
                    <value>bvalue>
                    <value>cvalue>
                list>
            property>
    
            <property name="set">
                <set>
                    <value>avalue>
                    <value>bvalue>
                    <value>cvalue>
                    <value>cvalue>
                set>
            property>
    
            <property name="map">
                <map>
                    <entry key="1" value="a"/>
                    <entry key="2" value="b"/>
                    <entry key="3" value="c"/>
                map>
            property>
    
            <property name="properties">
                <props>
                    <prop key="4">dprop>
                    <prop key="5">eprop>
                    <prop key="6">fprop>
                props>
            property>
    
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    测试运行结果

        public static void main(String[] args) {
    
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
    
            BookDao bookDao = (BookDao) context.getBean("bookDao");
    
            bookDao.save();
            //输出结果为:array数组:[1, 2, 3]
            //list数组:[a, b, c]
            //set数组:[a, b, c]
            //map数组:{1=a, 2=b, 3=c}
            //properties数组:{4=d, 5=e, 6=f}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2. 第三方数据源对象管理(管理第三方 bean)

    1. 导入 spring-context 依赖和 第三方 Druid 依赖
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>5.2.22.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.2.11version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 开启命名空间,在 beans 标签里添加 context ,如下:
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
           xmlns:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
    
           ">
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 创建资源包 properties 文件
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/spring
    jdbc.username=root
    jdbc.password=root
    
    • 1
    • 2
    • 3
    • 4
    1. 配置文件中加载 properties 文件,使用 ${} 读取文件里的属性值
    
    
       <context:property-placeholder location="jdbc.properties"/>
    
    
       <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
           <property name="driverClassName" value="${jdbc.driver}"/>
           <property name="url" value="${jdbc.url}"/>
           <property name="username" value="${jdbc.username}"/>
           <property name="password" value="${jdbc.password}"/>
       bean>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    测试运行结果

        public static void main(String[] args) {
    
            ApplicationContext context =new ClassPathXmlApplicationContext("applicationConfig.xml");
    
            DataSource dataSource = (DataSource) context.getBean("dataSource");
    
            System.out.println(dataSource);
            //输出结果为:{
            //	CreateTime:"2022-09-18 19:45:03",
            //	ActiveCount:0,
            //	PoolingCount:0,
            //	CreateCount:0,
            //	DestroyCount:0,
            //	CloseCount:0,
            //	ConnectCount:0,
            //	Connections:[
            //	]
            //}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.核心容器

    3.1创建容器的方式
    • 类路径加载配置文件

      ApplicationContext context =new ClassPathXmlApplicationContext("applicationConfig.xml");
      
      • 1
    • 文件路径加载配置文件

      ApplicationContext context1=new FileSystemXmlApplicationContext("D:\\SpringProjects\\spring_07_dataSource\\src\\main\\resources\\applicationConfig.xml");
      
      • 1
    • 加载多个配置文件,用逗号隔开

      ApplicationContext context =new ClassPathXmlApplicationContext("applicationConfig.xml","applicationConfig2.xml");
      
      • 1
    3.2获取 bean 的方式
    • 使用 bean 名称获取

      BookDao bookDao = (BookDao) context.getBean("bookDao");
      
      • 1
    • 使用 bean 名称获取并指定类型

      BookDao bookDao = context.getBean("bookDao", BookDao.class);
      
      • 1
    • 使用 bean 类型获取

      BookDao bookDao = context.getBean(BookDao.class);
      
      • 1
    3.3容器类层次结构

    在这里插入图片描述

  • 相关阅读:
    大屏项目也不难
    java.sql.SQLFeatureNotSupportedException解决方法
    项目ERP与传统ERP的区别是什么?
    Python 可迭代对象、迭代器、生成器
    vue案例
    [字符串和内存函数]错误信息报告函数strerror详解
    osgEarth::ElevationQuery的setMaxTilesToCache函数的作用
    金仓数据库 KingbaseES 插件参考手册 S(3)
    Linux的ssh服务管理
    C++引用
  • 原文地址:https://blog.csdn.net/m0_59564754/article/details/126922550