applicationContext.xml配置AOP切面编程
Computer.java
package com.wh.aop2;
public class Computer {
public void play01(){
System.out.println("一号玩家!");
}
public void play02(){
System.out.println("二号玩家!");
System.out.println(10/0);
}
public void play03(){
System.out.println("三号玩家!");
}
public void play04(){
System.out.println("四号玩家!");
}
public void play05(){
System.out.println("五号玩家!");
}
public void play06(){
System.out.println("六号玩家!");
}
}
AopProxy.java
package com.wh.aop2; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class AopProxy { public void doBefore01() {
System.out.println("无参数前置通知:");
} public void doBefore02() {
System.out.println("有参数前置通知:");
} public void doAround(ProceedingJoinPoint p) {
System.out.println("环绕之前置通知:");
Object obj = null;
try {
obj = p.proceed();
System.out.println("环绕之后置通知: " + obj);
}
catch (Throwable e) {
System.out.println("环绕之异常通知: " + e.getMessage());
}
finally {
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer" class="com.wh.aop.Computer"/>
<bean id="testBefore" class="com.wh.aop.TestBefore"/> <aop:config>
<aop:pointcut expression="execution(* com.wh.aop.Computer.add(*,*))" id="computerAddCut"/>
<aop:pointcut expression="execution(* com.wh.aop.Computer.div(*,*))" id="computerDivCut"/>
<!-- AOP前置通知 -->
<aop:aspect ref="testBefore">
<aop:before method="doBefore" pointcut-ref="computerAddCut"/>
</aop:aspect>
<!-- AOP后置通知 -->
<aop:aspect ref="testBefore">
<aop:after-returning method="doAfter" pointcut-ref="computerAddCut" returning="ret"/>
</aop:aspect>
<!-- AOP异常通知 -->
<aop:aspect ref="testBefore">
<aop:after-throwing method="doThrow" pointcut-ref="computerDivCut" throwing="e"/>
</aop:aspect>
</aop:config> <import resource="applicationContext2.xml"/> </beans>
applicationContext2.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer2" class="com.wh.aop2.Computer"/>
<bean id="aopProxy2" class="com.wh.aop2.AopProxy"/>
<aop:config>
<aop:pointcut expression="execution(* com.wh.aop2.Computer.*(..))" id="computerCut2"/>
<aop:aspect ref="aopProxy2">
<aop:around method="doAround" pointcut-ref="computerCut2"/>
</aop:aspect>
</aop:config> </beans>
TestAop.java
package com.wh.aop2; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test
public void test01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play01();
} @Test
public void testAround(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play01();
}
/**
环绕之前置通知:
一号玩家!
环绕之后置通知: null
环绕之最终通知:
*/ @Test
public void testAround2(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play02();
}
/**
环绕之前置通知:
二号玩家!
环绕之异常通知: / by zero
环绕之最终通知:
*/
//小结:在环绕通知中:正常情况下,四个通知都会出现,若出现异常,只有后置通知不会出现!
//通知顺序为:前置、异常、最终、后置
}
applicationContext.xml配置AOP切面编程的更多相关文章
- 注解配置AOP切面编程
1.导入先关jar包 2.编写applicationContext.xml,配置开启注解扫描和切面注解扫描 <?xml version="1.0" encoding=&quo ...
- SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...
- Spring MVC通过AOP切面编程 来拦截controller 实现日志的写入
首选需要参考的是:[参考]http://www.cnblogs.com/guokai870510826/p/5977948.html http://www.cnblogs.com/guokai8 ...
- Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- springmvc.xml和applicationContext.xml配置的特点
1:springmvc.xml配置要点 一般它主要配置Controller的组件扫描器和视图解析器 下为:springmvc.xml文件 <?xml version="1.0" ...
- AOP切面编程在android上的应用
代码地址如下:http://www.demodashi.com/demo/12563.html 前言 切面编程一直是一个热点的话题,这篇文章讲讲一个第三方aop库在android上的应用.第三方AOP ...
- 注解与AOP切面编程实现redis缓存与数据库查询的解耦
一般缓存与数据库的配合使用是这样的. 1.查询缓存中是否有数据. 2.缓存中无数据,查询数据库. 3.把数据库数据插入到缓存中. 其实我们发现 1,3 都是固定的套路,只有2 是真正的业务代码.我们可 ...
随机推荐
- 正确的在循环list的时候删除list里面的元素
s = [1,2,3,4,5] for i in s: s.remove(i) print(s) 输出结果:[2, 4] 1.当第一次删除后,后面的元素会前移,此时s=[2,3,4,5], 2.然 ...
- 工作用linux命令汇总
mv [filepath] [filepath] 移动,前者位置移动到后面位置,也可以用来重命名(mv test.txt newname.txt) cp [filepath] [filepath] 复 ...
- mysql 查询当天、本周,本月,上一个月的数据---https://www.cnblogs.com/benefitworld/p/5832897.html
mysql 查询当天.本周,本月,上一个月的数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM ...
- Android layer-list(3)
Android layer-list(3) 在附录文章3.4的基础上,就Android layer-list再写一个较为复杂的应用. 先写布局文件,该布局涉及到LinearLayoutCompa ...
- ACM 数学
欧几里得辗转相除法求最大公约数 int gcd(int a,int b) { ) return a; else return gcd(b,a%b); } 求组合数 int C(int n ,int m ...
- [bzoj1115][POI2009]石子游戏Kam_博弈论_阶梯博弈
石子游戏 Kam bzoj-1115 POI-2009 题目大意:给定n堆石子,两个人轮流取石子.每堆石子的个数都不少于前一堆石子.每次取后也必须维持这个性质.问谁有必胜策略. 注释:$1\le ca ...
- NETCore项目报错 An error occurred while starting the application
在发布到IIS的webApi项目中,运行时报出以上错误, 解决方法: 1.打开发布目录文件夹,找到web.config文件 2.打开web.config找到stdoutLogEnabled=" ...
- 导入数据到mysql的一种简单的方法
由于ubuntu默认自带的mysql版本号为5.5,并不能使用load data infile这样的高级的功能,因此我们写了一个通用的脚本来上传文件 shell脚本 cat ./employee.cs ...
- Node.js 博客实例(三)添加文件上传功能
原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第三章 上传文件眼下有三种方法: 使用 Express 自带的文件上传功能,不涉及数据库 使用 For ...
- windows下检測文件改变
这个主要是应用在我前一篇博客里提到的脚本热载入功能. 主要实现的功能检測目录内文件的变化(改变.新增.删除.重命名),当发现改变的时候通知lua又一次载入脚本.基本上就是一个windows api的使 ...