• Spring之IOC(一)


    1. IOC的概念

    IOC是控制反转的意思,把对象和创建和对象之间的调用过程交给spring进行管理,IOC可以使得对象之间的耦合度降低
    IOC的底层原理基于xml解析、工厂模式和反射

    举个例子,现在在Main类中调用Dog类中的woof()方法常用的做法如下:

    1. 原始方式

    public class Dog {
        public void woof(){
            System.out.println("wowowo");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class Main {
        public static void main(String[] args) {
            Dog dog=new Dog();
            dog.woof();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    假设现在不需要狗叫了,需要猫叫,需要另外一个类Cat的bark方法,此时Main类中所有涉及到声明Dog和创建Dog对象代码都需要更改

    2. 接口
    将狗叫和猫叫统一为动物类接口noise方法的不同实现

    public interface Animal {
        public void noise();
    
    
    • 1
    • 2
    • 3
    public class Dog implements Animal {
        public void noise(){
            System.out.println("wowowo");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    public class Cat implements Animal{
        public void noise() {
            System.out.println("miao");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class Main {
        public static void main(String[] args) {
            Animal animal=new Dog();
            animal.noise();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    此时如果需要猫叫,只需要更改new后面的部分,声明部分不用更改

    3. 工厂模式

    public class AnimalFactory {
        public static Animal animalFactory(String name){
            if("dog".equals(name)){
                return  new Dog();
            }else if("cat".equals(name)){
                return new Cat();
            }
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    public class Main {
        public static void main(String[] args) {
            Animal animal=AnimalFactory.animalFactory("dog");
            animal.noise();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    现在如果需要更改的话只需要修改animalFactory方法中的参数即可

    4. IOC方式

    //beans.xml
    <bean id="animal" class="cn.edu.xd.dao.Dog"/>
    
    • 1
    • 2
    class AnimalFactory {
        public static Animal getAnimal() {
            String className = 解析配置文件xml 拿到id对应的class
            // 反射
            class clazz = class.forName(className);
            return clazz.newInstance();
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    现在如果要修改的话只要在beans.xml修改animal对应的实现类即可

    2. IOC示例代码

    idea中创建一个maven工程,创建一个lib文件夹,在lib文件夹中加入以下jar包:
    在这里插入图片描述

    右键file->project structure, 将刚刚的lib文件夹中的jar加入项目中
    在这里插入图片描述

    项目结构如下:
    在这里插入图片描述

    创建一个User类:

    package cn.edu.xd.dao;
    
    public class User {
        public void add(){
            System.out.println("add method...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    创建一个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="user" class="cn.edu.xd.dao.User"/>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    测试类代码:

    public class TestUser {
        @Test
        public void test1(){
            System.out.println("get user by new");
            User user=new User();
            user.add();
        }
        @Test
        public void test2(){
            System.out.println("get user by IOC");
            ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
            User user=context.getBean("user", User.class);
            user.add();
    
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述


    3. IOC接口

    1. IOC基于IOC容器完成,IOC 容器底层就是对象工厂
    2. Spring 提供 IOC 容器实现两种方式:(两个接口)

    在这里插入图片描述
    ApplicationContext是BeanFactory的一个子接口

    (1)BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用,加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象

    public void test2(){
            System.out.println("get user by IOC");
            BeanFactory context=new ClassPathXmlApplicationContext("bean1.xml");//加载文件
            User user=context.getBean("user", User.class);//创建对象
            user.add();
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (2)ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用,加载配置文件时候就会把在配置文件对象进行创建

    public void test2(){
            System.out.println("get user by IOC");
            ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");//加载文件同时创建对象
            User user=context.getBean("user", User.class);
            user.add();
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ApplicationContext 接口有两个实现类:

    • FileSystemXmlApplicationContext:使用时参数是系统的绝对路径
    ApplicationContext context=new FileSystemXmlApplicationContext("D:\\IDEA-WORKSPACE\\springdemo\\src\\main\\resources\\bean1.xml");
    
    • 1
    • ClassPathXmlApplicationContext:使用时参数是类路径
     ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    
    • 1
  • 相关阅读:
    浅析Spring浅拷贝 BeanUtils.copyProperties方法
    企业电子招投标采购系统源码之电子招投标的组成
    S/4 HANA 大白话 - 财务会计-2 总账主数据
    连接器信号完整性仿真教程 八
    一篇文章学会调优 ClickHouse
    java-net-php-python-jspm校园闲置物品拍卖系统计算机毕业设计程序
    【计算机视觉40例】案例24:实例分割
    各类开源协议
    2000-2022年各区县农产品产量数据
    TI mmWave radar sensors Tutorial 笔记 | Module 4 : Some System Design Topics
  • 原文地址:https://blog.csdn.net/qq_43478694/article/details/125434277