• javaee spring aop 注解实现


    切面类

    package com.test.advice;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    
    //切面类
    @Aspect
    public class MyAdvice {
    
        //定义切点表达式
        @Pointcut("execution(* com.test.service.impl.*.add(..))")
        public void pc()
        {
    
        }
    
        //@Before("execution(* com.test.service.impl.*.add(..))")
        @Before("MyAdvice.pc()")
        //将这个增强方法切入到service层的add方法前
        public void before()
        {
            System.out.println("添加用户之前");
        }
    
    
        //目标方法执行后(不管是出异常还是成功执行)
        @After("MyAdvice.pc()")
        public void after()
        {
            System.out.println("添加用户之后");
        }
    
        //环绕通知,用这个增强代码替换掉目标方法
        @Around("MyAdvice.pc()")
        public void around(ProceedingJoinPoint point) throws Throwable {
            System.out.println("执行目标方法之前");
            point.proceed(); //放行切点处的方法(目标方法)
        }
    
        //目标方法成功执行后
        @AfterReturning("MyAdvice.pc()")
        public void afterReturning()
        {
            System.out.println("目标方法成功执行后");
        }
    
        //目标方法出异常
        @AfterThrowing("MyAdvice.pc()")
        public void afterThrowing()
        {
            System.out.println("目标方法出异常以后才执行");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    spring配置文件

    
    <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="com.test" />
    
        
        <bean id="myAdvice" class="com.test.advice.MyAdvice" />
    
        
        <aop:aspectj-autoproxy>
    
        aop:aspectj-autoproxy>
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    依赖

    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0modelVersion>
    
      <groupId>org.examplegroupId>
      <artifactId>testSpring07artifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>warpackaging>
    
      <name>testSpring07 Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>4.3.18.Releaseversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-coreartifactId>
          <version>4.3.18.Releaseversion>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-beansartifactId>
          <version>4.3.18.Releaseversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-context-supportartifactId>
          <version>4.3.18.Releaseversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-expressionartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        
        <dependency>
          <groupId>org.aspectjgroupId>
          <artifactId>aspectjweaverartifactId>
          <version>1.8.10version>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-aspectsartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-aopartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
      dependencies>
    
      <build>
        <finalName>testSpring07finalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    目标类

    package com.test.service.impl;
    
    import com.test.service.IUsersService;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UsersService implements IUsersService {
    
        @Override
        public void add()  {
    
            System.out.println("添加用户...");
        }
    
        @Override
        public void update() {
            System.out.println("修改用户...");
        }
    
        @Override
        public void delete() {
            System.out.println("删除用户...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    测试类

    package com.test.aop;
    
    import com.test.service.IUsersService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop {
    
        @Test
        public void test()
        {
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
            IUsersService usersServiceProxy=  applicationContext.getBean("usersService",IUsersService.class);
    
            try {
                usersServiceProxy.add();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    测试结果

    在这里插入图片描述

  • 相关阅读:
    技术管理进阶——什么是公司文化
    60 个深度学习教程:包含论文、实现和注释 | 开源日报 No.202
    sublime_text_4126_x64 激活及安装
    Python装饰器(包装函数、拦截函数)
    绍兴市越城区人大常委会主任徐荻一行莅临迪捷软件调研指导
    怎么实现一个登录时需要输入验证码的功能
    java计算机毕业设计高校学生智慧党建系统设计与开发源码+mysql数据库+系统+lw文档+部署
    打通AI应用工业化大生产全流程:昇腾注解“AI向上的力量”
    【导航】嵌入式 Linux 学习专栏目录 【快速跳转】
    ElasticSearch中实际操作细节点
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/132643754