Spring IOC(Inversion of Control)

2023-05-16  本文已影响0人  ChinaGoodStaff

IoC的理解

ioc也叫控制反转,所谓的控制,就是把对象交给ispring容器进行管理,反转则是对象交给了容器以后,对象之间相互调用,不需要再new出一个新的对象,只需要注入就好了
把创建和查找依赖对象的控制权交给 IoC 容器,由 IoC 容器进行注入、组合对象之间的关 系。这样对象与对象之间是松耦合、功能可复用(减少对象的创建和内存消耗),使得程序 的整个体系结构可维护性、灵活性、扩展性变高。 所谓IOC ,就简短一句话:对象由 spring来创建、管理,装配!

IoC遵循的原则

接口分离原则ISP(the Interface Segregation Principle ISP)

模块间要通过抽象接口隔离开,而不是通过具体的类强耦合起来

依赖倒置原则DIP(the Dependency Inversion Principle DIP)

具体实现依赖抽象,下层依赖上层

IOC是DIP的设计原理,DI是IOC的具体实现

IoC的代码实现

1.导入jar包+配置xml

入门搭建基于ioc的spring
需要导入的核心包

Beans
Core
Context
expression-sPex

配置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 class="com.company.dao.impl.UserDaoOracleImpl" id="userDaoOracle" />
    <bean class="com.company.service.impl.UserServiceImpl" id="userService" >
        <property name="dao" ref="userDaoOracle"></property>
    </bean>
</beans>
2.maven+注解+配置xml

配置maven依赖,到pom.xml中

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.lll</groupId>
    <artifactId>spring_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.18</version>
    </dependency>
    </dependencies>
</project>

添加spring配置文件spring_ioc.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"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="cn.lll.beans.User" id="user">
    </bean>
</beans>
 publicclassIocTest{

        @Test
        public void test01(){
            ApplicationContext ioc=new ClassPathXmlApplicationContext("spring‐ioc.xm
                    l");
                    User bean = ioc.getBean(User.class);
            System.out.println(bean);

        }
    }
3.springBoot+javaconfig

最厉害的实现方式,Spring3.0以后开始支持不需要xml配置文件的,可以使用javaconfig配置来代替xml配置定义外部bean
从Spring4.0开始支持springboot1.0之后,springboot完全采用javaconfig的方式进行开发

DI与IOC

很多人把IOC和DI说成一个东西, 笼统来说的话是没有问题的,但是本质上还是有所区别的,IOC和DI是从不同的角度描述的同一件事,I0C是从容器的角度描述,而DI是从应用程序的角度来描述,也可以这样说,I0C是依赖倒置原则的设计思想,而DI是具体的实现方式

IOC的优点

  1. 集中管理
  2. 功能可复用(减少对象的创建和内存消耗)
  3. 使得程序的整个体系结构可维护性、灵活性、扩展性变高
4. 解耦
上一篇 下一篇

猜你喜欢

热点阅读