• Spring框架原理 | IOC/DI | Bean


    在这里插入图片描述

    💗wei_shuo的个人主页

    💫wei_shuo的学习社区

    🌐Hello World !


    ☢Spring框架

    在这里插入图片描述

    Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。

    • 目的:解决企业应用开发的复杂性
    • 功能:使用基本的JavaBean代替EJB
    • 范围:任何Java应用
    Spring + Spring MVC = Spring Boot

    ☢Spring架构图

    💨组件介绍:

    💫核心容器(IOC)

    Core、Beans、Context、EL模块

    • Core模块:封装框架依赖的最底层部分,包括资源访问、类型转换及一些常用工具类
    • Beans模块:提供了框架的基础部分、包括反转控制和依赖注入。其中Beans Factory是容器核心,本质是"工厂设计模式"的实现,而且无需编程实现"单例设计模式",单例完全由容器控制,而且提倡面向接口编程,而非面向实现编程;所有应用程序对象及对象间关系由框架管理,从而真正把你从程序逻辑中吧维护对象之间的依赖关系提取出来,所有这些依赖关系都由BeansFactory来维护
    • Context模块:以Core和Beans为基础,集成Beans模块功能并添加资源绑定、数据验证、国际化、JavaEE支持、容器周期、事件传播等;核心接口是ApplicationContext
    • EL模块:提供强大的表达语言支持,支持访问和修改属性值,方法调用,支持访问及修改数组、容器和索引器,命名变量,支持算数和逻辑运算,支持从Spring容器获取Bean,它也支持列表投影,选择和一般的列表聚合等
    • Test模块:Spring支持Junit和TestNG测试框架,而且还额外提供一些基于Spring的测试功能,如测试Web框架时,模拟Http请求的功能

    💫面向切面编程模块(AOP)

    面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

    OOP:面向对象程序设计(Object Oriented Programming)作为一种新方法,其本质是以建立模型体现出来的抽象思维过程和面向对象的方法。模型是用来反映现实世界中事物特征的。任何一个模型都不可能反映客观事物的一切具体特征,只能对事物特征和变化规律的一种抽象,且在它所涉及的范围内更普遍、更集中、更深刻地描述客体的特征。通过建立模型而达到的抽象是人们对客体认识的深化。

    💫数据访问模块(Data Access/Integration)

    通过Spring提供的数据访问模块可以很方便地集成第三方的ORM框架,也可以使用Spring提供的JDBC模块实现数据的持久化

    💫Web模块(Web)

    Web模块建立在应用程序上下文模块之上,为基于Web的应用程序提供上下文,所以,Spring框架支持与Struts的集成。Web模块简化了处理多部分请求及将请求参数绑定到域对象的工作,并提供MVC的实现,即Spring MVC 这是一个全功能的构建Web应用程序的MVC实现。通过策略接口,MVC框架变为高度可配置的。MVC容纳了大量视图技术,包括JSP、Velocity、iText和POI

    💫单元测试模块(Test)

    单元测试模块提供了JUnit及TestNG,用于实现对应用的单元测试的集成测试,同时引入mock以实现对Web模块的单元测试

    💫思维转变(传统开发—>IOC容器)

    • 传统开发

    程序控制对象:如果有多种实现类,需要改变实现类,繁杂,无法适应变更

    public class UserServiceImpl implements UserService{
    
        private UserDao userDao = new UserDaoImpl();
    
        public void getUser(){
            userDao.getUser();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • IOC容器

    set注入,使程序更加灵活,降低耦合性,这就是IOC原型

    public class UserServiceImpl implements UserService{
    
        private UserDao userDao;	
    
        //通过构造器函数参数,让容器把创建好的依赖对象注入setUserDao,当然也可以使用setter方法进行注入
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        @Override
        public void getUser() {
            userDao.getUser();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ☢Spring IOC/DI

    Spring IOC:控制反转

    💨第一个Spring程序

    案例:Hello Spring

    • 编写Hello实体类
    package com.wei.pojo;
    
    public class Hello {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show() {
            System.out.println("Hello" + "," + name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 编写Spring文件,命名为beans.xml
    
    <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="hello" class="com.wei.pojo.Hello">
            <property name="name" value="Spring"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 编写测试类
    import com.wei.pojo.Hello;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        public static void main(String[] args) {
            //解析beans.xml文件,生成管理相应的Bean对象
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            //getBean:参数即为Spring配置文件中的bean的id
            Hello hello = (Hello) context.getBean("hello");     //强制类型转换
            hello.show();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    总结:

    • bean就是Java对象,由Spring创建和管理
    	<bean id="hello" class="com.wei.pojo.Hello">
            <property name="name" value="Spring"/>
        </bean>
    
    • 1
    • 2
    • 3
    • 解析beans.xml文件,生成管理相应的Bean对象
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
    • 1
    • getBean:参数即为Spring配置文件中的bean的id
     Hello hello = (Hello) context.getBean("hello");     //强制类型转换
    
    • 1

    💨Spring配置说明

    • id:bean的唯一标识符,也就是相当于对象名
    • class:bean对象所对应的全限定名:包名.类型
    • name:别名
    	<bean id="hello" class="com.wei.pojo.Hello">
            <property name="name" value="Spring"/>
        bean>
    
    • 1
    • 2
    • 3

    💨import

    • import,一般用于团队开发中,可以将多个配置文件导入合并为一个
    • 将所有xml文件导入到applicationContext.xml中,方法如下
    <import resource="beans1.xml"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xml"/>
    
    • 1
    • 2
    • 3

    💨IOC创建对象方式

    无参构造(默认)

    • 默认使用无参构造创建对象
        public User(){
            System.out.println("User的无参构造");
        }
    
    • 1
    • 2
    • 3
        <bean id="user" class="com.wei.pojo.User">
    		<property name="name" value="秦疆"/>-->
        bean>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    带参构造

    • 第一种方法:下标赋值
        public User(String name){
            this.name=name;
        }
    
    • 1
    • 2
    • 3
    
        <bean id="user" class="com.wei.pojo.User">
            <constructor-arg index="0" value="wei_shuo"/>
        bean>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 第二种方法:类型创建
        public User(String name){
            this.name=name;
        }
    
    • 1
    • 2
    • 3
        <bean id="user" class="com.wei.pojo.User">
            <constructor-arg type="java.lang.String" value="秦疆"/>
        bean>
    
    • 1
    • 2
    • 3
    • 第三种方法:参数名称
        public User(String name){
            this.name=name;
        }
    
    • 1
    • 2
    • 3
        <bean id="user" class="com.wei.pojo.User">
            <constructor-arg name="name" value="秦疆"/>
        bean>
    
    • 1
    • 2
    • 3

    💨DI依赖注入

    依赖:bean对象的创建依赖于容器

    注入:bean对象中的所有属性,容器注入

    • 构造器注入

      即无参构造/带参构造注入

    • Set注入

    //复杂类型
    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "Address{" +
                    "address='" + address + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    //真实测试对象
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbys;
        private Map<String,String> card;
        private Set<String> game;
        private String wife;
        private Properties info;
        //以及set/get方法  toString实现方法 此处省略
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
       <bean id="student" class="com.wei.pojo.Student">
            
            <property name="name" value="秦疆"/>
        bean>
    
    • 1
    • 2
    • 3
    • 4

    💫完整注入信息

    • Studnet类
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbys;
        private Map<String,String> card;
        private Set<String> game;
        private String wife;
        private Properties info;
        //以及set/get方法  toString实现方法 此处省略
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • Address类
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "Address{" +
                    "address='" + address + '\'' +
                    '}';
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • beans.xml配置
      <bean id="address" class="com.wei.pojo.Address">
            <property name="address" value="武汉"/>
        bean>
    
        <bean id="student" class="com.wei.pojo.Student">
            
            <property name="name" value="秦疆"/>
    
            
            <property name="address" ref="address"/>
    
            
            <property name="books">
                <array>
                    <value>红楼梦value>
                    <value>西游记value>
                    <value>水浒传value>
                    <value>三国演义value>
                array>
            property>
    
            
            <property name="hobbys">
                <list>
                    <value>听音乐value>
                    <value>看电影value>
                    <value>听书value>
                list>
            property>
    
            
            <property name="card">
                <map>
                    <entry key="学生编号" value="123"/>
                    <entry key="书籍编号" value="456"/>
                map>
            property>
    
            
            <property name="game">
                <set>
                    <value>英雄联盟value>
                    <value>Apexvalue>
                set>
            property>
    
            
            <property name="wife">
                <null/>
            property>
    
            
            <property name="info">
                <props>
                    <prop key="学号">2000prop>
                    <prop key="性别">prop>
                    <prop key="姓名">小明prop>
                props>
            property>
    
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 测试类
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = (Student) context.getBean("student");
            System.out.println(student.toString());
        }
    }
    
    
    /*
    输出结果:
    Student{
    	name='秦疆', 
    	address=Address{address='武汉'}, 
    	books=[红楼梦, 西游记, 水浒传, 三国演义], 
    	hobbys=[听音乐, 看电影, 听书], 
    	card={
    		学生编号=123, 
    		书籍编号=456
    	}, 
    	game=[英雄联盟, Apex], wife='null', 
    	info={
    		学号=2000, 
    		性别=男, 
    		姓名=小明
    		}
    }
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    💨Bean作用域

    • singleton 单实例(单例)(默认)   ----全局有且仅有一个实例
    <bean id="user2" class="com.wei.pojo.User" c:name="wei_shuo" c:age="18" scope="singleton"/>
    
    • 1
    • prototype 多实例(多例)   ---- 每次获取Bean的时候会有一个新的实例
     <bean id="user2" class="com.wei.pojo.User" c:name="wei_shuo" c:age="18" scope="prototype"/>
    
    • 1

    以下在web开发中使用

    • request
    • session
    • application
    • websocket

    💨Bean自动装配(Autowire)

    自动装配是Spring满足bean依赖的方式,Spring会在上下文中自动寻找,自动给bean装配属性

    • xml中显示装配
    • Java中显示配置
    • 隐式自动装配bean

    案例实现:

    • 一个人(people)有两个宠物(dog/cat)
    • 狗实现方法wang
    • 猫实现方法mioa
    • Dog类
    public class Dog {
        public void shout(){
            System.out.println("wang");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • Cat类
    public class Cat {
        public void shout(){
            System.out.println("miao");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • people类
    public class People {
        private Cat cat;
        private Dog dog;
        private String name;
        /*
        get/set方法 
        toString方法
        需要写,笔者为了不影响文章美观此处省略……
        */ 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • beans.xml
      • byName:会自动在容器上下文中查找,和自己对象set方法后面的值(dog/cat)对应的beanid,如果beanid满足则自动装配
      • byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean,如果类型全局唯一满足则自动装配
        
        <bean id="cat" class="com.wei.pojo.Cat"/>
        <bean id="dog" class="com.wei.pojo.Dog"/>
    
        
        <bean id="people" class="com.wei.pojo.People" autowire="byName">	
    
            <property name="name" value="wei_shuo"/>
    
            
    	
    	
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    💨Spring框架xml配置中属性 ref 与 value的区别

    • 属性 ref :对象引用类型,代表引用这个对象
    • 属性 value:字符串引用类型,代表引入的这个对象名字的字符串

    🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝冲冲冲🤞

    在这里插入图片描述

  • 相关阅读:
    几个Caller-特性的妙用
    【C#】Mapster对象映射的使用
    【Django使用】django经验md文档10大模块。第4期:Django数据库增删改查
    Jwt 介绍
    sensor曝光时间
    数据结构篇——KMP算法
    NeRF数据集
    数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
    [学习笔记]SQL server完全备份指南
    微服务之网关
  • 原文地址:https://blog.csdn.net/weixin_62765017/article/details/127356408