• spring的学习【1】


    一 Spring概述

    1.1 简介

    官网
    官方下载地址
    GitHub

    1.2 优点

    • Spring是一个开源免费的框架 , 容器 .
    • Spring是一个轻量级的框架 , 非侵入式的 .
    • 控制反转 IoC , 面向切面 Aop
    • 对事物的支持 , 对框架的支持
      Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架)

    1.3 组成

    在这里插入图片描述

    • Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器
      定义了创建、配置和管理 bean 的方式 。
      在这里插入图片描述
    • 核心容器:==核心容器提供 Spring 框架的基本功能。==核心容器的主要组件是 BeanFactory ,它是工厂模式的实现。 BeanFactory 使用控制反转(IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
    • Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
    • Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向切面的编程功能 , 集成到了 Spring框架中。所以,可以很容易地使 Spring 框架管理任何支持 AOP的对象。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖组件,就可以将声明性事务管理集成到应用程序中。
    • Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
    • Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
    • Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
    • Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText和 POI。

    1.4 拓展

    Spring Boot与Spring Cloud

    • Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务;
    • Spring Cloud是基于Spring Boot实现的;
    • Spring Boot专注于快速、方便集成的单个微服务个体,Spring Cloud关注全局的服务治理框架;
    • Spring Boot使用了约束优于配置的理念,很多集成方案已经帮你选择好了,能不配置就不配置 ,Spring Cloud很大的一部分是基于Spring Boot来实现,Spring Boot可以离开Spring Cloud独立使用开发项目,但是Spring Cloud离不开Spring Boot,属于依赖的关系。
    • SpringBoot在SpringClound中起到了承上启下的作用,如果你要学习SpringCloud必须要学习SpringBoot。
      在这里插入图片描述

    二 IOC基础

    2.1 IOC本质

    • 控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法。。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方。

    狂神认为所谓控制反转就是:获得依赖对象的方式反转了。
    在这里插入图片描述

    • IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解,
      新版本的Spring也可以零配置实现IoC。
    • Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从Ioc容器中取出需要的对象。
      在这里插入图片描述

    采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为
    一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

    • 控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)

    三 分析HellloSpring

    3.1 导入jar包

    注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项

    <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.18version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.2 编写代码

    • 实体类
    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
    • 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.kuang.pojo.Hello">
    	<property name="name" value="Spring"/>
    bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 测试
    @Test
    public void test(){
    	//解析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

    3.3 分析并思考这个案例

    • Hello 对象是谁创建的 ?
      • hello 对象是由Spring创建的
    • Hello 对象的属性是怎么设置的 ?
      • hello 对象的属性是由Spring容器设置的
    • 这个过程就叫控制反转 :
      • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是
        由Spring来创建的
      • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
    • 依赖注入 : 就是利用set方法来进行注入的.
      IOC是一种编程思想,由主动的编程变成被动的接收

    四 IOC创建对象方式

    4.1 通过无参构造方法来创建

    package com.yang.pojo;
    
    /**
     * @author 缘友一世
     * @date 2022/7/25-21:54
     */
    public class User {
        private String name;
        public User() {
            System.out.println("User的无参构造!");
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name=name;
        }
        public void show() {
            System.out.println("name="+name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        
       <bean id="user" class="com.yang.pojo.User">
            <constructor-arg name="name" value="greatBoy"/>
        bean>
        
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    @Test
    public void test(){
    ApplicationContext context = new
    ClassPathXmlApplicationContext("beans.xml");
    //在执行getBean的时候, user已经创建好了 , 通过无参构造
    User user = (User) context.getBean("user");
    //调用对象的方法 .
    user.show();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 通过无参函数构造方法来创建
    • 在调用show方法之前,User对象已经通过无参构造初始化了!

    4.2 beans.xml的三种编写方式

     
        <bean id="user" class="com.yang.pojo.User">
            <constructor-arg index="0" value="java"/>
        bean>
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
        
        <bean id="user" class="com.yang.pojo.User">
            <constructor-arg type="java.lang.String" value="java"/>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        
        <bean id="user" class="com.yang.pojo.User">
            <constructor-arg name="name" value="greatBoy"/>
        bean>
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.3 通过有参构造方法来创建

    • User.java
    package com.yang.pojo;
    
    /**
     * @author 缘友一世
     * @date 2022/7/25-21:54
     */
    public class User {
        private String name;
        public User(String name) {
            this.name=name;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name=name;
        }
        public void show() {
            System.out.println("name="+name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • beans.xml
    <bean id="user" class="com.yang.pojo.User">
            <constructor-arg name="name" value="greatBoy"/>
        bean>
        <bean id="userT" class="com.yang.pojo.UserT">
    
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    test.java

    @Test
    public void testT(){
    	ApplicationContext context = new
    	ClassPathXmlApplicationContext("beans.xml");
    	UserT user = (UserT) context.getBean("userT");
    	user.show();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五 Spring配置

    5.1 别名

    在这里插入图片描述

    5.2 Bean的配置

    在这里插入图片描述

    5.3 import

    • 团队的合作通过import来实现 .
     <import resource="{path}/beans.xml"/>
    
    • 1

    六 依赖注入(Dependency Injection,DI)

    • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
    • ==注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 ==.

    6.1 构造器注入

    • 四 IOC创建对象方式 刚讲过

    6.2 set注入(important)

    • 要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写
    • Address.java
    package com.yang.pojo;
    
    /**
     * @author 缘友一世
     * @date 2022/7/26-14:17
     */
    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
    • 20
    • 21
    • 22
    • 23
    • 24
    • Student.java
    package com.yang.pojo;
    
    import java.util.*;
    
    /**
     * @author 缘友一世
     * @date 2022/7/26-14:17
     */
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbies;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties info;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String[] getBooks() {
            return books;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public List<String> getHobbies() {
            return hobbies;
        }
    
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
    
        public Map<String, String> getCard() {
            return card;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public Set<String> getGames() {
            return games;
        }
    
        public void setGames(Set<String> games) {
            this.games = games;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        public Properties getInfo() {
            return info;
        }
    
        public void setInfo(Properties info) {
            this.info = info;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", address=" + address.toString() +
                    ", books=" + Arrays.toString(books) +
                    ", hobbies=" + hobbies +
                    ", card=" + card +
                    ", games=" + games +
                    ", wife='" + wife + '\'' +
                    ", info=" + info +
                    '}';
        }
    }
    
    
    • 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
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
       <bean id="address" class="com.yang.pojo.Address">
           <property name="address" value="开封"/>
       bean>
       <bean id="student" class="com.yang.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="hobbies">
               <list>
                   <value>开挂value>
                   <value>修炼value>
                   <value>打怪兽value>
                   <value>讲课value>
               list>
           property>
           
           <property name="card">
               <map>
                   <entry key="身份证" value="666"/>
                   <entry key="银行卡" value="888"/>
               map>
           property>
           
           <property name="games">
               <set>
                   <value>LOLvalue>
                   <value>COCvalue>
                   <value>BOBvalue>
               set>
           property>
           
           <property name="wife">
               <null/>
           property>
           
           <property name="info">
               <props>
                   <prop key="driver">666888prop>
                   <prop key="url">xxxprop>
                   <prop key="username">rootprop>
                   <prop key="password">1234567890prop>
               props>
           property>
       bean>
    beans>
    
    • 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
    • 62
    • test.java
    import com.yang.pojo.Student;
    import com.yang.pojo.User;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author 缘友一世
     * @date 2022/7/26-14:28
     */
    public class MyTest04 {
        public static void main(String[] args) {
            ApplicationContext Context = new ClassPathXmlApplicationContext("beans04.xml");
            Student student = (Student) Context.getBean("student");
            System.out.println(student.toString());
            /*
            * Student{name='酸梅汤小王子',
            *  address=Address{address='开封'},
            * books=[唐三, 萧炎, 罗峰, 狂神],
            * hobbies=[开挂, 修炼, 打怪兽, 讲课],
            * card={身份证=666, 银行卡=888},
            * games=[LOL, COC, BOB], wife='null',
            * info={password=1234567890, url=xxx, driver=666888, username=root}
            * }
    
             * */
        }
    }
    
    • 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

    6.3 拓展的注入方式【p&c】

    • User.java【这里没有有参构造器】
    package com.yang.pojo;
    
    /**
     * @author 缘友一世
     * @date 2022/7/26-15:01
     */
    public class User {
        private String name;
        private int age;
    
        public User() {
        }
    
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 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

    benans.xml

    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="user" class="com.yang.pojo.User" p:name="白小纯" p:age="18"/>
    
        
        <bean id="user2" class="com.yang.pojo.User" c:name="黑小纯" c:age="18" scope="prototype"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • test
    @Test
    public void test02(){
    	ApplicationContext context = new
    	ClassPathXmlApplicationContext("applicationContext.xml");
    	User user = (User) context.getBean("user");
    	System.out.println(user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6.4 Bean的作用域【Bean scopes】

    在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,
    bean就是由IoC容器初始化、装配及管理的对象 .

    在这里插入图片描述

    类型说明
    singleton在Springloc容器中仅存在一个Bean实例,Bean以单例方式存在,默认值
    prototype每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean0时,相当于执行newXxxBean()
    request每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
    session同一个HTTPSession共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationCont

    几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。

    6.4.1 The Singleton Scope【单例模式,默认模式】

    在这里插入图片描述

    
        <bean id="user" class="com.yang.pojo.User" p:name="白小纯" p:age="18" scope="singleton"/>
    
    • 1
    • 2
     @Test
        public void test1() {
            ApplicationContext context = new ClassPathXmlApplicationContext("UserBeans.xml");
            User user01 = context.getBean("user", User.class);
            User user02 = context.getBean("user", User.class);
            System.out.println(user01==user02);//true
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6.4.2 The Prototype Scope【原型模式】

    • 每次从容器中get的时候,都会产生一个新的对象
      在这里插入图片描述
    
        <bean id="user2" class="com.yang.pojo.User" c:name="黑小纯" c:age="18" scope="prototype"/>
    
    • 1
    • 2
     public void test1() {
            ApplicationContext context = new ClassPathXmlApplicationContext("UserBeans.xml");
            User user1 = context.getBean("user2", User.class);
            User user2 = context.getBean("user2", User.class);
            System.out.println(user1==user2);//false
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 其余的request、session、application、这些个只能在web开发中使用!
  • 相关阅读:
    Idea debug 调试运行慢
    iPhone垃圾清理器:AnyMP4 iOS Cleaner for mac
    如何将谷歌浏览器设置为默认浏览器
    Android codec2 视频框架 之输入buffer
    Golang实现小型CMS内容管理功能(一):Gin框架搭配Gorm实现增删查改功能
    TinyXml库的用法之xml文件节点遍历与节点删除
    当下社会和经济形势概述
    Go语言入门心法(三): 接口
    rsync远程同步
    JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率
  • 原文地址:https://blog.csdn.net/yang2330648064/article/details/126024010