Spring 注解AOP

1、导包


2、编写切面类,在切面类上直接使用AOP注解

package star.july.e_spring_aop_annotation;
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;
//使用注解AOP
@Aspect //代表一个切面类
public class MyAspect {
           /**
           * 常用的通知类:
           *   前置通知: 在目标对象的连接点之前调用
           *   后置通知:在目标对象的连接点之后调用
           *   环绕通知: 在目标对象的连接点之前与之后调用
           */
          
           @Before (value = "execution(* star.july.*.UserDao.*())" )
           public void before(){
                   System. out .println( "开启事务" );
          }
          
           //定义后置通知
           @After (value = "execution(* star.july.*.UserDao.update())" )
           public void after(){
                   System. out .println( "结束事务" );
          }
          
           //环绕通知
           @Around (value = "execution(* star.july.*.UserDao.*())" )
           public void round(ProceedingJoinPoint jp) throws Throwable{
                   System. out .println( "环绕通知前面代码" );
                   
                    //执行目标对象的核心方法
                   jp.proceed(); //执行连接点方法
                   
                   System. out .println( "环绕通知后面代码" );
          }
          
          
}



3、编写dao类

package star.july.e_spring_aop_annotation;
public class UserDao{
           //调用业务方法
           public void save(){
                   System. out .println( "调用save方法" );
          }
          
           public void update(){
                   System. out .println( "调用update方法" );
          }
}



4、配置

<? 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" >
           <!-- 1.创建目标对象 -->
           < bean id = "userDao" class = "star.july.e_spring_aop_annotation.UserDao" ></ bean >
          
           <!-- 2.创建切面类对象 -->
           < bean id = "myaspect" class = "star.july.e_spring_aop_annotation.MyAspect" ></ bean >
          
           <!-- 注意:注解方式必须开启自动创建代理类配置 -->
           < aop:aspectj-autoproxy ></ aop:aspectj-autoproxy >
        
       
</ beans >


5、测试

package star.july.e_spring_aop_annotation;

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

public class Demo {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserDao userDao = (UserDao)ac.getBean("userDao");
        userDao.save();
        userDao.update();
    }
}



可以参考上一篇文章,下方有链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Spring注解实现AOP的例子: 首先,在你的Spring配置文件中,需要开启AOP支持: ``` <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> ``` 然后,创建一个切面类,使用@Aspect注解标注: ``` @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before executing " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("After executing " + joinPoint.getSignature().getName() + ", result is " + result); } @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "exception") public void logAfterThrowing(JoinPoint joinPoint, Exception exception) { System.out.println("Exception thrown from " + joinPoint.getSignature().getName() + ", exception is " + exception); } } ``` 在上面的例子中,我们定义了三个通知,分别在方法执行前、执行后和抛出异常时执行。我们使用@Pointcut注解定义了一个切点,它匹配com.example.service包中的所有方法。然后,在通知方法上使用@Before、@AfterReturning和@AfterThrowing注解,分别表示在方法执行前、执行后和抛出异常时执行。 最后,在你的服务类中,使用@Loggable注解标注需要记录日志的方法: ``` @Service public class MyService { @Loggable public void doSomething() { // ... } } ``` 这样,在执行doSomething方法时,LoggingAspect中定义的通知方法就会被自动调用,从而实现了AOP的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值