Spring学习个人记录

Spring详细笔记(个人纪录)

 

 

 

Spring的7大模块:

1.核心容器(Spring Core Container):1.Beans 2.Core 3.Context 4.Expression

2.Spring 上下文(Spring Context):

3.Spring AOP(面向切面编程):

4.Spring DAO:

5.Spring ORM:

6.Spring Web :

7.Spring MVC框架:

Spring相关Jar包:   1.aop     2.beans     3.context      4.core

IOC概念:

  1. 控制:传统的应用程序对象有程序本身控制创建,使用Spring后,把对象的创建和对象之间的调用过程,交给Spring管理

  2. 反转:程序本身不主动创建对象,变成被动接收对象

  3. 使用IOC目的是为了降低耦合度

IOC底层原理:

技术:1.XML解析、 工程设计模式、 反射

IOC过程:

第一步XML配置文件,配置创建的对象

<bean id = "dao" class = "com.SpringTest.spring5.UserDao"></bean>

第二部:有Service类和Dao类,创建工厂类

Class UserFactory{
public static UserDao getDao(){
    String classValue  = class属性值;  //1.XML解析
    Class clazz = Class.forName(classValue);  //2.通过反射创建对象
    return (UserDao)clazz.newInstance();
}
}

IOC(接口)

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

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

1.BeanFactory:IOC容器基本实现,是Spring中内置接口,一般不通过给开发人员使用

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

2.ApplicationsContextBeanFactory接口的子接口,提供了更多跟强大的功能,一般提供给开发人员使用

特点:在加载配置文件的时候就会把对象进行创建

3.ApplicationsContext实现类:

2个主要的实现类:

FileSystemXmlApplicationContext表示的是系统的盘符路径

ClassPathXmlApplicationContext表示的是类路径

 

IOC中Bean管理:

Bean管理指的是2种方式:1.Spring创建对象 2.Spring注入属性

Bean管理操作有2中方式:

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

  2. 基于注解方式实现

基于xml配置文件方式实现

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

1.XML配置文件创建对象

 

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

(2).bean标签有很多属性,bean标签帮助我们把对象进行创建,

  • id属性:唯一标识,他相当于对象名

  • class属性:类全路径(包类路径) ,相当于 ( new 类型() )

(3).创建对象的时候,默认也是执行无参构造方法完成对象创建。

2.XML方式注入属性

(1).DI:依赖注入,注入属性。是IOC的一种具体实现,但是需要在创建对象的基础之上完成。

Spring支持的2种注入方式:

1.使用Set方式进行注入

  • 创建类,定义属性和对应的set方法

  • 在spring配置文件配置对象创建,配置属性注入

   <bean id="User" class="com.SpringTest.spring5.UserAdd">   
       <!--使用property完成属性注入
         name:类里属性名称
         value:向属性注入的值
       -->
        <property name = "name" value = "李三"></property>
    </bean>

2.有参构造方法注入

  • 创建类,定义属性,创建属性对象的构造参数

  • 在Spring配置文件中进行配置

<!--有参构造注入属性-->    

<bean id="impl" class="com.SpringTest.spring5.UserImpl">      

 <constructor-arg name="str" value="测试"></constructor-arg>    

</bean>

 

p名称空间注入(了解)

1.使用p名称空间注入,可以简化XML配置方式

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

   xmlns:p = "http://www.springframework.org/schema/p"

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

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

1.字面量

  • null值 (Spring中设置空值方式)

<property name="book1">
    <null/>
</property>
  • 包含一些特殊符号

1.把<>进行转义(&lt; ,  &gt;) 2.把带特殊符号的内容写到CDATA

<property name="book1">
    <value><![CDATA[<<水浒>>]]></value>
</property>

2.注入属性—外部Bean

1.创建两个类service类和dao类

2.在service调用dao里面的方法

3.在Spring配置文件中进行配置(ref属性:用来给一个对象的属性设置值)

3.注入属性—内部Bean和级联赋值  

 <!--内部Bean-->
    <bean id="emp" class="com.SpringTest.spring5.bean.Emp">
    <!--2个基本属性-->
        <property name="EName" value="李四"></property>
        <property name="gender" value="男"></property>
    <!--设置对象属性-->
        <property name="dept">
            <bean id="dept" class="com.SpringTest.spring5.bean.Dept">
                <property name="dName" value="人事部门"></property>
            </bean>
        </property>
    </bean>
    <!--级联赋值,第二种写法-->
    <bean id="emp1" class="com.SpringTest.spring5.bean.Emp">
        <property name="EName" value="李四"></property>
        <property name="gender" value="男"></property>
        <property name="dept" ref="dept1"></property>
        <property name="dept.dName" value="安保部"></property>
    </bean>
    <bean id="dept1" class="com.SpringTest.spring5.bean.Dept">
    <!--<property name="dName" value="财务部"> </property>-->
    </bean>
