Spring的AOP解析

AOP思想

AOP(Aspect Oriented Programming),即面向切面编程,AOP利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块。简单来说,就是横向重复,纵向抽取。常用的地方,比如事务管理、日志、缓存等等

以往用到的AOP编程思想
  • Filter过滤器
    Filter过滤器

  • Struts2中的拦截器
    Struts2中的拦截器

  • 动态代理技术
    动态代理技术

spring的aop的图解
spring的aop的图解

aop中的名词解释
aop中的名词解释

Spring中Aop开发

  • 导包
    spring的4个基本包+log4j两个包+spring的aop包+spring需要的第三方包
    4+2
    spring-aspects-4.2.4.RELEASE.jar
    spring-aop-4.2.4.RELEASE.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

  • 准备目标对象
    创建被代理对象的接口和实现类

// 接口
package cn.itcast.service;
public interface UserService {
    void save();
    void delete();
    void update();
    void find();
}

//实现类
package cn.itcast.service;

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("保存用户!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("删除用户!");
    }
    @Override
    public void update() {
        System.out.println("更新用户!");
    }
    @Override
    public void find() {
        System.out.println("查找用户!");
    }
}
  • 编写一个通知类
package cn.itcast.e_annotationaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;

//表示该类是一个通知类
public class MyAdvice {
    //前置通知
    //指定该方法是前置通知,并制定切入点
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}
  • 配置进行织入,在xml配置文件中将通知配入目标对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.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(约束)命名空间 -->
<!-- 1.配置目标对象 -->
    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.配置通知对象 -->
    <bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean>
<!-- 3.配置将通知织入目标对象 -->
    <aop:config>
        <!-- 配置切入点 
            public void cn.itcast.service.UserServiceImpl.save() 
            void cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.*()

            * cn.itcast.service.*ServiceImpl.*(..)
            * cn.itcast.service..*ServiceImpl.*(..)
        -->
        <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice" >
            <!-- 指定名为before方法作为前置通知 -->
            <aop:before method="before" pointcut-ref="pc" />
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc" />
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- 异常拦截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
</beans>

AOP注解开发

  • 前置通知
    @Before(“execution(* cn.itcast.service.ServiceImpl.(..))”)
  • 后置通知
    @AfterReturning(“execution(* cn.itcast.service.ServiceImpl.(..))”)
  • 环绕通知
    @Around(“execution(* cn.itcast.service.ServiceImpl.(..))”)
  • 异常通知
    @AfterThrowing(“execution(* cn.itcast.service.ServiceImpl.(..))”)
  • 后置通知
    @After(“execution(* cn.itcast.service.ServiceImpl.(..))”)

每一个方法都写上注解会比较繁琐,所以提出公共的部分,写一个方法,写上切入点的注解。
@Pointcut(“execution(* cn.itcast.service.ServiceImpl.(..))”)
public void pc(){}
然后在各个方法中使用该方法即可
@Before(“MyAdvice.pc()”)
public void before(){
System.out.println(“这是前置通知!!”);
}

猜你喜欢

转载自blog.csdn.net/qq_42780864/article/details/81366591