当前位置: 首页 > news >正文

007 spring aop(通知)(xml)

文章目录

    • pom.xml
    • LogAspect.java
    • StudentServiceImpl.java
    • StudentService.java
    • applicationContext.xml
    • StudentServiceImplTest.java

pom.xml


<?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><parent><groupId>com.aistart</groupId><artifactId>Spring_demo</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>Aspect_demo</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.3.30</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version></dependency></dependencies></project>

LogAspect.java

package com.aistart.aspect;import org.aspectj.lang.ProceedingJoinPoint;public class LogAspect {public void LogInfoAfter(){System.out.println("后置日志");}public void LogInfoBefore(){System.out.println("前置日志");}public void doAfterReturning(String result) {System.out.println("后置通知, 返回值: " + result);}public void doAfterThrowing(Exception e) {System.out.println("后置通知, 异常是: " + e);}public Object doAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println("-----------------------");System.out.println("环绕通知: 进入方法");Object o = pjp.proceed();System.out.println("环绕通知: 退出方法");return o;}
}

StudentServiceImpl.java

package com.aistart.service.impl;import com.aistart.service.StudentService;
import org.springframework.stereotype.Service;@Service
public class StudentServiceImpl implements StudentService {@Overridepublic void insertStudent() {System.out.println("加入一个学生的业务");
//        System.out.println(1/0);}
}

StudentService.java


package com.aistart.service;public interface StudentService {void insertStudent();
}

applicationContext.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 https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.aistart.service"></context:component-scan><context:annotation-config></context:annotation-config>
<!--    <bean id="studentService" class="com.aistart.service.impl.StudentServiceImpl"/>--><!--增强类/切面对象--><bean id="logAspect" class="com.aistart.aspect.LogAspect"/><!-- bean definitions here --><aop:config><aop:aspect ref="logAspect"><!--代理的功能--><!--execution([修饰符] 返回类型 全限类名.函数名() )--><aop:pointcut id="studentServiceCut" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/><aop:pointcut id="studentServiceCut1" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/><aop:before method="LogInfoBefore" pointcut-ref="studentServiceCut"></aop:before><aop:after method="LogInfoAfter" pointcut-ref="studentServiceCut"></aop:after><aop:around method="doAround" pointcut-ref="studentServiceCut"></aop:around><aop:after-throwing throwing="e" method="doAfterThrowing" pointcut-ref="studentServiceCut"></aop:after-throwing><aop:after-returning returning="result" method="doAfterReturning" pointcut-ref="studentServiceCut"></aop:after-returning></aop:aspect></aop:config></beans>

StudentServiceImplTest.java


package com.aistart.service.impl;import com.aistart.service.StudentService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import static org.junit.Assert.*;public class StudentServiceImplTest {@Testpublic void insertStudent() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");StudentService studentService = context.getBean( StudentService.class);studentService.insertStudent();}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • fastlio2 保存每帧的点云和每帧的里程计为单独的文件做后端回环优化和手动回环优化
  • 整数和(推公式)
  • MongoDB复制集安装配置图文教程-Windows篇 fasstgpt必看
  • Rust---复合数据类型之字符串与切片(2)
  • LangChain - PromptTemplate
  • k8s删除namespace失败一直处于Terminating状态
  • 【第十一届大唐杯全国大学生新一代信息通信技术大赛】赛题分析
  • 支持向量机(SVM)白话之个人理解(学习记录)
  • 初学ELK - elk部署
  • 素数判定(HDU2012)
  • 利用ES6 Set去重
  • rust实现一个post小程序
  • MySQL 多表查询
  • 二叉树的遍历的递归与非递归算法
  • 【C语言】结构体、枚举、联合(自定义类型)
  • [PHP内核探索]PHP中的哈希表
  • Google 是如何开发 Web 框架的
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • CAP 一致性协议及应用解析
  • Computed property XXX was assigned to but it has no setter
  • HomeBrew常规使用教程
  • input的行数自动增减
  • Java读取Properties文件的六种方法
  • js正则,这点儿就够用了
  • mac修复ab及siege安装
  • React Native移动开发实战-3-实现页面间的数据传递
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • SQLServer插入数据
  • Sublime Text 2/3 绑定Eclipse快捷键
  • XForms - 更强大的Form
  • 分布式熔断降级平台aegis
  • 和 || 运算
  • 基于Android乐音识别(2)
  • 码农张的Bug人生 - 初来乍到
  • 使用 Docker 部署 Spring Boot项目
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • 仓管云——企业云erp功能有哪些?
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • ​​​【收录 Hello 算法】10.4 哈希优化策略
  • # Spring Cloud Alibaba Nacos_配置中心与服务发现(四)
  • #define
  • (day 12)JavaScript学习笔记(数组3)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (附源码)计算机毕业设计大学生兼职系统
  • (含笔试题)深度解析数据在内存中的存储
  • (接口自动化)Python3操作MySQL数据库
  • (没学懂,待填坑)【动态规划】数位动态规划
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (排序详解之 堆排序)
  • (四)模仿学习-完成后台管理页面查询
  • (一)基于IDEA的JAVA基础1
  • (已解决)报错:Could not load the Qt platform plugin “xcb“