​
    <!--级联赋值,第一种写法-->
    <bean id="emp2" class="com.SpringTest.spring5.bean.Emp">
        <property name="EName" value="李四"></property>
        <property name="gender" value="男"></property>
        <property name="dept" ref="dept2"></property>
    </bean>
    <bean id="dept2" class="com.SpringTest.spring5.bean.Dept">
        <property name="dName" value="财务部"> </property>
    </bean>

IOC操作Bean管理(FactoryBean)

1.Spring有两种类型Bean:普通Bean ,另一种工厂Bean

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

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

  • 创建类,让这个类作为工厂Bean,实现接口FactoryBean

  • 实现接口里面的方法,在实现的方法中定义返回的bean类型

IOC操作Bean管理(作用域)

1.在Spring中,可以设置bean实例是单实例还是多实例

2.在Spring中,在默认情况下,bean默认为单实例

3.如何设置多实例对象

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

  • scope属性值 scope="prototype"

  • 第一个值 默认值,singletion,表示单实例对象

  • 第二个值 protoyype,表示是多实例对象

  • singletionprotoyype的区别:

1.设置scope值是singletion时候,加载Spring配置文件时候就会创建单实例对象

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

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

1.生命周期:从对象创建到对象销毁的过程

2.bean生命周期:

1.通过构造器创建bean实例(无参构造)

2.为bean的属性设置值和堆其他bean引用 (调用set方法)

3.把bean的实例传递给bean后置处理器的方法(postProcessBeforeInitialization

4.调用bean的初始方法 (需要进行配置)

5.把bean的实例传递给bean后置处理器的方法(postProcessAfterInitialization

6.bean可以使用了 (对象获取到了)

7.当容器关闭时候,调用bean的销毁的方法 (需要进行配置销毁的方法)

添加后置处理器:

public class BeanOrder {
​
    private String name;
​
    public BeanOrder() {
        System.out.println("第一步,无参构造");
    }
​
    public void setName(String name) {
        this.name = name;
        System.out.println("第二步,调用set方法设置值");
    }
​
    //创建初始化方法
    public void initMethod(){
        System.out.println("第三步,执行初始化方法");
    }
    //创建销毁方法方法
    public void destroyMethod(){
        System.out.println("第五步,执行销毁方法");
    }

}
    
    @Test
    public void beanTest(){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("OrdBean.xml");
        BeanOrder order = context.getBean("orders", BeanOrder.class);
        System.out.println("第四步,获取到bean创建的BeanOrder对象");
        System.out.println(order);
        //手动销毁bean实例
        context.close();
    }
   @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;
    }
第一步,无参构造
第二步,调用set方法设置值
初始化之前执行的方法
第三步,执行初始化方法
初始化之后执行的方法
第四步,获取到bean创建的BeanOrder对象
com.SpringTest.spring5.Beans.BeanOrder@64f6106c
第五步,执行销毁方法

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

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

2.根据属性名称或者属性类型自动注入

    <!--实现自动装配
    bean标签属性autowire,配置自动装配
    autowire属性常用2个值:
    byName:根据属性名称注入,注入值bean的id值和类属性名称一样
    byType:根据属性类型注入
    -->

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

  1. 直接配置数据库信息

    • 配置druid连接池

    • 引入druid连接池 依赖jar包

         <!--直接配置连接池-->
        <bean id="source" class="com.alibaba.druid.pool.DruidDataSource">
    ​
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/javaweb"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>

  2. 通过引入外部属性文件配置数据库连接池

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

      driverClassName = com.mysql.cj.jdbc.Driver
      url = jdbc:mysql://localhost:3306/javaweb
      username = root
      password = root
    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">
​
    <!--配置连接池-->
    <!--引入外部配置文件-->
    <context:property-placeholder location="jdbc.properties"/>
    <bean id="source" class="com.alibaba.druid.pool.DruidDataSource">
​
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
​
    </bean>
​
</beans>

基于注解方式实现

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

1.什么是注解:

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

  2. 使用注解,注解作用在类上边,方法上边,属性上边

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

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

  • @Compon

  • @Service (一般用在Service )

  • @Controllter (一般用在Controllter)

  • @Repository (一般用在Repository)

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

  1. 引入依赖jar:spring-aop-5.2.9.RELEASE.jar

  2. 开启组件扫面

    • 如果开启扫面对个包,包之间用,隔开

    • 扫描包上层目录

    <context:component-scan base-package="com.SpringTest.annotation" ></context:component-scan>
  3. 创建类,在类上边添加创建对象注释

    • 在注释里面value属性值可以省略不写,默认的是类名称,首字母的小写

    • SpringDemo  -- springDemo

4.开启组件扫面细节配置

<!--表示进行默认的扫描
    扫描包含org.springframework.stereotype.Component的,其余的不进行扫描
-->
<context:component-scan base-package="com.SpringTest" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值