您现在的位置是:首页 > 个人日记个人日记

spring-09-AOP-注解配置详细介绍

2019-02-03 18:48:59【个人日记】306人已围观

简介使用注解的方式来配置AOP更加简洁

注解配置

先在spring-config.xml加入注解扫描

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:contet="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  7. <!--扫描注解-->
  8. <contet:component-scan base-package="com.spring"></contet:component-scan>
  9. <!--使AOP注解生效
  10. 如果要代理的类有接口,且实现则使用JDK的动态代理
  11. 如果要代理的类没有实现接口则使用cglib动态代理
  12. -->
  13. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  14. </beans>

在service层加入注解@Service

主要配置在增强中

  1. package com.spring.aspect;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.*;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @Aspect//表示一个切面
  7. public class TransactionAdvice {
  8. /**
  9. * 切入点配置
  10. */
  11. //这里使用切入点
  12. @Pointcut("execution(* com.spring.service.UserService.*(*))")
  13. private void pointCut() {
  14. }
  15. /**
  16. * 使用环绕
  17. *
  18. * @param proceedingJoinPoint
  19. * @return
  20. */
  21. // @Around("pointCut()")
  22. public Object around(ProceedingJoinPoint proceedingJoinPoint) {
  23. Object result = null;
  24. try {
  25. System.out.println("开启事务");
  26. result = proceedingJoinPoint.proceed();
  27. System.out.println("提交事务");
  28. } catch (Throwable throwable) {
  29. result = 500;
  30. System.out.println("回滚事务");
  31. throwable.printStackTrace();
  32. } finally {
  33. System.out.println("释放资源");
  34. }
  35. return result;
  36. }
  37. /**
  38. * 前置通知
  39. */
  40. @Before("pointCut()")
  41. public void brfore() {
  42. System.out.println("开启事务");
  43. }
  44. /**
  45. * 后置通知
  46. */
  47. @AfterReturning("pointCut()")
  48. public void afterReturning() {
  49. System.out.println("提交事务");
  50. }
  51. /**
  52. * 最终停止
  53. */
  54. @After("pointCut()")
  55. public void after() {
  56. System.out.println("释放资源");
  57. }
  58. /**
  59. * 异常通知
  60. */
  61. @AfterThrowing(pointcut = "pointCut()",throwing = "e")
  62. public void afterThrowing(Throwable e) {
  63. System.out.println("回滚事务" + e.getMessage());
  64. }
  65. }

以上是两种方式配置AOP
1) 直接使用@Around标签环绕
2) 在创建之前需要在类中创建一个名称为切点名称的方法,设为私有,并在上面使用注解 @Pointcut("execution(* com.spring.service.UserService.())")

Tags: JavaWeb  

上一篇: 创建博客心得

下一篇: mybatis-08-延迟加载

评论区

    2024-04-19 11:55:53

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我