Spring5学习笔记(尚硅谷最新视频)持续更新中.......

发布时间:2022-07-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Spring5学习笔记(尚硅谷最新视频)持续更新中.......脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

 

SPRing5框架学习笔记

1.Spring框架概述

框架=设计模式+反射+注解

  1. Spring是轻量级的开JavaEE框架

  2. Spring可以解决企业应用开发的复杂性

  3. Spring有两个核心部分:IOC和AOP

    (1)IOC:控制反转,把创建对象的过程交给Spring进行管理

    (2)AOP:面向切面,不修改源代码进行功能增强

  4. Spring特点

    (1)方便解耦,简化开发

    (2)AOP编程的支持

    (3)方便程序的测试

    (4)方便和其他框架进行整合

    (5)方便进行事务操作

    (6)降低API开发难度

    Spring5入门案例

    1. 创建普通类,在这个类中创建一个普通方法

    public class User {    public void add(){        System.out.println("add.....");    }}
    1. 创建Spring配置文件,在配置文件配置创建的对象

      Spring配置文件使用XML格式

      <!-- 配置User对象创建-->    <bean id="user" class="Spring5.User"></bean>
  1. 进行测试代码编写

    public void testAdd() {    //加载spring5的配置文件        ApplicationContext context = new ClassPathXMLApplicationContext("bean1.xml");        //获取配置创建的对象        User user = context.getBean("user", User.class);        System.out.println(user);        user.add();}

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

IOC(概念和原理)

  1. 什么是IOC

    (1)控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理

    (2)使用IOC的目的:为了降低耦合度

    (3)做入门案例就是IOC实现

  2. IOC底层原理

    (1)xml解析、工厂模式、反射

  3. 画图讲解IOC底层原理

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

IOC(BeanFactory接口)

  1. IOC思想基于IOC容器完成,IOC容器底层就是对象工厂

  2. Spring提供IOC容器实现的两种方式:(两个接口)

    (1)BeanFactory:IOC容器基本实现,是Spring内部使用的接口

    • 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象

(2)ApplicationContext:BeanFactory接口的子接口,提供更多强大的功能,一般由开发人员进行使用

      * 加载配置文件的时候就会把在配置文件对象进行创建
  1. ApplicationContext接口有实现类

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

IOC操作Bean管理

  1. 什么是Bean管理

    (1)Bean管理指的是两个操作

    (1)Spring创建对象

    (2)Spring注入属性

  2. Bean操作管理有两种方式

    (1)基于xml配置文件方式实现

    (2)基于注解方式实现

IOC操作Bean管理(基于xml方式

  1. 基于xml方式创建对象

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

(1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建

(2)在bean标签有很多属性,介绍常用属性:

  • id:唯一标识

  • class:全限定类名

  • scoPE:单例多例选择

  • inIT-method:执行完对象创建的构造方法后立即执行的方法名

(3)创建对象的时候,默认页四执行无参数构造方法完成对象创建

  1. 基于xml方式注入属性

    (1)DI:依赖注入,就是注入属性

  2. 第一种注入方式:使用set方法注入

    (1)创建类,定义属性和对应的set方法

public class Book {    private String bname;    private String bauthor;    private String baddress;    public void setBname(String bname) {        this.bname = bname;    }    public void setBauthor(String bauthor) {        this.bauthor = bauthor;    }}

(2)在spring配置文件中配置对象创建,配置属性注入

 <!--set方法注入属性-->    <bean id="book" class="nuc.edu.lix.Book" >        <property name="bname" value="44"></property>        <property name="bauthor" value="444"></property>    </bean>
  1. 第二种注入方式:使用有参构造进行注入

    (1)创建类,定义属性,创建属性对应构造方法

    public class Orders {    private String oname;    private String address;    public Orders(String oname, String address) {        this.oname = oname;        this.address = address;    }}

    (2)在spring文件中进行配置

    <!--有参构造注入属性-->    <bean id="orders" class="nuc.edu.lix.Orders">        <constructor-arg name="oname" value="脑"></constructor-arg>        <constructor-arg name="address" value="China"></constructor-arg>    </bean>
  2. p名称空间注入

    (1)使用p名称空间注入,可以简化基于xml配置方式

    第一步 添加p名称空间在配置文件中

    <!--添加p名称空间在配置文件中-->
    <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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    

    第二步:进行属性注入,在bean标签里面进行操作

    <!--使用p名称空间注入-->
        <bean id="book" class="nuc.edu.lix.Book" p:bname="jiuyang" p:bauthor="ssws">
        </bean>

    IOC操作Bean管理(xml注入其他类型属性)

    1. 字面量

      (1)null值

      <property name="address">
          <null/>
      </property>

      (2)属性值包含特殊符号

      方法:

      1. 把<>进行转义&lt;&gt;

        1. 把带特殊符号内容写道CDATA

          <property name="address">
              <value><![CDATA[<<南京>>]]></value>
          </property>
    2. 注入属性--外部bean

      (1)创建两个类service类和DAO

      (2)在service调用dao里面的方法

      (3)在spring配置文件中进行配置

      Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  3. 注入属性--内部bean

    (1)一对多关系:部门和员工

    部门是一,员工是多

    (2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  1. 注入属性--级联赋值

    (1)第一种写法

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

    (2)第二种写法(需要生成dept的get方法)

<bean id="emp" class="nuc.edu.lix.bean.Emp">
    <property name="ename" value="locy"></property>
    <property name="gender" value="女"></property>
    <property name="dept" ref="dept"></property>
    <property name="dept.dname" value="技部"></property>

</bean>

<bean id="dept" class="nuc.edu.lix.bean.Dept">
    <property name="dname" value="财务部"></property>
</bean>

IOC操作Bean管理(xml注入集合属性)

  1. 注入数组类型属性

  2. 注入List集合类型属性

  3. 注入Map集合类型属性

    (1)创建类,定义数组、list、map、set、类型属性,生成对应的set方法

    public class BeanCollection {
        private String[] numbers;
        private List<String> list;
        private Map<String,String> map;
        private Set<String> set;
    
        private List<;money> lists;
    
        public void setLists(List<Money> lists) {
            this.lists = lists;
        }
    
        public void setNumbers(String[] numbers) {
            this.numbers = numbers;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    
        public void setSet(Set<String> set) {
            this.set = set;
        }
    
        public void test() {
            System.out.println(Arrays.toString(numbers));
            System.out.println(list);
            System.out.println(map);
            System.out.println(set);
            System.out.println(lists);
        }
    }

    (2)在spring配置文件进行配置

<bean id="beanCollection" class="nuc.edu.ljx.bean.BeanCollection">
    <property name="numbers">
        <array>
            <value>赵丽颖</value>
            <value>杨幂</value>
        </array>
    </property>

    <property name="list">
        <list>
            <value>小姐妹</value>
            <value>小兄弟</value>
        </list>
    </property>

    <property name="map">
        <map>
            <entry key="小姐妹" value="刘诗诗"></entry>
            <entry key="小兄弟" value="唐嫣"></entry>
        </map>
    </property>

    <property name="set">
        <set>
            <value>关晓彤</value>
            <value>鹿晗</value>
        </set>
    </property>

</bean>
  1. 在集合里面设置对象类型值

<bean id="money1" class="nuc.edu.ljx.bean.Money">
    <property name="money" value="20000000"></property>
</bean>
<bean id="money2" class="nuc.edu.ljx.bean.Money">
    <property name="money" value="20000001"></property>
</bean>
   ~~~java

<property name="lists"> <list> <ref bean="money1"></ref> <ref bean="money2"></ref> </list> </property><!--注入list集合类型值是对象--> ~~~

  1. 把集合注入部分提取出来

    (1)在spring配置文件中引入名称空间util

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    
        <util:list id="schoolList">
            <value>小学</value>
            <value>中学</value>
            <value>大学</value>
        </util:list>
    
        <bean id="school" class="nuc.edu.ljx.bean.School">
            <property name="name" ref="schoolList"></property>
        </bean>
    </beans>

IOC操作Bean管理(FactoryBean)

  1. Spring有两种类型bean,一种是普通bean,另外一种是工厂bean(FactoryBean)

  2. 普通bean:在配置文件中定义bean类型就是返回类型

  3. 工厂bean:在配置文件中定义bean的类型可以和返回类型不一样

    第一步:创建类,让这个类作为工厂bean,实现接口FactoryBean

    第二步:实现接口里的方法,在实现的方法中定义返回的bean类型

    public class MyBean implements FactoryBean<School> {
        //定义返回bean
        @override
        public School getObject() throws Exception {
            School school = new School();
            school.setId("1913040202");
            return school;
    
        }
    
        @Override
        public Class<School> getObjectType() {
            return null;
        }
    }
<bean id="myBean" class="nuc.edu.ljx.factorybean.MyBean"></bean>
@Test
public void testBean3() {
    //加载spring5的配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    //获取配置创建的对象
    School school = context.getBean("myBean", School.class);

    System.out.println(school.getId());;
}

IOC操作Bean管理(bean作用域)

  1. 在spring里面,设置创建bean实例是单实例还是多实例

  2. 在spring里面,默认情况下,bean是单实例对象

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  1. 如何设置单实例还是多实例

    (1)在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例

    (2)scope属性值

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

(3)singleton和prototype区别

第一:s..是单实例,p..是多实例

第二:设置scope值为singleton时候,加载spring配置文件时候就会创建单实例对象

设置scope值为prototype时候,不是在加载spring配置文件时候创建对象,而是在调用getBean方法的时候创建多实例对象

IOC操作Bean管理(bean生命周期)

  1. 生命周期

    (1)从对象创建到对象销毁的过程

  2. bean生命周期

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  3. 演示生命周期

    public class Orders {
        private String string;
    
        public Orders() {
            System.out.println("第一步:执行构造函数方法");
        }
    
        public void setString(String string) {
            this.string = string;
            System.out.println("第二步:执行赋值操作");
        }
    
        public void initMethod() {
            System.out.println("第三步:执行初始化方法");
        }
    
        public void destoryMethod() {
            System.out.println("第五步:执行销毁方法");
        }
    }

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  1. bean的后置处理器,bean生命周期有七步

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  1. 演示添加后置处理器效果

    (1)创建类,实现接口BeanPostProcessor,创建后置处理器

    public class InitBean implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeinitialization(Object bean, String beanName) throws BeansException {
            System.out.println("在初始化方法之前执行");
    
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    
            System.out.println("在初始化方法之后执行");
            return bean;
        }
    }
    
    <bean id="initBean" class="nuc.edu.ljx.bean.InitBean"></bean>

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

IOC操作Bean管理(xml自动装配)

  1. 什么是自动装配

    (1)根据指定装配原则(属性名称或者属性类型),spring自动将匹配的属性值进行注入

  2. 演示自动装配过程

    (1)根据属性名称自动注入

    <!--自动注入属性配置:autowire
    以下注释掉的为例子:
    由于不想新建类,太麻烦,所以读者看作School中有一个Orders类型的属性
    
    autowire有两种方式:byName;byType:根据属性的名称进行注入,根据属性的类型进行注入
    
    -->
    
    <bean id="school" class="nuc.edu.ljx.bean.School" autowire="byName"></bean>
    <bean id="orders" class="nuc.edu.ljx.bean.Orders"></bean>

(2)根据属性类型自动注入

<!--自动注入属性配置:autowire
以下注释掉的为例子:
由于不想新建类,太麻烦,所以读者看作School中有一个Orders类型的属性

autowire有两种方式:byName;byType:根据属性的名称进行注入,根据属性的类型进行注入

-->

<bean id="school" class="nuc.edu.ljx.bean.School" autowire="byName"></bean>
<bean id="orders" class="nuc.edu.ljx.bean.Orders"></bean>

IOC操作Bean管理(外部属性文件)

  1. 直接配置数据库信息

    (1)配置德鲁伊连接池

    (2)引入德鲁伊连接池依赖jar包

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

<!--直接配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverclassname" value="com.MySQL.jdbc.Driver"></property>
    <property name="url" value="jdbc:mySQL://localhost:3306/userDb"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>

</bean>
  1. 引入外部属性文件配置数据库连接池

    (1)创建外部属性文件,properties格式文件,写数据库信息

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

(2)把外部properties属性文件引入到spring配置文件中

*** 引入context名称空间

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

*** 在spring配置文件中使用标签引入外部属性文件

<!--外部属性文件引入该配置文件
1.引入context名称空间
2.引入外部属性文件
3.配置连接池
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${prop.driverClass}"></property>
    <property name="url" value="${prop.url}"></property>
    <property name="username" value="${prop.userName}"></property>
    <property name="password" value="${prop.password}"></property>

</bean>

IOC操作Bean管理(基于注解方式)

  1. 什么是注解

    (1)注解是代码特殊注解,格式:@注解名称(属性名称=属性值,属性名称=属性值...)

    (2)使用注解,注解作用在类上面,方法上面,属性上面

    (3)使用注解的目的:简化xml配置

  2. Spring针对Bean管理中创建对象提供注解

    (1)@component

    @Service

    @Controller

    @Repository

    *** 上面四个注解功能是一样的,都可以用来创建bean实例

  3. 基于注解方式实现对象创建

    第一步 引入依赖

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

第二步 开启组件扫描

<!--开启组件扫描
    1  如果扫描多个包,多个包使用逗号隔开
    2  扫描包上层目录
-->

<context:component-scan base-package="nuc.edu.ljx"></context:component-scan>

第三步 创建类,在类上面添加创建对象注解

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  1. 开启组件扫描细节配置

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

  1. 基于注解方式实现属性注入(不需要添加set方法)

    (1)@Autowired:根据属性类型进行自动装配

    第一步:把service和dao对象创建,在service和dao类添加创建对象注解

    第二步:在service注入dao对象,在service类添加dao类型属性,在属性上使用注解

    Spring5学习笔记(尚硅谷最新视频)持续更新中.......

(2)@Qualifier:根据名称进行注入

这个@Qualifier注解的使用,和上面@Autowired一起使用

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

(3)@Resource:可以根据类型注入,可以根据名称注入

Spring5学习笔记(尚硅谷最新视频)持续更新中.......

(4)@Value:注入普通类型属性

@Value(value="abc")
private String name;
  1. 完全注解开发

    (1)创建配置类,替代xml配置文件

    @configuration//作为配置类,替代xml配置文件
    @ComponentScan(basePackages={"nuc.edu.ljx"})
    public class SpringConfig {}

    (2)编写测试类

     

脚本宝典总结

以上是脚本宝典为你收集整理的Spring5学习笔记(尚硅谷最新视频)持续更新中.......全部内容,希望文章能够帮你解决Spring5学习笔记(尚硅谷最新视频)持续更新中.......所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。