AOP基于注解实现

本文介绍了如何使用AOP注解实现切面增强,包括@Aspect声明切面类,@Before、@After、@Around、@AfterThrowing声明各种增强,并展示了对应的XML配置、接口、实现类以及测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

切面类
@Component    //控制反转
@Aspect      //声明切面类
public class ForumAdvisor {
    //execution(* com.mitu.aspect.*.removeTopic(..))切点
    @Before("execution(* com.mitu.aspect.*.removeTopic(..))")    //声明前置增强
    public void before(JoinPoint jPoint) throws Throwable {
        System.out.println("这是前置增强");
        
        //获取目标对象包全名
        String classType=jPoint.getTarget().getClass().getName();
        Class<?> clazz=Class.forName(classType);
        String className = clazz.getName();
        System.out.println(className);
        
        //获取目标对象类名
        String classSimpleName = clazz.getSimpleName();
        System.out.println(classSimpleName);
        
        //获取目标对象方法名
        String methodName = jPoint.getSignature().getName();
        System.out.println(methodName);
        
        //获取目标对象参数值
        Object[] args = jPoint.getArgs();
        System.out.println("参数:"+args[0]);
    }
    
    @After("execution(* com.mitu.aspect.*.*(..))")    //声明后置增强
    public void after() {
        System.out.println("这是后置增强");
    }

    @Around("execution(* com.mitu.aspect.*.*(..))")    //声明环绕增强
    public Object around(ProceedingJoinPoint point) throws Throwable{
        //计算方法耗时
        System.out.println("这是环绕增强(前置增强)");
        long start = System.currentTimeMillis();
        
        Object object = point.proceed();
        
        System.out.println("这是环绕增强(后置增强)");
        long end = System.currentTimeMillis();
        
        //获取目标对象包全名
        String classType=point.getTarget().getClass().getName();
        Class<?> clazz=Class.forName(classType);
        String className = clazz.getName();
        //获取目标对象方法名
        String methodName = point.getSignature().getName();
        
        System.out.println(className+":"+methodName+"耗时"+(end-start));
        return object;
    }

    @AfterThrowing(pointcut="execution(* com.mitu.aspect.*.*(..))",throwing="ex")    //声明抛出异常增强
    public void afterThrowing(Exception ex) {
        System.out.println("这是抛出异常增强");
    }
}

aop.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 开启AOP注解开关 -->
    <aop:aspectj-autoproxy/>
    <!-- 开启注解自动扫描(需要扫描切面类、依赖类与被依赖类  不需要扫描操作类(客户端类)) -->
    <context:component-scan base-package="com.mitu.annonationaop"/>
</beans>

接口类:
public interface IForumService {
    public void removeTopic(int topicId);
    public void removeForum(int forumId);
}

实现类:
@Component    //控制反转
public class ForumServiceImp implements IForumService{

    @Override
    public void removeTopic(int topicId) {
        try {
            System.out.println("删除论坛帖子:"+topicId);
            //Thread.currentThread()获取当前线程
            Thread.currentThread().sleep(600);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        throw new RuntimeException("抛出运行时异常");
    }

    @Override
    public void removeForum(int forumId) {
        try {
            System.out.println("删除论坛板块:"+forumId);
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

测试类:
public class AnnonationAopTest {
    public static void main(String[] args) {
        ApplicationContext aContext = new ClassPathXmlApplicationContext("com/mitu/annonationaop/aop.xml");
        //注意强制类型转换的类型为:接口IForumService
        IForumService forumService = (IForumService)aContext.getBean("forumServiceImp");
        forumService.removeForum(200);
        forumService.removeTopic(150);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值