当前位置: 首页 > news >正文

【Spring】一文带你吃透IOC技术

在这里插入图片描述

个人主页: 几分醉意的CSDN博客_传送门

本文目录

  • 💖loC 控制反转
    • ✨loC的技术实现
    • ✨实现步骤
    • ✨创建接口和实现类
    • ✨创建Spring的配置文件和声明bean
    • ✨创建spring容器对象
    • ✨spring容器创建对象的特点
    • ✨创建非自定义类的对象
    • ✨创建没有接口的类的对象
    • ✨获取容器中对象的信息

💖loC 控制反转

loC,Inversion ofControl:控制反转,是一个理论,一个指导思想。指导开发人员如何使用对象,管理对象的。把对象的创建,属性赋值,对象的声明周期都交给代码之外的容器管理。

loC分为控制和反转
  控制:对象创建,属性赋值,对象声明周期管理。
  反转:把开发人员管理对象的权限转移给了代码之外的容器实现。由容器完成对象的管理。
  ●正转:开发人员在代码中,使用new构造方法创建对象。开发人员掌握了对象的创建,属性赋值,对象从开始到销毁的全部过程。开发人员有对对象全部控制。

通过容器,可以使用容器中的对象(容器已经创建了对象,对象属性赋值了, 对象也组装好了)。 Spring就是一个容器,可以管理对象,创建对象,给属性赋值。

✨loC的技术实现

DI(依赖注入):DependencyInjection,缩写是DI是loC的一种技术实现。程序只需要提供要使用的对象的名称就可以了,对象如何创建,如何从容器中查找,获取都由容器内部自己实现。

依赖名词:比如说ClassA类使用了ClassB的属性或者方法,叫做ClassA依赖ClassB。

public class ClassB {
    public void creat(){ }

}

public class ClassA{
    //属性
    private ClassB b = new ClassB();
    
    public void buy(){
        b.creat();
    }
}

执行ClassAbuy()
ClassA a = new ClassA();
a.buy();

注意:Spring框架使用的DI实现loC.通过spring框架,只需要提供要使用的对象名称就可以了。从容器中获取名称对应的对象。spring底层使用的反射机制,通过反射创建对象,给属性。

✨实现步骤

使用spring: spring作为容器管理对象, 开发人员从spring中获取要使用的对象。

实现步骤:

新建maven项目

加入依赖, 修改pom.xml
spring-context : spring依赖
junit: 单元测试

开发人员定义类: 接口和实现类
类也可以没有接口。
接口和实现类定义:和没有spring一样。

创建spring的配置文件。 作用:声明对象。
把对象交给spring创建和管理。
使用表示对象声明,一个bean表示一个java对象。

使用容器中的对象。
创建一个表示spring容器的对象 ApplicationContext
从容器中,根据名称获取对象,使用getBean(“对象名称”)

✨创建接口和实现类

public interface SomeService {
    void doSOme();
}
public class SomeServiceImpl implements SomeService {
    @Override
    public void doSOme() {
        System.out.println("1");
    }
}

✨创建Spring的配置文件和声明bean

在 src/main/resources/目录现创建一个 xml 文件,文件名可以随意,但Spring 建议的名称为 applicationContext.xml。spring 配置中需要加入约束文件才能正常使用,约束文件是 xsd 扩展名。

<?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">
</beans>

spring标准的配置文件:
beans是跟标签,他的后面是约束文件说明。
beans里面是bean 声明。
bean :用于定义一个实例对象。一个实例对应一个 bean 元素。
id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean
与 Bean 间的依赖关系也是通过 id 属性关联的。
class:指定该 Bean 所属的类,注意这里只能是类,不能是接口。

下面我们开始创建Spring配置文件吧

<?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">

    <!--声明对象
       id:自定义对象名称,唯一值。(可以没有,spring可以提供默认名称)
       class:类的全限定名称,spring通过反射机制创建对象,不能是接口

       spring根据id,class创建对象, 把对象放入到spring的一个map对象。
       map.put(id,对象)
   -->
    <bean id="someService" class="youfei1_v.service.impl.SomeServiceImpl"/>
</beans>

✨创建spring容器对象


public class AppMain {

    public static void main(String args[]){
        //SomeService service  = new SomeServiceImpl();
        //service.doSome();

        //1.指定spring配置文件: 从类路径(classpath)之下开始的路径
        String config="beans.xml";
        //2.创建容器对象, ApplicationContext 表示spring容器对象。 通过ctx获取某个java对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //3.从容器中获取指定名称的对象, 使用getBean(“id”)
        SomeService service = (SomeService) ctx.getBean("someService");
        //4.调用对象的方法,接口中的方法
        service.doSome();

    }
}

测试类

//spring创建对象, 调用是类的那个方法?
    //默认是调用类的无参数构造方法。
    @Test
    public void test01(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

       // SomeService service  = (SomeService) ctx.getBean("someService");
     //   service.doSome();
     
		//按照类型获取对象,就不用转换对象了
        SomeService service = ctx.getBean(SomeService.class);
        service.doSome();
    }

ApplicationContext介绍
在这里插入图片描述

✨spring容器创建对象的特点

