J2EE进阶学习——Spring框架(五):AOP

AOP概念

1.AOP:面向切面(方面)编程,扩展功能不修改源代码实现

2.AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监测、事务管理、安全检查、缓存)


AOP原理

未改进的做法

纵向抽取机制

动态代理方式

横向抽取机制

AOP操作术语(重点掌握加粗的三个)

  • Joinpoint(连接点):所谓连接点是指那些被拦截到的点,在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点
  • Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义

  • Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知(切面完成的功能)

  • Aspect(切面):是切入点和通知(引介)的结合

  • Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field
  • Target(目标对象):代理的目标对象(要增强的类)
  • Weaving(织入):是把增强应用到目标的过程,把advice应用到target的过程
  • Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
操作术语

Spring的AOP操作

1.在Sping里面进行AOP操作,使用aspectj实现

  • aspectj不是Spring的一部分,和Spring一起使用进行AOP操作
  • Spring2.0以后新增了对Aspectj支持

2.使用aspectj实现AOP有两种方式

  • 基于aspectj的xml配置
  • 基于aspectj的注解方式
AOP操作的准备工作:
  • 导包


    jar包
  • 创建Spring核心配置文件,导入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>

附上文档网站:https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html

3.使用表达式配置切入点

  • 切入点:实际增强的方法
  • 常用的表达式
    execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
    • execution(* com.TiHom.aop.Book.add(...))
    • execution(* com.TiHom.aop.Book.*(...))
    • execution(* .(...))
    • 匹配所有save开头的方法 execution(* save*(...))

4.基于AspecJ的配置文件方式的AOP操作

Book

public class Book {
    public void add(){
        System.out.println("add...");
    }
}

MyBook

public class MyBook {
    public void before1(){
        System.out.println("before...");
    }

    public void after1(){
        System.out.println("after...");
    }

    //环绕增强
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("方法之前");
        //执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("方法之后");
    }
}

bean3.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: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/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    <aop:aspectj-autoproxy />
    <bean id="book" class="com.TiHom.aop.Book"></bean>
    <bean id="myBook" class="com.TiHom.aop.MyBook"></bean>

    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* com.TiHom.aop.Book.*(..))"/>
        <aop:aspect ref="myBook">
            <aop:before method="before1" pointcut-ref="pointcut1"/>
            <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
            <aop:around method="around" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
</beans>    

5.log4j介绍

  • 通过log4j可以看到程序运行过程中更详细的内容
    • 经常使用log4j查看日志
  • 使用
    • 导入log4j的jar包
    • 复制log4j的配置文件,到src下
  • 设置日志级别
    • info:看到基本信息
    • debug:看到更详细的信息

6.spring web整合

让spring的配置文件在服务器启动的时候加载

要导入spring-web的jar包,这里贴一个spring jar包下载地址
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring-web/4.3.1.RELEASE/spring-web-4.3.1.RELEASE.jar

在web.xml中配置

<!-- 指定监听器的位置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring配置的位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:bean1.xml</param-value>
</context-param>

7.基于Aspecj注解的AOP操作

切面中

@Aspect
public class MyBook {
    @Before(value = "execution(* com.TiHom.aop.Book.*(..))")
    public void before1(){
        System.out.println("before...");
    }
}

bean3.xml中

    <!-- 开启AOP操作 -->
    <aop:aspectj-autoproxy />
    <!-- 创建对象 -->
    <bean id="book" class="com.TiHom.aop.Book"></bean>
    <bean id="myBook" class="com.TiHom.aop.MyBook"></bean>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容