SpingAOP案例(一)注解(Annotation)方式

本文介绍了一个使用Spring AOP实现的切面类,该类通过注解定义了前置、后置、例外和环绕通知,并展示了如何在配置文件中启用AspectJ自动代理。

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

有些类请参照上一篇文章的其它类,这里就不列举了
//这是切面类
package com.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
public class TestAnnotationAspect {

	
	@Pointcut("execution(* com.spring.service.*.*(..))")
    @SuppressWarnings("unused")
	private void pointCutMethod() {

		
    }  
	
	@Before("pointCutMethod()")
	 public void doBefore() {  
	        System.out.println("前置通知");  
	    }  
	 
	 //声明后置通知  
    @AfterReturning(pointcut = "pointCutMethod()", returning = "result")  
	 public void doAfterReturning(String result) {  
	        System.out.println("后置通知");  
	        result="";
	        System.out.println("---" + result + "---");  
	    }  
	 
	 //声明例外通知  
	    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")  
	    public void doAfterThrowing(Exception e) {  
	        System.out.println("例外通知");  
	        System.out.println(e.getMessage());  
	    }  
	    //声明环绕通知  
	    @Around("pointCutMethod()")  
	    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
	        System.out.println("进入方法---环绕通知");  
	        Object o = pjp.proceed();  
	        System.out.println("退出方法---环绕通知");  
	        return o;  
	    }  
}
//这是配置文件
<?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"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
        
       <!--   xml方式
    <aop:config>  
        <aop:aspect id="TestAspect" ref="aspectBean">  
           
            <aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))" />  
            <aop:before pointcut-ref="businessService" method="doBefore"/>  
            <aop:after pointcut-ref="businessService" method="doAfter"/>  
            <aop:around pointcut-ref="businessService" method="doAround"/>  
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
        </aop:aspect>  
    </aop:config>  
      
    <bean id="aspectBean" class="com.spring.aop.TestAspect" />  
    <bean id="aService" class="com.spring.service.AServiceImpl" ></bean>  
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
    -->
    
     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />  
  
    <bean id="aspectBean" class="com.spring.aop.TestAnnotationAspect" />  
    <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean> 
</beans>  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arisono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值