1.容器对象ApplicationContext:接口
通过ApplicationContext对象,获取要使用的其他iava对象,执行getBean(“bean的id”
2.spring默认是调用类的无参数构造方法,创建对象
3.spring读取配置文件,一次创建好所有的java对象,都放到map中。

public class SomeServiceImpl implements SomeService {

    /**
     * spring默认使用的无参数构造方法,创建对象。
     * 如果定义了有参数构造方法, 应该在定义无参数构造方法
     */
    public SomeServiceImpl() {
        System.out.println("SomeServiceImpl的无参数构造方法");
    }
}
//spring创建对象, 调用是类的那个方法?
    //默认是调用类的无参数构造方法。
    @Test
    public void test01(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //SomeService service = ctx.getBean(SomeService.class);
        //service.doSome();

        SomeService service  = (SomeService) ctx.getBean("someService");
        service.doSome();
    }

创建容器对象时会自动创建配置文件中的对象

   /**
      spring是在什么时候创建的对象?
      创建spring容器对象的时候,会读取配置文件,创建文件中声明的java对象。

      优点:
         获取对象的速度快, 因为对象已经创建好了

      缺点:
         占用内存
     */
    @Test
    public void test02(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //SomeService service  = (SomeService) ctx.getBean("someService");
        //service.doSome();
    }
/**
     *  spring容器创建对象, 一次创建几个 ?
     *  在创建容器(ApplicationContext)对象时,会把配置文件中的所有对象都创建出来(spring的默认规则)(有几个bean就创建几个对象)
     */
    @Test
    public void test03(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //SomeService service  = (SomeService) ctx.getBean("someService");
        //service.doSome();
    }

✨创建非自定义类的对象

Spring配置文件

<?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="mydate" class="java.util.Date" />

</beans>

测试

//让spring创建非自定义类的对象
    //有class就能让spring创建对象
    @Test
    public void test05(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Date date = (Date) ctx.getBean("mydate");
        System.out.println("date==="+date);
    }

✨创建没有接口的类的对象

//没有接口的类
public class OtherService {
    public void doOther(){
        System.out.println("执行OtherService的doOther()");
    }
}

Spring配置文件

<?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="otherService" class="com.service.OtherService"/>

</beans>

测试

    //有class就能让spring创建对象
    @Test
    public void test05(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        OtherService service = (OtherService) ctx.getBean("otherService");
        service.doOther();
    }

✨获取容器中对象的信息

    /**
     * 获取容器中对象的信息
     */
    @Test
    public void test04(){
        String config="beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        //获取容器中定义对象的数量 。一个bean对应一个对象
        int nums = ctx.getBeanDefinitionCount();
        System.out.println("容器中定义对象的数量=="+nums);

        //获取容器中定义的对象名称
        String names[] = ctx.getBeanDefinitionNames();
        for(String name:names){
            System.out.println("容器中对象的名称=="+name);
        }
    }

相关文章:

  • I2C知识大全系列二 —— I2C硬件及时序
  • Python基础入门(持续更新中)
  • 【预测模型-SVM预测】基于粒子群算法结合支持向量机SVM实现Covid-19风险预测附matlab代码
  • 初阶数据结构 二叉树常用函数 (二)
  • 【正点原子I.MX6U-MINI应用篇】4、嵌入式Linux关于GPIO的一些操作
  • 【C语言】解题训练
  • 【蓝桥杯国赛真题04】python输出平方 蓝桥杯青少年组python编程 蓝桥杯国赛真题解析
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • RabbitMQ 集群部署及配置
  • python @classmethod详解
  • JSP超市管理系统myeclipse定制开发SQLServer数据库网页模式java编程jdbc
  • FreeRTOS 软件定时器的使用
  • 软件测试培训到底值不值得参加?
  • IIC通信协议
  • FUP AMD300-27便携式拉曼食品安全分析仪 检测微痕量农兽药残留 非法添加
  • 07.Android之多媒体问题
  • Apache Pulsar 2.1 重磅发布
  • iOS 颜色设置看我就够了
  • JavaScript 奇技淫巧
  • JavaScript对象详解
  • SpiderData 2019年2月16日 DApp数据排行榜
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 看域名解析域名安全对SEO的影响
  • 那些被忽略的 JavaScript 数组方法细节
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 探索 JS 中的模块化
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • UI设计初学者应该如何入门?
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #Spring-boot高级
  • #微信小程序(布局、渲染层基础知识)
  • $.ajax中的eval及dataType
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (五)IO流之ByteArrayInput/OutputStream
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转)Oracle 9i 数据库设计指引全集(1)
  • *Django中的Ajax 纯js的书写样式1
  • .Family_物联网
  • .NET : 在VS2008中计算代码度量值
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .NET性能优化(文摘)
  • [100天算法】-目标和(day 79)
  • [4.9福建四校联考]
  • [AIGC] Nacos:一个简单 yet powerful 的配置中心和服务注册中心
  • [android学习笔记]学习jni编程
  • [DL]深度学习_Feature Pyramid Network
  • [Geek Challenge 2023] web题解
  • [Head First设计模式]策略模式
  • [Invalid postback or callback argument]昨晚调试程序时出现的问题,MARK一下