• Java框架(三)--Spring IoC容器与Bean管理(3)--对象依赖注入


    对象依赖注入

    在这里插入图片描述
    依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作。
    基于setter方法注入对象
    基于构造方法注入对象

    基于setter方法注入对象

    利用setter实现静态数值注入

    在这里插入图片描述
    将Apple实体类的setTitle修改为

        public void setTitle(String title) {
            System.out.println("title属性设置为:"+title);
            this.title = title;
        }
    
    • 1
    • 2
    • 3
    • 4

    然后在applicationContext.xml中添加配置为

    <!--在IoC容器启动时,自动由Spring实例化Apple对象,取名sweetApple放入到容器中-->
        <bean id="sweetApple" class="com.ql.spring.ioc.entity.Apple">
            <!--IoC容器自动利用反射机制运行时调用setXXX方法为属性赋值-->
            <property name="title" value="红富士"/>
            <property name="origin" value="欧洲"/>
            <property name="color" value="红色"/>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    最后从IoC容器中获取对象

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            Apple sweetApple = context.getBean("sweetApple", Apple.class);
            System.out.println(sweetApple.getTitle());
    /*
    title属性设置为:红富士
    红富士
    */ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    利用setter实现对象注入

    在这里插入图片描述
    将Apple、Child实体类修改为

    public Apple() {
        System.out.println("Apple对象已创建,"+this);
    }
    
    • 1
    • 2
    • 3
    public Child() {
        System.out.println("正在创建Child对象:"+this);
    }
    public void setApple(Apple apple) {
        System.out.println("注入的Apple对象:"+apple);
        this.apple = apple;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    然后在applicationContext.xml中添加配置为

    <bean id="lily" class="com.ql.spring.ioc.entity.Child">
        <property name="name" value="莉莉"/>
        <!--利用ref注入依赖对象-->
        <property name="apple" ref="sweetApple"/>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    最后从IoC容器中获取对象

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Child lily = context.getBean("lily", Child.class);
    /*
    Apple对象已创建,com.ql.spring.ioc.entity.Apple@4bec1f0c
    正在创建Child对象:com.ql.spring.ioc.entity.Child@6c3708b3
    注入的Apple对象:com.ql.spring.ioc.entity.Apple@4bec1f0c
    */ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    基于构造方法注入对象

    首先保证Child实体类有带参构造方法

     public Child(String name, Apple apple) {
    	System.out.println("构造方法参数apple:"+apple);
         this.name = name;
         this.apple = apple;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后在applicationContext.xml中添加配置为

        <bean id="sourApple" class="com.ql.spring.ioc.entity.Apple">
            <property name="title" value="青苹果"/>
            <property name="origin" value="中亚"/>
            <property name="color" value="绿色"/>
        </bean>
        
        <bean id="andy" class="com.ql.spring.ioc.entity.Child">
            <constructor-arg name="name" value="安迪"/>
            <constructor-arg name="apple" ref="sourApple"/>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    最后从IoC容器中获取对象

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Child andy = context.getBean("andy", Child.class);
    andy.eat();
    /**
    Apple对象已创建,com.ql.spring.ioc.entity.Apple@67784306
    title属性设置为:青苹果
    构造方法参数apple:com.ql.spring.ioc.entity.Apple@67784306
    安迪吃到了中亚种植的青苹果
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    体验依赖注入的优势

    打开IDEA创建一个新的maven工程
    在这里插入图片描述
    打开pom.xml添加spring依赖

    <repositories>
            <repository>
                <id>aliyun</id>
                <name>aliyun</name>
                <url>https://maven.aliyun.com/repository/public</url>
            </repository>
        </repositories>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在com.ql.spring.ioc.bookshop.dao包下创建BookDao接口和它的实现类BookDaoImpl

    package com.ql.spring.ioc.bookshop.dao;
    
    public interface BookDao {
        public void insert();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.ql.spring.ioc.bookshop.dao;
    
    public class BookDaoImpl implements BookDao{
        @Override
        public void insert() {
            System.out.println("向MySQL Book表插入一条数据");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在com.ql.spring.ioc.bookshop.service包下创建BookService业务类

    package com.ql.spring.ioc.bookshop.service;
    
    import com.ql.spring.ioc.bookshop.dao.BookDao;
    
    public class BookService {
        private BookDao bookDao;
        public void purchase(){
            System.out.println("正在执行图书采购业务方法");
            bookDao.insert();
        }
    
        public BookDao getBookDao() {
            return bookDao;
        }
    
        public void setBookDao(BookDao bookDao) {
            this.bookDao = bookDao;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在resources下创建两个配置文件applicationContext-dao.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.ql.spring.ioc.bookshop.dao.BookDaoImpl">
    
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    applicationContext-service.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="bookService" class="com.ql.spring.ioc.bookshop.service.BookService">
            <!--id=bookDao-->
            <property name="bookDao" ref="bookDao"/>
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    最后在com.ql.spring.ioc.bookshop包创建应用入口类BookShopApplication

    package com.ql.spring.ioc.bookshop;
    
    import com.ql.spring.ioc.bookshop.service.BookService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class BookShopApplication {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-*.xml");
            BookService bookService = context.getBean("bookService", BookService.class);
            bookService.purchase();
    
        }
    }
    /*
    正在执行图书采购业务方法
    向MySQL Book表插入一条数据
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    如果当前应用把MySQL数据库更换为Oracle该怎么变动?
    在com.ql.spring.ioc.bookshop.dao包下创建Oracle数据库实现类BooKDaoOracleImpl

    package com.ql.spring.ioc.bookshop.dao;
    
    public class BooKDaoOracleImpl implements BookDao{
        @Override
        public void insert() {
            System.out.println("向Oracle Book表插入一条数据");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    之后需要变化的地方只有一个,那就是applicationContext-dao.xml配置文件里把原有MySQL实现类替换为Oracle实现类

    <?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.ql.spring.ioc.bookshop.dao.BooKDaoOracleImpl">
    
        </bean>
    </beans>
    <!--
    正在执行图书采购业务方法
    向Oracle Book表插入一条数据
    -->
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    这里展示了通过Spring IoC控制反转容器对对象之间解耦,导致开发数据库交互层人员和业务层人员的解耦,这对大型软件开发公司非常重要,可以节约很多时间成本的节约。

  • 相关阅读:
    HyperBDR云容灾深度解析四:资源组编排简化容灾操作
    基于springboot、vue汽车租赁系统
    IDEA Mac 快捷键
    Docker中的常用命令
    【实战项目】自主web服务器
    关系型数据库语言基础整理
    pytorch 学习率衰减策略
    [0CTF 2016]piapiapia
    Response对象-响应字符数据
    Kotlin委托Delegate托管by
  • 原文地址:https://blog.csdn.net/qq_32091929/article/details/125472255