SringAOP的三种实现

本文详细介绍了通过Spring API、自定义类和注解三种方法实现AOP,包括如何配置切点表达式execution,并解析了其实用语法。通过实例演示,展示了如何在UserService类的方法前后添加日志操作。

第一种:通过 Spring API 实现

  • 创建接口
package cn.itcast.demo01;

public interface UserService {

   public void add();

   public void delete();

   public void update();

   public void search();

}
  • 创建实现类(需要增强的类)
package cn.itcast.demo01;

public class UserServiceImpl implements UserService {
    
   public void add() {
       System.out.println("增加用户");
  }


   public void delete() {
       System.out.println("删除用户");
  }


   public void update() {
       System.out.println("更新用户");
  }


   public void search() {
       System.out.println("查询用户");
  }
}
  • 实现增强前的方法
package cn.itcast.demo01;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

   //method : 要执行的目标对象的方法
   //objects : 被调用的方法的参数
   //Object : 目标对象
   public void before(Method method, Object[] objects, Object o) throws Throwable {
       System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被执行了");
  }
}
  • 实现增强后的方法
package cn.itcast.demo01;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
   //returnValue 返回值
   //method被调用的方法
   //args 被调用的方法的对象的参数
   //target 被调用的目标对象
   public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
       System.out.println("执行了" + target.getClass().getName()
       +"的"+method.getName()+"方法,"
       +"返回值:"+returnValue);
  }
}
  • 在配置文件中进行配置
<?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-->
    <bean id="userService" class="cn.itcast.demo01.UserServiceImpl"/>
    <bean id="log" class="cn.itcast.demo01.Log"/>
    <bean id="afterLog" class="cn.itcast.demo01.AfterLog"/>

    <!--aop的配置-->
    <aop:config>
        <!--切入点 expression:表达式匹配要执行的方法-->
        <aop:pointcut id="pointcut" expression="execution(* cn.itcast.demo01.UserServiceImpl.*(..))"/>
        <!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>
  • 测试
import cn.itcast.demo01.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
   @Test
   public void test(){
       ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
       UserService userService = (UserService) context.getBean("userService");
       userService.search();
  }
}

第二种:自定义类来实现Aop

  • 定义接口
package cn.itcast.demo01;

public interface UserService {

   public void add();

   public void delete();

   public void update();

   public void search();

}
  • 定义要增强的类
package cn.itcast.demo01;

public class UserServiceImpl implements UserService {
    
   public void add() {
       System.out.println("增加用户");
  }


   public void delete() {
       System.out.println("删除用户");
  }


   public void update() {
       System.out.println("更新用户");
  }


   public void search() {
       System.out.println("查询用户");
  }
}
  • 定义要增强的方法类
package cn.itcast.demo02;

public class DiyPointcut {

   public void before(){
       System.out.println("---------方法执行前---------");
  }
   public void after(){
       System.out.println("---------方法执行后---------");
  }
   
}
  • 配置文件
<?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-->
    <bean id="userService" class="cn.itcast.demo01.UserServiceImpl"/>
    <bean id="diy" class="cn.itcast.demo02.DiyPointcut"/>
    <!--aop的配置-->
    <aop:config>
        <!--第二种方式:使用AOP的标签实现-->
        <aop:aspect ref="diy">
            <aop:pointcut id="diyPonitcut" expression="execution(* cn.itcast.demo01.UserServiceImpl.*(..))"/>
            <aop:before pointcut-ref="diyPonitcut" method="before"/>
            <aop:after pointcut-ref="diyPonitcut" method="after"/>
        </aop:aspect>
    </aop:config>
</beans>

第三种:使用注解实现

  • 第一步:编写一个注解实现的增强类
package cn.itcast.demo03;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointcut {
   @Before("execution(* cn.itcast.demo01.UserServiceImpl.*(..))")
   public void before(){
       System.out.println("---------方法执行前---------");
  }

   @After("execution(* cn.itcast.demo01.UserServiceImpl.*(..))")
   public void after(){
       System.out.println("---------方法执行后---------");
  }

   @Around("execution(* cn.itcast.demo01.UserServiceImpl.*(..))")
   public void around(ProceedingJoinPoint jp) throws Throwable {
       System.out.println("环绕前");
       System.out.println("签名:"+jp.getSignature());
       //执行目标方法proceed
       Object proceed = jp.proceed();
       System.out.println("环绕后");
       System.out.println(proceed);
  }
}
  • 编写接口
package cn.itcast.demo01;

public interface UserService {

   public void add();

   public void delete();

   public void update();

   public void search();

}
  • 编写需要增强的实现类
package cn.itcast.demo01;

public class UserServiceImpl implements UserService {
    
   public void add() {
       System.out.println("增加用户");
  }


   public void delete() {
       System.out.println("删除用户");
  }


   public void update() {
       System.out.println("更新用户");
  }


   public void search() {
       System.out.println("查询用户");
  }
}
  • 配置文件
<?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-->
    <bean id="userService" class="cn.itcast.demo01.UserServiceImpl"/>
    <bean id="annotationPointcut" class="cn.itcast.demo03.AnnotationPointcut"/>
    <aop:aspectj-autoproxy/>
</beans>

execution 表达式

例如定义切入点表达式 execution (* com.sample.service.impl..*.*(..))

execution()是最常用的切点函数,其语法如下所示:

整个表达式可以分为五个部分:

1、execution(): 表达式主体。

2、第一个*号:表示返回类型,*号表示所有的类型。

3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。

4、第二个*号:表示类名,*号表示所有的类。

5、*(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

名词解释

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值