Spring知识概括

Spring知识概括

  • Spring概述
  • IOC 容器
  • SpEL表达式语言
  • AOP
  • JdbcTemplate
  • 事务管理
  • Spring5 框架新功能

Spring概述

Spring概述:

  • Spring 是轻量级的开源的 JavaEE 框架
  • Spring 可以解决企业应用开发的复杂性
  • Spring 有两个核心部分:IOC 和 Aop
    ①IOC:控制反转,把创建对象过程交给 Spring 进行管理
    ②Aop:面向切面,不修改源代码进行功能增强
  • Spring 特点:
    ①方便解耦,简化开发
    ②Aop 编程支持
    ③方便程序测试
    ④方便和其他框架进行整合
    ⑤方便进行事务操作
    ⑥降低 API 开发难度

IOC 容器

IOC简述:

  • 控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理
  • 使用 IOC 目的:为了耦合度降低
  • IOC 底层原理:
    ①xml 解析
    ②工厂模式
    ③反射
  • 图解 IOC 底层原理:
    Spring知识概括_第1张图片

BeanFactory 接口:

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

  • Spring 提供 IOC 容器实现两种方式:(两个接口)
    BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用,加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象。
    ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用,加载配置文件时候就会把在配置文件对象进行创建。
    BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory

  • ApplicationContext 接口有实现类:
    Spring知识概括_第2张图片

IOC 操作 Bean 管理:

  • Bean 管理指的是两个操作
    Spring 创建对象
    Spirng 注入属性

  • Bean 管理操作有两种方式
    基于 xml 配置文件方式实现
    基于注解方式实现

基于 xml 方式:

  • 基于 xml 方式创建对象
    ①在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
    ②在 bean 标签有很多属性,介绍常用的属性:
    <1>id 属性:唯一标识
    <2>class 属性:类全路径(包类路径)
    ③创建对象时候,默认也是执行无参数构造方法完成对象创建
  • 基于 xml 方式注入属性,DI就是依赖注入,也就是注入属性。
    使用 set 方法进行注入
    <1>创建类,定义属性和对应的 set 方法
    使用有参数构造进行注入
    <1>创建类,定义属性,创建属性对应有参数构造方法
    <2>在 spring 配置文件中进行配置
    p 名称空间注入(了解):使用 p 名称空间注入,可以简化基于 xml 配置方式
    <1>添加 p 名称空间在配置文件中
    <2>进行属性注入,在 bean 标签里面进行操作

<bean id="teacher1" class="com.hex.first.Teacher">
    <property name="teacherName" value="hex">property>
    <property name="teacherAge" value="20">property>
bean>

<bean id="course1" class="com.hex.first.Course">
    <property name="courseName" value="Java">property>
    <property name="courseHour" value="120">property>
    <property name="teacher" ref="teacher1">property>
bean>




<bean id="teacher2" class="com.hex.first.Teacher">
    <constructor-arg name="teacherName" value="hex" index="0" type="String">constructor-arg>
    <constructor-arg name="teacherAge" value="22" index="1" type="int">constructor-arg>
bean>

<bean id="course2" class="com.hex.first.Course">
    <constructor-arg name="courseName" value=".Net C#" index="0" type="String">constructor-arg>
    <constructor-arg name="courseHour" value="110" index="1" type="int">constructor-arg>
    <constructor-arg name="teacher"  ref="teacher2" index="2" type="com.hex.first.Teacher">constructor-arg>
bean>



<bean id="teacher3" class="com.hex.first.Teacher" p:teacherName="zs" p:teacherAge="21">
bean>
<bean id="course3" class="com.hex.first.Course" p:courseName="C++" p:courseHour="100" p:teacher-ref="teacher3">
bean>

xml 注入其他类型属性:

  • 字面量:
    null 值
    空串
    属性值包含特殊符号
    <1>如果遇到特殊字符则需要进行转义
    <2>进行CDATA封装
注入空字符串值:
<property name="emptyValue">
	<value>value>
property>

注入null值:
<property name="nullValue">  
	<null/>
property>

bean>

CDATA封装:
]]>

转义:
<	>	大于
>	<	小于
&	&	和号
'	.	省略号
"	"	引号
  • 注入属性-外部 bean
    ①创建两个类 service 类和 dao 类
    ②在 service 调用 dao 里面的方法
    ③在 spring 配置文件中进行配置
<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="userService" class="com.Keafmd.spring5.service.UserService">
    
    <property name="userDao" ref="userDaoImpl">property>
bean>

<bean id="userDaoImpl" class="com.Keafmd.spring5.dao.UserDaoImpl">bean>
beans>
  • 注入属性-内部 bean
    简介:
    <1>内部bean可以在 元素内使用 元素
    <2>内部bbean不管是否指定id或者name,该bean都有一个唯一的匿名标识符,且不能被指定别名。
    <3>内部bean通常是匿名的,也就是内部bean对其它外部的bean不可见。
    <4>内部bean的Scope一般是prototype。
    ②关系梳理:
    <1>一对多关系:部门和员工
    <2>一个部门有多个员工,一个员工属于一个部门
    <3>部门是一,员工是多
    <4>在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
    ③步骤:
    <1>创建实体类
    <2>在 spring 配置文件中进行配置
<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="emp" class="com.Keafmd.spring5.bean.Emp">
    
    <property name="ename" value="小明">property>
    <property name="gender" value="">property>

    
    <property name="dept">
        <bean id="dept" class="com.Keafmd.spring5.bean.Dept">
            <property name="dname" value

你可能感兴趣的:(Spring,spring)