javaee spring aop 的五种通知方式

这篇具有很好参考价值的文章主要介绍了javaee spring aop 的五种通知方式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

spring配置文件

<?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="com.test" />

   <!-- 创建增强类对象 -->
    <bean id="myAdvice" class="com.test.advice.MyAdvice" />

    <!-- 织入 -->
    <aop:config>
          <!-- 定义切点-->
          <aop:pointcut id="pc" expression="execution(* com.test.service.impl.*.add(..))" />
          <!--切入 -->
          <aop:aspect ref="myAdvice">
              <!-- 将增强类对象myAdvice中的before方法切入到pc对应的切点所在的方法前面 -->
              <aop:before method="before" pointcut-ref="pc" />

              <aop:after method="after" pointcut-ref="pc" />

              <aop:around method="around" pointcut-ref="pc" />

              <aop:after-returning method="afterReturning" pointcut-ref="pc" />

              <aop:after-throwing method="afterThrowing" pointcut-ref="pc" />

          </aop:aspect>
    </aop:config>


</beans>

切面类

package com.test.advice;

import org.aspectj.lang.ProceedingJoinPoint;

//增强类
public class MyAdvice {

    //将这个增强方法切入到service层的add方法前
    public void before()
    {
        System.out.println("添加用户之前");
    }

    //目标方法执行后(不管是出异常还是成功执行)
    public void after()
    {
        System.out.println("添加用户之后");
    }
    //环绕通知,用这个增强代码替换掉目标方法
    public void around(ProceedingJoinPoint point) throws Throwable {
          System.out.println("执行目标方法之前");
          point.proceed(); //放行切点处的方法(目标方法)
    }

    //目标方法成功执行后
    public void afterReturning()
    {
       System.out.println("目标方法成功执行后");
    }

    //目标方法出异常
    public void afterThrowing()
    {
        System.out.println("目标方法出异常以后才执行");
    }

}

目标类

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("删除用户...");
    }
}

测试结果

javaee spring aop 的五种通知方式,Mac开发,java-ee,spring,java文章来源地址https://www.toymoban.com/news/detail-692540.html

依赖

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>testSpring06</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>testSpring06 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.Release</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.18.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- aop -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.10</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>testSpring06</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

到了这里,关于javaee spring aop 的五种通知方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • List集合的五种遍历方式

    目录 一、List五种遍历方式  1、普通for遍历 2、 增强for遍历  3、Lambda表达式  4、迭代器遍历  5、列表迭代器 方法 说明 迭代器遍历 在遍历的过程中需要删除元素,请使用迭代器。 列表迭代器 在遍历的过程中需要添加元素,请使用列表迭代器。 增强for遍历 仅仅想遍历,那

    2024年02月10日
    浏览(34)
  • 微前端:qiankun的五种通信方式

    今天盘点一下 qiankun 父子应用的通信方式都有哪些,我发现了 5 种。 1、localStorage/sessionStorage 2、通过路由参数共享 3、官方提供的 props 4、官方提供的 actions 5、使用vuex或redux管理状态,通过shared分享 接下来我们一个一个进行说明 有人说这个方案必须主应用和子应用是同一个

    2024年03月21日
    浏览(34)
  • Java遍历Map的五种方式

    java中遍历map一般有五种方法,从最早的Iterator,到java5支持的foreach,再到java8的Lambda表达式。 如果只是获取key,或者value,推荐使用keySet或者values方式 如果同时需要key或者value推荐使用entrySet 如果需要在遍历过程中删除元素推荐使用Iterator 如果需要在遍历过程中增加元素,可

    2024年02月03日
    浏览(41)
  • Vue路由跳转的五种方式

    路由跳转有两种形式:声明式导航、编程式导航 1. router-link 声明式 prop= :to=“…” 相当与 router.push(…) router-link中链接如果是’ / \\\'开始,就是从根路由开始 如果开始不带 ’ / \\\',则是从当前路由开始 例子 2. this.$router.push() 可追溯 编程式 router.push(…)//该方法的参数可以是一个

    2024年02月05日
    浏览(27)
  • 微信小程序传参的五种方式

    大家好,今天和大家分享一下微信小程序页面之间传参的五种方式,这个的话也是有人问了我一嘴,然后现在我和大家分享一下。 使用GPT搜索的话给我的答案是纯文字的描述,但是大致就是这样: URL参数传递:可以将参数直接拼接在页面的URL中,在打开目标页面时,通过获

    2024年02月06日
    浏览(29)
  • springboot实现跨域的五种方式

    出于浏览器的同源策略限制。同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。 同源策略 同源策略会

    2024年02月05日
    浏览(32)
  • MAC磁盘空间不足怎么清理?MAC清理磁盘空间的五种方法

    MAC磁盘空间不足怎么清理?当我们使用苹果MAC一段时间后,就会有大量的垃圾文件占用磁盘空间,例如系统缓存文件、应用程序缓存文件、备份和重复文件、旧版的应用程序及其部件等,为了不影响电脑的后续使用,我们需要经常清理磁盘空间,下面小编为大家带来了MAC清理

    2024年01月22日
    浏览(57)
  • Java调用python代码的五种方式

    你还在纠结怎么样在Java中调用python吗?我们在实际工程项目问题中,经常会碰到不同语言代码之间互调的问题,比如此处的Java调用python(常见Java调用python写的处理模型来完成数据处理等)。 让我们来看看具体怎么操作吧! 说明: Java调用不带参数的python代码执行 样例代码

    2024年02月09日
    浏览(40)
  • 前端传递对象,后台接收对象的五种方式

    前端传递对象,后台接收对象的方式主要有以下几种: 使用 @RequestBody 注解 :在 Spring Boot 控制器方法中使用 @RequestBody 注解将请求体中的 JSON 数据映射为 Java 对象。 使用 @ModelAttribute 注解 :在 Spring Boot 控制器方法中使用 @ModelAttribute 注解将 URL 参数或表单数据映射为 Java 对象

    2024年02月06日
    浏览(35)
  • 【SpringBoot】 启动后执行方法的五种方式

    在 SpringBoot 工程 启动后, 执行方法的五种方式: 1、实现 CommandLineRunner 接口 项目初始化完毕后,才会调用方法,提供服务 2、实现 ApplicationRunner 接口 同 CommandLineRunner。只是传参格式不一样。CommandLineRunner:没有任何限制;ApplicationRunner:key-value 3、实现 ApplicationListener 接口

    2023年04月08日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包