Spring入门介绍

概述

  • 下载地址:https://repo.spring.io/release/org/springframework/spring/
  • spring是开源的轻量级框架
  • spring核心的主要两部分
    • AOP :面向切面编程,扩展功能而不修改源代码
    • IOC :控制反转,对象的建立经过spring配置来实现
  • spring是一站式框架
    • web :springMVC
    • service :IOC
    • dao :JDBC

IOC

IOC的底层原理

  • 建立xml配置文件,配置要建立对象的类
    <bean id="userSevice" class="com.ljw.service.UserService">
  • bean标签的属性
    • id:值不能有特殊字符
    • class:要配置的类的路径
    • scope:建立对象的方式
      • singleton:默认值,单例
      • prototype:多例
    • name:跟id相似,但能够有特殊字符
  • applicationContext.xml的默认内容
<?xml version="1.0" encoding="UTF-8"?>
<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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
</beans>
  • 建立工厂类,使用dom4j解析xml配置文件+反射,获得该类的实例
    IOC原理

属性注入(DI)

  • DI:依赖注入,向类里面的属性设置值
  • set方法
    1. 在类中定义该属性的set方法
    2. spring_context.xml 配置文件中配置相关的设置
  • 有参构造
    1. 定义有参构造方法
    2. spring_context.xml 配置文件中配置相关的设置
  • 对象属性注入
    1. 将要注入对象设置为类的属性
    2. spring_context.xml 配置文件中配置相关的设置
      spring_context.xml中的配置:
      spring_context.xml

测试:
testjava

  • 复杂类型的注入
    1. Array
    2. List
    3. Map
    4. properties
    1572178214073_26.png

Spring 的 bean 管理(注解)

  • 注解详情请查看文章:注解 @Annotationweb

    1、导入 jar 包

    复杂数据类型注入

2、建立 Spring 配置文件,导入约束

<?xml version="1.0" encoding="UTF-8"?>
<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"> <!-- bean definitions here -->
</beans>

3、再 Spring 中开启注解扫描

开启注解扫描

  • 若是有多个包,就填写共有的包名

4、注解建立对象

1572178213540_18.png

4.1 建立对象有四个注解

  • @Component
  • @Controller
  • @Service
  • @Repository
  • 目前这四个注解的功能是一致的,都是建立对象,只是为了让标注类自己的用途清晰

4.2 建立对象的方式

  • @Scope(value="singleton") 单实例
  • @Scope(value="prototype") 多实例
    建立对象

5、注解注入属性

  1. 建立service类,建立dao类,再service中获得dao对象
    • 用注解建立 dao 对象和 service 对象
      dao对象
      service对象spring

    • 再service类中建立dao类型的属性,并用注解注入对象,有两种方式
      第一种:@Autowired
      @Autowired编程

    第二种:@Resource
    @Resourcespring-mvc

    • 测试
      • image.png

AOP

1、AOP概念

  • AOP:面向切面(方面)编程
  • AOP采起横向抽取机制,取代了传统纵向继承体系重复性代码

2、AOP原理

AOP原理

3、AOP操做术语

  • Joinpoint(链接点): 类里面能够被加强的方法,这些方法称为链接点(能够被加强的方法)
  • Pointcut(切入点):所谓切入点是指咱们要对哪些Joinpoint进行拦截的定义.
  • Advice(通知/加强):所谓通知是指拦截到Joinpoint以后所要作的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能,在方法以前和以后执行(好比:计算程序运行的时间))
  • Aspect(切面): 是切入点和通知(引介)的结合
  • Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, - Introduction能够在运行期为类动态地添加一些方法或Field.
  • Target(目标对象):代理的目标对象(要加强的类)
  • Weaving(织入):是把加强应用到目标的过程.
    • 把advice 应用到 target的过程
  • Proxy(代理):一个类被AOP织入加强后,就产生一个结果代理类

4、使用AspectJ实现AOP

1. 介绍

  • AspectJ不是Spring的一部分,是和Spring一块儿使用进行AOP操做

