Spring中的控制反转

控制反转(IoC):对象创建及处理交给容器

在传统面向对象编程中,应用程序是主动创建相关的对象。然后再将对象组合使用。这样会导致类与类之间高耦合,并且难以测试。 在使用Spring框架之后,对象的实例不再由调用者来创建,而是由Spring的loC容器来创建,IoC容器会负责控制程序之间的关系,而不是由调用者的程序代码直接控制。

这样,控制权由应用代码转移到了IoC容器,控制权发生了反转,这就是Spring的控制反转。

eg:

pom.xml中导入spring四个基础包和一个依赖包

<?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>org.example</groupId>
    <artifactId>Spring_Study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- 基础包spring-core-5.2.8.RELEASE -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- 基础包spring-beans-5.2.8.RELEASE -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- 基础包spring-context-5.2.8.RELEASE -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- 基础包spring-expression-5.2.8.RELEASE -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        
        <!-- 依赖包commons-logging-1.2 -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</project>

spring四个基础包:

Core:spring框架的基本依赖包,提供了主要的基础服务,包括①IOC(控制反转)和②AOP(面向切面)等功能;

Beans:提供BeanFactory,实现对bean的管理配置;

Context:继承了bean模块,用于上下文的实现,对基本功能的扩展提供企业级的支持,如缓存,调度,模板引擎等;

Spring-expression:为Spring提供强大的表达式语言支持,支持set和get属性值、属性赋值、方法调用、访问数组集合及索引的内容、逻辑算术运算、命名变量、通过名字从Spring IoC容器检索对象,还支持列表的投影、选择以及聚合等

创建实体类

package com.itheima;

public class HelloSpring {
    private String userName;
    public void setUserName(String username) {
        this.userName = username;
    }
    public void show() {
        System.out.println(userName + ":欢迎来到Spring");
    }
}

创建spring配置文件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,将指定类配置给Spring,让Spring创建HelloSpring实例 -->
    <bean id="helloSpring" class="com.itheima.HelloSpring">
        <!-- 为userName属性赋值 -->
        <property name="userName" value="张三" ></property>
    </bean>
</beans>

创建测试类

package com.itheima;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloSpring {
    public static void main(String[] args) {
        //初始化spring容器,加载applicationContext.xml配置
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过spring容器获得helloSpring实例
        HelloSpring helloSpring = (HelloSpring) applicationContext.getBean("helloSpring");
        helloSpring.show();
    }
}

在main()方法中没有通过关键字new创建HellowSpring类的对象,而是通过Spring容器获取实体类对象,这就是Spring IoC容器的实现机制

结合这篇有关依赖注入的文章更有助于理解控制反转Spring中的依赖注入-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值