Spring之基于注解的AOP配置

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Spring之基于注解的AOP配置,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

一、添加依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

二、AOP相关注解

@Aspect 注解声明该类为切面类。

@Before 申明该方法为前置通知,属性value指定切入点表达式,或指定切入点表达式的引用

@AfterReturning申明该方法为后置通知,属性value指定切入点表达式,或指定切入点表达式的引用

@AfterThrowing 申明该方法为异常通知,属性value指定切入点表达式,或指定切入点表达式的引用

@After 申明该访问为最终通知,属性value指定切入点表达式,或指定切入点表达式的引用

@Around 申明该方法是环绕通知,属性value指定切入点表达式,或指定切入点表达式的引用
@Pointcut 指定切入点表达式,属性value:指定表达式的内容

三、定义切面

@Component("webLog")
//@Aspect 申明该类是一个切面类
@Aspect
public class WebLog {
    
    @Pointcut("execution(* cn.ybzy.service.impl.*.*(..))")
    private void webLog(){
    
    }
  
    /**
     * 环绕通知
     * @param proceedingJoinPoint Spring提供的一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法相当于明确调用切入点方法。
     *
     *            该接口可以作为环绕通知的方法参数,spring中的环绕通知提供的是一种可以在代码中手动控制增强方法的方式。
     * @return
     */
 /*   @Around(value = "webLog()")
   // @Around("execution(* cn.ybzy.service.impl.*.*(..))")
    public Object aroundLog(ProceedingJoinPoint proceedingJoinPoint){
        Object returnValue = null;
        try{
            //获取方法执行所需的参数
            Object[] args = proceedingJoinPoint.getArgs();
    
            System.out.println("前置通知。。。");
            
            //调用切入点方法
            returnValue = proceedingJoinPoint.proceed(args);
            
            System.out.println("后置通知。。。");
            
            
            return returnValue;
            
        }catch (Throwable t){
            System.out.println("异常通知。。。");
            throw new RuntimeException(t);
        }finally {
            System.out.println("最终通知。。。");
        }
    }
   */
      /**
     * 前置通知
     */
    @Before(value = "execution(* cn.ybzy.service.impl.*.*(..))")
    public  void beforeLog(){
        System.out.println("前置通知。。。");
    }
    
    /**
     * 后置通知
     */
    @AfterReturning(value = "execution(* cn.ybzy.service.impl.*.*(..))")
    public  void afterReturningLog(){
        System.out.println("后置通知。。。");
    }
    /**
     * 异常通知
     */
    @AfterThrowing(value = "webLog()")
    public  void afterThrowingLog(){
        System.out.println("异常通知。。。");
    }
    
    /**
     * 最终通知
     */
    @After(value = "webLog()")
    public  void afterLog(){
        System.out.println("最终通知。。。");
    }
    
   
}

四、创建接口及实现

public interface IUserService {
   void saveUser();
 }


@Service("userService")
public class UserServiceImpl implements IUserService {
    
    @Override
    public void saveUser() {
        System.out.println("保存...");
        //int i=1/0;
    }
}

五、使用配置类代替XML配置文件

@Configuration
@ComponentScan(basePackages="cn.ybzy")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
使用spring.xml配置文件方式
<?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"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置扫描的包-->
    <context:component-scan base-package="cn.ybzy"></context:component-scan>

    <!-- 配置spring开启注解AOP的支持 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

六、执行测试

public class WebLogTest {

    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        IUserService as = (IUserService)ac.getBean("userService");
        as.saveUser();
    }
}

1.使用前置、后置、异常、最终通知
在这里插入图片描述
2.使用环绕通知
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137130.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!