您的位置:首页 > 编程语言 > Java开发

Spring入门介绍

2019-11-09 12:04 2271 查看

概述

  • 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配置文件+反射,得到该类的实例

    属性注入(DI)

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

    测试:

    • 复杂类型的注入

        Array
      1. List
      2. Map
      3. properties

    Spring 的 bean 管理(注解)

    一、导入 jar 包

    二、创建 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>

    三、再 Spring 中开启注解扫描

    • 如果有多个包,就填写共有的包名

    四、注解创建对象

    4.1 创建对象有四个注解

    4.2 创建对象的方式

    • @Scope(value="singleton") 单实例
    • @Scope(value="prototype") 多实例

    五、注解注入属性

    1. 创建service类,创建dao类,再service中得到dao对象

        用注解创建 dao 对象和 service 对象

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

      第二种:@Resource

      • 测试

    AOP

    一、AOP概念

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

    二、AOP原理

    三、AOP操作术语

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

    1. 介绍

    • AspectJ不是Spring的一部分,是和Spring一起使用进行AOP操作

    2. 实现AOP的两种方式

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

    3. 实现步骤

    • AOP操作准备

      导入基本jar包和AOP相关的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配置文件

      导入约束

  • 配置AOP操作

    Spring整合web项目

    一、实现原理

    1. 通过 new 对象,功能可以实现,但效率很低
    1. 实现思想:把加载配置文件和创建对象过程,在服务器启动的时候完成

    2. 具体实现原理 (1) ServletContext对象 (2) 监听器 (3) 具体步骤

        在服务器启动的时候,为每个项目创建一个ServletContext对象
      • 在 ServletContext 对象创建的时候,使用监听器可以监听 ServletContext对象什么时候创建
      • 当监听器监听到 ServletContext 对象创建的时候,加载 spring 配置文件,创建配置文件对象
      • 把创建出来的对象放到 ServletContext 域对象里面(setAttribute 方法)
      • 获取对象(getAttribute 方法)

    知识点

    ##ServletContext 对象

    一、介绍

    • ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象
    • 这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。

    二、作用

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

    三、域对象

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

    监听器 Listener

    一、监听器模型

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

    二、执行步骤

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

    execution表达式

    一、语法

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

    二、常用的写法

    • execution(* com.ljw.spring.aop.User.add(..)) 对User类的add方法增强
    • execution(* com.ljw.spring.aop.User.*(..)) 对User类的所有方法增强
    • execution(* *.*(..)) 对所有方法增强
    • execution(* save*(..)) 对所有save开头的方法增强
  • 内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: