spring aop的使用(注解方式以及基于xml配置方式)

Spring之AOP:基于XML或注解的配置方式 文章目录前言一、AOP是什么?二、AOP的术语三、基于XML文件的配置方法1.添加依赖2.xml文件配置3.测试结果4.环绕通知5.测试结果四、基于注解的配置方法1.新增XML文件2.测试结果五、完全不使用XML文件的注解配置 前言 阅读文章前最好已经掌握了Java的动态代理和spring的IoC。 一、AOP是什么? 百度百科:可以通过预编译方式和运行其动态代理实现在不修改源代码的情况下给程序动态统一添加某种特定功能的一种技术。 两个关键词,不修改源码、统一添加。 二、AOP的术语 先用一个动态代理的 阅读详情

转载的。。原文地址

http://blog.youkuaiyun.com/xzf19901108/article/details/7835558

注解方式


*******************

beans.xml

*******************

[html]  view plain copy print ?
  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:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="  
  7.            http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context   
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.            ">  
  14.              
  15.           <aop:aspectj-autoproxy/>  
  16.           <bean id="myInterceptor" class="blog.service.MyInterceptor"/>  
  17.           <bean id="personService" class="blog.service.impl.PersonServiceBean"></bean>  
  18.             
  19. </beans>  



***************

MyInterceptor.java

***************

[java]  view plain copy print ?
  1. package blog.service;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11.   
  12. /** 
  13.  * 切面 
  14.  * 
  15.  */  
  16. @Aspect  
  17. public class MyInterceptor {  
  18.     @Pointcut("execution (* blog.service.impl.PersonServiceBean.*(..))")  
  19.     public void anyMethod() {  
  20.         //表达式解释:* blog.service.impl.PersonServiceBean.*(..):第一个*号表示返回类型;  
  21.         //blog.service.impl.PersonServiceBean.*:表示PersonServiceBean类下的所有方法  
  22.         //blog.service.impl..*.*:表示blog.service.impl包及其子包下的所有类的所有方法  
  23.         //(..):表示所有的参数类型几个数都不限  
  24.     }// 声明一个切入点  
  25.   
  26.     //会拦截参数签名位 (String , int)或(String , Integer )类型的方法,  
  27.     //参数顺序与args(uname,id)一致,参数类型由(int id,String uname)决定  
  28.     @Before("anyMethod() && args(uname,id)")  
  29.     public void doAccessCheck(int id,String uname) {  
  30.         System.out.println("前置通知:" + uname + id);  
  31.     }  
  32.   
  33.     @AfterReturning(pointcut="anyMethod()",returning="result")  
  34.     public void doAfterReturning(String result) {  
  35.         System.out.println("后置通知:" + result);  
  36.     }  
  37.   
  38.     @After("anyMethod()")  
  39.     public void doAfter() {  
  40.         System.out.println("最终通知");  
  41.     }  
  42.   
  43.     @AfterThrowing(pointcut="anyMethod()",throwing="e")  
  44.     public void doAfterThrowing(Exception e) {  
  45.         System.out.println("例外通知:" + e.getMessage());  
  46.     }  
  47.       
  48.     @Around("anyMethod()")  
  49.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  50.         //if(){//判断用户是否有权限  
  51.         System.out.println("进入方法");  
  52.         Object result = pjp.proceed();  
  53.         System.out.println("退出方法");  
  54.         //}  
  55.         return result;  
  56.     }  
  57. }  


***************

PersonService.java

***************

[java]  view plain copy print ?
  1. package blog.service;  
  2.   
  3.   
  4. public interface PersonService {  
  5.     public String save(String name);  
  6.     public String update(String name,Integer userId);  
  7.     public String delete(int id,String dept);  
  8.       
  9. }  


***************

PersonServiceBean.java

***************

[java]  view plain copy print ?
  1. package blog.service.impl;  
  2.   
  3. import blog.service.PersonService;  
  4.   
  5. public class PersonServiceBean implements PersonService {  
  6.     private String username = null;  
  7.   
  8.     public String getUsername() {  
  9.         return username;  
  10.     }  
  11.   
  12.     public PersonServiceBean() {  
  13.     }  
  14.   
  15.     public PersonServiceBean(String username) {  
  16.         this.username = username;  
  17.     }  
  18.   
  19.     @Override  
  20.     public String save(String name) {  
  21.         if (name.equals("") || name == null) {  
  22.             throw new RuntimeException("出错啦");  
  23.         }  
  24.         System.out.println("in save method!" + name);  
  25.   
  26.         return "in save method!" + name;  
  27.     }  
  28.   
  29.     @Override  
  30.     public String update(String name, Integer userId) {  
  31.         System.out.println("in update method! name = " + name + " id = "  
  32.                 + userId + " username = " + username);  
  33.         return "in update method!name = " + name + " id = " + userId;  
  34.     }  
  35.   
  36.     @Override  
  37.     public String delete(int id, String dept) {  
  38.         System.out.println("id = " + id + " dept = " +  dept);  
  39.         return "in delete method! + nameid = " + id + " dept = " +  dept;  
  40.     }  
  41.   
  42. }  


