spring aop

pom.xml

复制代码
<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
View Code
复制代码

Hello

复制代码
package org.mythsky.springaopdemo;

public interface Hello {
    // 定义一个简单方法,模拟应用中的业务逻辑方法
    void foo();
    // 定义一个addUser()方法,模拟应用中的添加用户的方法
    int addUser(String name , String pass);
}
View Code
复制代码

HelloImpl

复制代码
package org.mythsky.springaopdemo;

import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component("hello")
public class HelloImpl implements Hello {

    public void foo() {
        System.out.println("执行Hello组件的foo()方法");
    }

    public int addUser(String name, String pass) {
        System.out.println("执行Hello组件的addUser添加用户:"+name);
        return 0;
    }
}
View Code
复制代码

AuthAspect

复制代码
package org.mythsky.springaopdemo;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AuthAspect {
    @Before("execution(* org.mythsky.springaopdemo.*.*(..))")
    public void authority(){
        System.out.println("模拟执行权限检查");
    }
}
View Code
复制代码

services.xml

<context:component-scan base-package="org.mythsky.springaopdemo"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

BeanTest

复制代码
package org.mythsky.springaopdemo;

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

public class BeanTest {
    public static void main(String[] args){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml");
        Hello hello = ctx.getBean("hello" , Hello.class);
        hello.foo();
        hello.addUser("孙悟空" , "7788");
    }
}
View Code
复制代码

运行结果:

 

这里要注意切面类也需要添加@Component注解。

 AfterReturning

LogAspect

复制代码
package org.mythsky.springaopdemo;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {
    @AfterReturning(returning = "rvt",pointcut = "execution(* org.mythsky.springaopdemo.*.*(..))")
    public void log(Object rvt){
        System.out.println("获取目标方法返回值:"+rvt);
        System.out.println("模拟记录日志功能...");
    }
}
View Code
复制代码

重新运行结果:

 AfterThrowing

RepairAspect

复制代码
package org.mythsky.springaopdemo;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class RepairAspect {
    @AfterThrowing(throwing = "ex",pointcut = "execution(* org.mythsky.springaopdemo.*.*(..))")
    public void doRecoveryActions(Throwable ex){
        System.out.println("目标方法中抛出的异常:"+ex);
        System.out.println("模拟Advice对异常的修复...");
    }
}
View Code
复制代码

修改下HelloImpl

复制代码
public int addUser(String name, String pass) {
        System.out.println("执行Hello组件的addUser添加用户:"+name);
        if(name.length()<3||name.length()>10){
            throw new IllegalArgumentException("name参数的长度必须大于3,小于10!");
        }
        return 18;
    }
View Code
复制代码

修改主程序BeanTest

复制代码
public static void main(String[] args){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml");
        Hello hello = ctx.getBean("hello" , Hello.class);
        hello.foo();
        hello.addUser("悟空" , "7788");
    }
View Code
复制代码

运行结果:

 After

ReleaseAspect

复制代码
package org.mythsky.springaopdemo;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ReleaseAspect {
    @After("execution(* org.mythsky.springaopdemo.*.*(..))")
    public void release(){
        System.out.println("模拟方法结束后的释放资源...");
    }
}
View Code
复制代码

运行结果:

 Around

TxAspect

复制代码
package org.mythsky.springaopdemo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TxAspect {
    @Around("execution(* org.mythsky.springaopdemo.*.*(..))")
    public Object processTx(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("执行目标方法之前,模拟开始事务...");
        Object[] args=jp.getArgs();
        if(args!=null&&args.length>1){
            args[0]="【增加的前缀】"+args[0];
        }
        Object rvt=jp.proceed(args);
        System.out.println("执行目标方法之后,模拟结束事务...");
        if(rvt!=null&&rvt instanceof Integer)
            rvt=(Integer)rvt*(Integer)rvt;
        return rvt;
    }
}
View Code
复制代码

运行结果:

 

posted @   uptothesky  阅读(133)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
点击右上角即可分享
微信分享提示