2. 实现AOP的两种方式

  • 基于AspectJ的XML配置
  • 基于AspectJ的注解方式

3. 实现步骤

  • AOP操做准备
    • 导入基本jar包和AOP相关的jar包
      jar包服务器

    • 导入AOP约束
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
    </beans>
  • 使用表达式配置切入点
    • 切入点:实际加强的方法
    • 经常使用的表达式:execution 表达式
  • 加强类
package com.ljw.spring.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component(value="userAdvice")
public class UserAdvice {
    
    /**
     * @description 前置通知
     */
    public void userBefore() {
        System.out.println("前置通知........");
    }
    
    /**
     * @description 后置通知
     */
    public void userAfter() {
        System.out.println("后置通知........");
    }
    
    /**
     * @description 环绕通知
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    public void userAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 方法以前
        System.out.println("方法以前..........");
        
        // 执行加强的方法
        proceedingJoinPoint.proceed();
        
        // 方法以后
        System.out.println("方法以后..........");
    }
}
  • 配置Spring配置文件
    • 导入约束
      • image.png
    • 配置AOP操做
      • image.png

Spring整合web项目

1、实现原理

  1. 经过 new 对象,功能能够实现,但效率很低
    • image.png
  1. 实现思想:把加载配置文件和建立对象过程,在服务器启动的时候完成mvc

  2. 具体实现原理 (1) ServletContext对象 (2) 监听器 (3) 具体步骤
    • 在服务器启动的时候,为每一个项目建立一个ServletContext对象
    • 在 ServletContext 对象建立的时候,使用监听器能够监听 ServletContext对象何时建立
    • 当监听器监听到 ServletContext 对象建立的时候,加载 spring 配置文件,建立配置文件对象
    • 把建立出来的对象放到 ServletContext 域对象里面(setAttribute 方法)
    • 获取对象(getAttribute 方法)

知识点

ServletContext 对象

1、介绍

  • ServletContext官方叫servlet上下文。服务器会为每个工程建立一个对象,这个对象就是ServletContext对象
  • 这个对象全局惟一,并且工程内部的全部servlet都共享这个对象。因此叫全局应用程序共享对象。

2、做用

  • 是一个域对象
  • 能够读取全局配置参数
  • 能够搜索当前工程目录下面的资源文件
  • 能够获取当前工程名字(了解)

3、域对象

  1. 域对象介绍
  • 域对象是服务器在内存上建立的存储空间,用于在不一样动态资源(servlet)之间传递与共享数据。
  1. 怎样获得 ServletContext 对象
  • ServletContext servletContext = config.getServletContext();
  • ServletContext servletContext = this.getServletContext();
  1. 域对象方法
  • 凡是域对象都有以下三个方法
    • image.png

监听器 Listener

1、监听器模型

  • 事件:用户对组件的一个操做,或者说程序执行某个方法,称之为一个事件,如机器人程序执行工做。
  • 事件源:发生事件的组件就是事件源,也就是被监听的对象,如机器人能够工做,能够跳舞,那么就能够把机器人看作是一个事件源。
  • 事件监听器(处理器):监听并负责处理事件的方法,如监听机器人工做状况,在机器人工做先后作出相应的动做,或者获取机器人的状态信息。

2、执行步骤

  1. 给事件源注册监听器。
  2. 组件接受外部做用,也就是事件被触发。
  3. 组件产生一个相应的事件对象,并把此对象传递给与之关联的事件处理器。
  4. 事件处理器启动,并执行相关的代码来处理该事件。

execution表达式

1、语法

  • execution(<访问修饰符>?<返回类型><方法全名>(<参数>)<异常>)

2、经常使用的写法

  • execution(* com.ljw.spring.aop.User.add(..)) 对User类的add方法加强
  • execution(* com.ljw.spring.aop.User.*(..)) 对User类的全部方法加强
  • execution(* *.*(..)) 对全部方法加强
  • execution(* save*(..)) 对全部save开头的方法加强
相关文章
相关标签/搜索