java AOP使用配置项来进行注入实践
场景:
在目标方法前面和后面执行通知方法
目标类
@Component
public class Play { public void watchTV(){
System.out.println("目标执行方法,正在看电视");
}
}
切面类
public class WatchTVAspect { public void beforeAdvice(){
System.out.println("切点函数执行前的方法");
} public void afterAdvice(){
System.out.println("切点函数执行后的方法");
} public void afterReturning(){
System.out.println("目标方法返回时执行 ,后置返回通知");
} public void afterThrowing(){
System.out.println("目标方法抛出异常时执行 异常通知");
} public void around(){
System.out.println("在目标函数执行中执行,可控制目标函数是否执行,环绕通知");
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.mb"></context:component-scan> <bean id="watchTv" class="com.mb.util.WatchTVAspect"></bean>
<aop:config>
<aop:aspect id="check" ref="watchTv">
<aop:pointcut id="accountPoint" expression="execution(* com.mb.common.Play.*(..))"></aop:pointcut>
<aop:before pointcut-ref="accountPoint" method="beforeAdvice"></aop:before>
<aop:after pointcut-ref="accountPoint" method="afterAdvice"></aop:after>
<!--<aop:around method="around" pointcut-ref="accountPoint" ></aop:around>-->
<!--<aop:after-returning method="afterReturning" pointcut-ref="accountPoint"></aop:after-returning>-->
<!--<aop:after-throwing method="afterThrowing" pointcut-ref="accountPoint"></aop:after-throwing>-->
</aop:aspect>
</aop:config>
</beans>
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:**/applicationContext*.xml"})
public class DemoTest { @Autowired
Play play; @Test
public void aspectTest(){
play.watchTV(); }
} 测试结果:
切点函数执行前的方法
目标执行方法,正在看电视
切点函数执行后的方法
java AOP使用配置项来进行注入实践的更多相关文章
- java AOP使用注解@annotation方式实践
java AOP使用配置项来进行注入实践 AOP实际开发工作比较常用,在此使用注解方式加深对面向切面编程的理解 废话不多少,先看下面的实例代码 场景: 1.未满一岁的小孩,在执行方法之前打印:“ ...
- 编译时MSIL注入--实践Mono Cecil(1)
原文:编译时MSIL注入--实践Mono Cecil(1) 紧接上两篇浅谈.NET编译时注入(C#-->IL)和浅谈VS编译自定义编译任务—MSBuild Task(csproject),在第一 ...
- Java AOP的底层实现原理
Java AOP的底层实现原理 一.什么是AOP 1.AOP:Aspect Oriented Programming(面向切面编程),OOP是面向对象编程,AOP是在OOP基础之上的一种更高级的设计思 ...
- attilax.java 注解的本质and 使用最佳实践(3)O7
attilax.java 注解的本质and 使用最佳实践(3)O7 1. 定义pojo 1 2. 建立注解By eclipse tps 1 3. 注解参数的可支持数据类型: 2 4. 注解处理器 2 ...
- paip.java win程序迁移linux的最佳实践
paip.java win程序迁移linux的最佳实践 1.class load路径的问题... windows哈第一的从calsses目录加载,,而linux优先从jar加载.. 特别的是修理了ja ...
- 【转】Java中关于异常处理的十个最佳实践
原文地址:http://www.searchsoa.com.cn/showcontent_71960.htm 导读:异常处理是书写强健Java应用的一个重要部分,Java许你创建新的异常,并通过使用 ...
- Java AOP (1) compile time weaving 【Java 切面编程 (1) 编译期织入】
According to wikipedia aspect-oriented programming (AOP) is a programming paradigm that aims to inc ...
- Java AOP (2) runtime weaving 【Java 切面编程 (2) 运行时织入】
接上一篇 Java AOP (1) compile time weaving [Java 切面编程 (1) 编译期织入] Dynamic proxy 动态代理 Befor talking abou ...
- 【转载】以Java的视角来聊聊SQL注入
以Java的视角来聊聊SQL注入 原创 2017-08-08 javatiku Java面试那些事儿 在大二就接触过sql注入,之前一直在学习windows逆向技术,认为web安全以后不是自己的从业方 ...
随机推荐
- Spring中 @Autowired标签与 @Resource标签 的区别
http://blog.csdn.net/angus_17/article/details/7543478 http://bbs.csdn.net/topics/390175654 https://w ...
- 深入理解 Laravel 中 config 配置加载原理
Laravel的配置加载其实就是加载config目录下所有文件配置.如何过使用php artisan config:cache则会把加载的配置合并到一个配置文件中,下次请求就不会再去加载config目 ...
- AC自动机-HDU2222-模板题
http://acm.hdu.edu.cn/showproblem.php?pid=2222 一个AC自动机的模板题.用的kuangbin的模板,静态建Trie树.可能遇到MLE的情况要转动态建树. ...
- python成长之路七-函数的进阶
1,python中,名称空间分三种: 全局命名空间 局部命名空间(临时命名空间) 内置名称空间 2,作用域(两种): 1,全局作用域 包含:全局名称空间 内置名称空间 2,局部作用域 包含:局 ...
- python成长之路五-文件操作
1,文件操作 f = open("D:\种子.txt",encoding="utf-8",mode="r") # 打开一个种子.txt文件, ...
- BZOJ5337 [TJOI2018] 碱基序列 【哈希】【动态规划】
题目分析: 这道题的难点在于要取模,而题面没有写. 容易想到一个O(1E7)的dp.KMP或者哈希得到相关位置然后对于相关位置判断上一个位置有多少种情况. 代码: #include<bits/s ...
- 【Luogu4723】线性递推(常系数齐次线性递推)
[Luogu4723]线性递推(常系数齐次线性递推) 题面 洛谷 题解 板子题QwQ,注意多项式除法那里每个多项式的系数,调了一天. #include<iostream> #include ...
- 【转】__int64 与long long 的区别
//为了和DSP兼容,TSint64和TUint64设置成TSint40和TUint40一样的数 //结果VC中还是认为是32位的,显然不合适 //typedef signed long int ...
- 洛谷 P4074 [WC2013]糖果公园 解题报告
P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...
- BSGS
北上广深/拔山盖世算法. yaT+b = z mod p p为质数,Hash表存b,枚举a,复杂度p0.5 记得特判y = 0的情况. inline void solve3() { Hash::cle ...