spring AOP 基于注解(spring版本4.2.0)

spring AOP 概念

点击打开链接

spring AOP 如何获取目标方法的参数,如何在目标方法执行前,修改目标方法的参数。如何在目标方法执行后,修改目标方法的返回结果。

点击打开链接

spring AOP 基于注解的实现

需要依赖的jar
aopalliance-1.0.jar,aspectjrt-1.8.7.jar,cglib-nodep-3.2.0.jar,aspectjweaver-1.8.7.jar

被代理类

package com.sky.lp.AAtestAop;

import org.springframework.stereotype.Component;

@Component
public class Human implements Sleepable{

    @Override
    public String sleep(String str) {
        System.out.println("睡觉了!梦中自有颜如玉!");
        return str + "+lp";
//      throw new RuntimeException("被代理类抛错");
    }

}

被代理类实现的接口

package com.sky.lp.AAtestAop;

public interface Sleepable {

    public String sleep(String str);

}

切面

package com.sky.lp.AAtestAop;

import java.util.Date;

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.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class SleepHelper {

    //切点
    @Pointcut("execution(* com.sky.lp.AAtestAop.Human.*(..))")
    public void sleeppoint() {
    }

    //环绕通知
    @Around("sleeppoint()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("进入around通知" + new Date().getTime());
        Object object = pjp.proceed();// 执行目标方法
        System.out.println("退出around通知" + new Date().getTime());
        return object;
    }

    //前置通知
    @Before("sleeppoint()")
    public void before() {
        System.out.println("这里是Before通知" + new Date().getTime());
    }
    //在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行
    @AfterReturning("sleeppoint()")
    public void afterReturning() {
        System.out.println("这里是afterReturning通知" + new Date().getTime());
    }
    //在连接点抛出异常后执行,不抛出异常则不执行
    @AfterThrowing("sleeppoint()")
    public void afterThrowing() {
        System.out.println("这里是afterThrowing通知" + new Date().getTime());
    }

    //在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容
    @After("sleeppoint()")
    public void after() {
        System.out.println("这里是After通知" + new Date().getTime());
    }

}

测试类

package com.sky.lp.AAtestAop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-servlet.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("human");
        String str = sleeper.sleep("lp");
        System.out.println("结果:" + str);
    }

}

测试类运行结果
进入around通知1447752935590
这里是Before通知1447752935590
睡觉了!梦中自有颜如玉!
退出around通知1447752935590
这里是After通知1447752935590
这里是afterReturning通知1447752935590
结果:lp+lp

spring-servlet.xml 配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
    <context:annotation-config />
    <context:component-scan base-package="com.sky.lp.AAtestAop" />

    <!--    <aop:aspectj-autoproxy proxy-target-class="true "/> -->
<!-- proxy-target-class 默认值为 false,设置为 true 意思是 强制使用 CGLIB 动态代理 -->
<!-- 如果不指定 proxy-target-class ,spring会根据 被代理类 是否实现了接口 进行判断   使用jdk动态代理还是使用CGLIB动态代理。 -->
<!-- 如果 被代理类实现了 接口,则使用JDK动态代理,如果没有实现接口,则使用CGLIB动态代理。 -->
    <aop:aspectj-autoproxy/>

</beans>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOPSpring框架中的一个重要模块,它提供了面向切面编程(AOP)的支持。AOP是一种编程思想,它可以在不改变原有代码的情况下,通过在程序运行时动态地将代码“织入”到现有代码中,从而实现对原有代码的增强。 Spring AOP提供了基于注解AOP实现,使得开发者可以通过注解的方式来定义切面、切点和通知等相关内容,从而简化了AOP的使用。 下面是一个基于注解AOP实现的例子: 1. 定义切面类 ```java @Aspect @Component public class LogAspect { @Pointcut("@annotation(Log)") public void logPointcut() {} @Before("logPointcut()") public void beforeLog(JoinPoint joinPoint) { // 前置通知 System.out.println("执行方法:" + joinPoint.getSignature().getName()); } @AfterReturning("logPointcut()") public void afterLog(JoinPoint joinPoint) { // 后置通知 System.out.println("方法执行完成:" + joinPoint.getSignature().getName()); } @AfterThrowing(pointcut = "logPointcut()", throwing = "ex") public void afterThrowingLog(JoinPoint joinPoint, Exception ex) { // 异常通知 System.out.println("方法执行异常:" + joinPoint.getSignature().getName() + ",异常信息:" + ex.getMessage()); } } ``` 2. 定义业务逻辑类 ```java @Service public class UserService { @Log public void addUser(User user) { // 添加用户 System.out.println("添加用户:" + user.getName()); } @Log public void deleteUser(String userId) { // 删除用户 System.out.println("删除用户:" + userId); throw new RuntimeException("删除用户异常"); } } ``` 3. 在配置文件中开启AOP ```xml <aop:aspectj-autoproxy/> <context:component-scan base-package="com.example"/> ``` 在这个例子中,我们定义了一个切面类LogAspect,其中通过@Aspect注解定义了一个切面,通过@Pointcut注解定义了一个切点,通过@Before、@AfterReturning和@AfterThrowing注解分别定义了前置通知、后置通知和异常通知。 在业务逻辑类中,我们通过@Log注解标注了需要增强的方法。 最后,在配置文件中,我们通过<aop:aspectj-autoproxy/>开启了AOP功能,并通过<context:component-scan>扫描了指定包下的所有组件。 这样,当我们调用UserService中的方法时,就会触发LogAspect中定义的通知,从而实现对原有代码的增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值