***************

SpringAOPTest.java

***************

[java]  view plain copy print ?
  1. package junitTest;  
  2.   
  3.   
  4. import org.junit.BeforeClass;  
  5. import org.junit.Test;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9. import blog.service.PersonService;  
  10.   
  11. public class SpringAOPTest {  
  12.   
  13.     @BeforeClass  
  14.     public static void setUpBeforeClass() throws Exception {  
  15.     }  
  16.       
  17.     @Test  
  18.     public void AopTest(){  
  19.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
  20.         PersonService service = (PersonService)ctx.getBean("personService");  
  21.         try {  
  22.               
  23.             service.delete(12, "it");  
  24.             service.update("发多个地方",324);  
  25.             service.save("");  
  26.               
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.   
  32. }  


***************

运行结果

***************



基于xml配置方式



*************

beans.xml

************

[html]  view plain copy print ?
  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:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="  
  7.            http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context   
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.            ">  
  14.              
  15.           <aop:aspectj-autoproxy/>  
  16.           <bean id="myInterceptor" class="blog.service.MyInterceptor"/>  
  17.           <bean id="personService" class="blog.service.impl.PersonServiceBean"></bean>  
  18.             
  19.           <aop:config>  
  20.             <aop:aspect id="aspectBean" ref="myInterceptor" >  
  21.                 <aop:pointcut  id="point"  expression="execution (!void blog.service.impl.PersonServiceBean.*(..))"/>  
  22.                 <aop:before method="doAccessCheck" pointcut-ref="point" />  
  23.                 <aop:after-returning method="doAfterReturning" pointcut-ref="point" />  
  24.                 <aop:after-throwing method="doAfterThrowing" pointcut-ref="point"/>  
  25.                 <aop:after method="doAfter" pointcut-ref="point"/>  
  26.                 <aop:around method="doBasicProfiling" pointcut-ref="point" />  
  27.                   
  28.             </aop:aspect>  
  29.           </aop:config>  
  30.             
  31. </beans>  

*******************

MyInterceptor.java

*******************

[java]  view plain copy print ?
  1. package blog.service;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4.   
  5.   
  6. public class MyInterceptor {  
  7.     public void anyMethod() {  
  8.     }  
  9.   
  10.     public void doAccessCheck() {  
  11.         System.out.println("前置通知" );  
  12.     }  
  13.   
  14.     public void doAfterReturning() {  
  15.         System.out.println("后置通知");  
  16.     }  
  17.   
  18.     public void doAfter() {  
  19.         System.out.println("最终通知");  
  20.     }  
  21.   
  22.     public void doAfterThrowing() {  
  23.         System.out.println("例外通知");  
  24.     }  
  25.       
  26.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  27.         //if(){//判断用户是否有权限  
  28.         System.out.println("进入方法");  
  29.         Object result = pjp.proceed();  
  30.         System.out.println("退出方法");  
  31.         //}  
  32.         return result;  
  33.     }  
  34. }  


aop表达式的使用方法

<aop:pointcut  id="point"  expression="execution (!void blog.service.impl.PersonServiceBean.*(..))"/>

!void blog.service.impl.PersonServiceBean.*(..)中!void 表示拦截所有返回类型为非void类型的方法

java.lang.String表示拦截所有返回类型为String类型的方法

* blog.service.impl.PersonServiceBean.*(java.lang.String,..)表示拦截所有第一个参数为String类型的方法


Spring AOP配置实战:XML与注解方式详解 Spring AOP(面向切面编程)是一种编程范式,旨在提高代码的模块化,通过将横切关注点(如日志记录、权限控制、事务管理)从业务逻辑中分离出来。其核心概念包括:切面(Aspect):一个模块化的组件,封装了横切逻辑,例如日志切面。连接点(Join Point):程序执行过程中的某个点,如方法调用或异常抛出。切入点(Pointcut):定义哪些连接点将被切面处理,通常通过表达式匹配方法。通知(Advice):切面在特定连接点上执行的动作,如前置通知、后置通知等。 阅读详情

相关推荐

SpringDoc 基本使用指南

SpringDoc 凭借零配置启动注解简洁和深度兼容 Spring 生态的优势,已成为 Spring Boot API 文档的首选工具。自动化- 减少手动维护文档的成本;标准化- 严格遵循 OpenAPI 3 规范;可扩展- 分组、安全控制灵活适配复杂项目。访问即可查看交互式文档(默认路径)

墨鸦的博客 1万+

Spring中基于配置XML与Annotation注解配置AOP

Spring中基于配置XML与Annotation注解配置AOP 基于AspectJ注解的AOP实现   定义bean-productInfoService类   定义日志切面bean   定义切面   定义切入点  Pointcut(“execution(*com.shw.service.ProductInfoServic

qq_34639724的博客 823

Isograph Reliability Workbench 14实战:从零构建故障树分析模型

本文以“马达不转”为例,手把手教你使用Isograph Reliability Workbench 14从零构建故障树分析模型。内容涵盖创建工程、定义顶事件、添加逻辑门与基础事件等核心步骤,并介绍了模型检查与定性定量分析,帮助你系统掌握这一强大的可靠性工程工具。

weixin_29309213的博客 362

Java程序员从笨鸟到菜鸟之(七十四)细谈Spring(六)spring之AOP基本概念和配置详解...

首先我们来看一下官方文档所给我们的关于AOP的一些概念性词语的解释: 切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在SpringAOP中,切面可以使用基于模式)或者基于Aspect注解方式来实现。通俗点说就是我们加入的切面类(比如log类),可以这么理解。 连接点(Joinpoint):在程序执行过程...

红太阳照大地 281

别再傻傻分不清了!用Design Compiler和PrimeTime实操对比PBA与GBA模式

本文深度解析静态时序分析(STA)中Design Compiler与PrimeTime工具的PBA与GBA模式差异,通过实际案例演示两种模式在转换时间计算、时钟路径分析等方面的对比,并提供工程实践中模式选择的策略与调试技巧,帮助工程师在芯片设计流程中平衡精度与效率。

weixin_42552315的博客 415

【芯片验证随心记】1.SystemVerilog中,rand和randc的区别?如何用constraint实现randc?

在SystemVerilog里,randc 的“不重复循环”特性是用一个 周期性排列器(permutation engine) 实现的,而这个引擎在 LRM 里并没有公开。我们只能在 “用户空间” 用 rand + constraint 去模仿它的行为。每次随机化只取 queue.pop_front(),用完再洗。在4次随机化内,y会依次出现0,1,2,3各一次(顺序随机)每次随机化,x可能是0-3中的任意值,可能连续多次相同。每次随机化时,在未使用的值里随机挑一个;第5次随机化时,重新生成新的排列周期。

qq_43172512的博客 342

Spring中的AOP(四)——基于Annotation的配置方式(二)

为什么80%的码农都做不了架构师?>>> ...

weixin_34174105的博客 240

机器学习实战4-教育领域:学生成绩的可视化分析与成绩预测-详细分析

大家好,我是微学AI,今天给大家带来机器学习实战4-学生成绩的可视化分析与成绩预测,机器学习在教育中的应用具有很大的潜力,特别是在学生成绩的可视化分析与成绩预测方面。机器学习可以通过对学生的父母教育情况和学校表现等数据进行分析和挖掘,从而揭示潜在的学习模式和趋势。

微学AI的博客 7678

Spring AOP三种配置方式深度解析:XML、注解与纯注解实战指南

摘要:本文全面解析Spring AOP的三种配置方式。XML配置实现完全解耦但冗长;注解配置平衡灵活性,需结合XML启用;纯注解方式最为推荐,提供类型安全和重构支持。三者在类型安全、重构友好度等方面各具特点:XML适用于遗留系统,注解适合传统项目,纯注解是SpringBoot首选。文章通过代码示例对比分析,建议新项目优先采用纯注解,老项目逐步迁移,并提供了性能优化、切面组织等最佳实践。AOP的本质是通过关注点分离提升系统可维护性,合理运用可显著改善代码质量。

weixin_62938484的博客 1151

【Matlab】多旋翼无人机视觉伺服跟踪控制程序

本文介绍了基于Matlab的多旋翼无人机视觉伺服跟踪控制程序开发方法。该系统采用视觉感知、坐标转换、伺服控制和仿真验证四个核心模块,通过HOG+SVM目标检测、Shi-Tomasi特征提取和PID双闭环控制算法实现动态目标跟踪。详细阐述了相机标定、目标检测、坐标转换及伺服控制等关键环节的Matlab实现代码,并进行了仿真验证。结果表明该系统在±0.1m的跟踪精度下可实现100Hz的实时处理,适用于室内外复杂场景。文章还提出了工程应用中的优化建议,为无人机视觉伺服系统的开发提供了完整的技术方案。

unber的博客 507

YOLOv8-Pose NCNN Android部署优化实践

本文探讨了在Android设备上高效部署YOLOv8-Pose姿态检测模型的优化实践。通过修改Pose头部导出逻辑,分离后处理到C++端实现,并替换SiLU激活函数为ReLU,显著提升了计算效率。具体步骤包括模型结构优化、ONNX导出、NCNN转换与算子融合,最终在Android端实现了30FPS的稳定推理性能。项目提供了完整的优化思路和实现方案,对移动端AI应用开发具有参考价值。

FJN110的博客 323

Spring AOP 两种配置方式详解:XML 与注解

本文对比了传统编程与AOP(面向切面编程)的实现差异,以登录功能为例展示了AOP将验证逻辑与业务逻辑解耦的优势。重点介绍了Spring AOP的两种实现方式:XML配置和注解配置。通过Demo类示例,详细说明了如何定义切面类(包含五种通知类型)、配置切入点表达式,并提供了完整的测试代码和运行结果。文章还列出了必要的Maven依赖,对比了两种配置方式的适用场景,XML适合大型项目管理,注解更适合快速开发。最后强调应根据项目需求选择合适的AOP实现方式,以提高代码的可维护性和扩展性。

m0_62923286的博客 607

用 OP-TEE 给 AI 模型“上锁”:密文存储、TEE 解密放行、推理后销毁(实战可落地)

本文提出一种基于OP-TEE的AI模型保护方案,通过两级密钥体系确保模型静态存储为密文,运行时仅在TEE内解密。方案采用KEK+DEK架构,DEK永不出TEE,支持分块解密与可验证擦除。该方案能有效防御静态分析、应用层逆向等威胁,但需承认其物理级攻击的局限性。文章详细描述了端到端架构、文件格式设计及TA实现要点,包括密钥管理、分块解密策略和销毁机制,为设备侧AI部署提供了工程可行的安全解决方案。

Interview_TC的博客 289

spring aop注解与xml配置两种方式

spring aop切面两种使用方式: 1.使用xml配置文件的方式,个人感觉代码比较清晰,能够体现出明显的层次感 xml文件配置的方式开启aop 我创建的是普通java工程,用到的相关的jar包 和测试相关类:包可以创建maven配置依赖后下载下来自己归类使用 目的就是在指定切点运行指定切面,xml相关配置: 需要注意的是:切面的ref与bean的name属性一致 切点的expression写...

weixin_43671743的博客 383

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Spring-06 Xml和注解方式配置Aop

在 spring 核心配置文件中添加 aop 约束。

linyuancsdn的博客 506

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

java aop xml配置_Spring AOP 的两种配置方式 XML和注解配置

原标题:Spring AOP 的两种配置方式 XML和注解配置 一注解配置AOP注解配置AOP,大致分为三步:1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).2. 开发需要被拦截的类。3. 将切面配置到xml中,当然,我们也可以使用自动扫描...

weixin_42398629的博客 188

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

spring的AOP(基于xml配置和注解的实现方式)

1.基于xml的AOP 实现步骤 导入jar依赖坐标:spring-context, aspectjweaver 编写目标类、编写通知类 配置切面:在xml里配置 <!--配置AOP--> <aop:config> <!-- 定义一个切入点表达式 --> <aop:pointcut id="pc" expression="execution(切入点表达式)"/> .

m0_58615792的博客 417

spring之aop编程——基于注解、xml配置文件方式

AOP(Aspect Oriented Programming),是面向切面编程的技术。AOP基于IoC基础,是对OOP的有益补充。spring中AOP的配置方式有2种方式:xml配置和AspectJ注解方式。 一、xml配置的方式: 1、service接口和服务类: package cn.edu.nuc.SpringTest.service; public interface D

赶路人儿 3003

实例说明Spring实现AOP的2种方式(注解和XML配置)

本文说到spring的配置,常见的有两种:注解方式和xml文件配置方式 首先, 建立model类UserManager,代码如下: package com.spring.model; public class UserManager { private int id; private String name; private String password;

yyy 1166

(11)Spring框架----基于@AspectJ注解方式和基于XML配置方式开发AOP

AOP并不是Spring框架特有的,Spring只是支持AOP编程的框架之一。每一个框架对AOP的支持各有特点,有些AOP能够对方法的参数进行拦截,有些AOP对方法进行拦截。而Spring AOP是一种基于方法拦截的AOP,换句话说Spring只能支持方法拦截的AOP。在Spring中有4中方式去实现AOP的拦截功能。 ❤使用ProxyFactoryBean和对应的接口实现AOP ❤使用XML...

应无所住而生其心 1493

Spring AOP 注解配置方式和XML配置方式

jar包 拿接口配置切点时,要注意execution里的路径 一. @Aspect 注解配置 @component注解注释bean @aspect注解注释 切面类 <context:component-scan base-package="com.qyc.aop"></context:component-scan> 开启注解扫描 <aop:aspec...

qq_41835813的博客 298
上一篇: FF浏览器嵌套div中margin-top转移问题的解决办法
下一篇: [spring]Spring MVC web.xml 配置文件配置
wendellup
博客等级 码龄16年 54粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值