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 是真正的业务代码.我们可 ...
随机推荐
- ndk编译libpcap 1.7.4
android完全菜鸟,绝对的第一次接触,想做手机抓包,在网上搜又是NDK 又是JNI 又是JNETPCAP 完全蒙了,让我这种android和java都弄不明白什么关系的人情何以堪! 静下心想一想, ...
- 洛谷 2471 BZOJ 1067 [SCOI2007]降雨量
[题解] 用线段树维护区间最大值(因为没有修改,St表也可以),然后由于x,y可能是降雨量未知的年份,需要进行分类讨论. #include<cstdio> #include<algo ...
- ZOJ 3684 Destroy
Destroy Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...
- [K/3Cloud]K3Cloud平台开发之Python插件
有时候我们的表单可能很简单,只是一个简单交互的表单,但有可能还是要劳师动众的给它建个工程写个插件,是不是很不爽?例如我有如下一个表单: 功能很简单就是选个业务对象,收集绑定几个字段名,然后确定返回一个 ...
- oracle 如何查看创建表等数据库对象时的DDL语句
http://missyou4417.blog.163.com/blog/static/78905686201271041340284/ http://www.xifenfei.com/2012/05 ...
- Ubuntu 16.04添加阿里云源/163源
添加国内源有个好处,比如下载软件时直接时国内的服务器,速度有保证. 以下是操作方法: 1.备份 sudo cp /etc/apt/sources.list /etc/apt/sources.list. ...
- IPV6相关RFC文档
1. 通用 IPv6的通用RFC和Internet草案 RFC# 类 标题 1752 标准记录 对IP下一代协议的建议 1924 资料 IPv6地址的压缩表示法 2851 标准记录 Internet网 ...
- DCS实践干货:使用Redis实现分布式锁
场景介绍 很多互联网场景(如商品秒杀,论坛回帖盖楼等),需要用加锁的方式,以对某种资源进行顺序访问控制.如果应用服务集群部署,则涉及到对分布式应用加锁.当前分布式加锁主要有三种方式:(磁盘)数据库.缓 ...
- android 4.0主线程訪问网络问题
在4.0下面,在主线程中訪问网络,假设请求超过6s的话,就会报ANR,那么这就会带来一个问题,假设网络慢或者请求的数据过大时,界面会卡顿,造成界面灵敏性非常差,因此网络请求一般不能放在主线程中操作,g ...
- 如何在 webpack 项目中使用绝对路径
react 小白编程 本项目是使用 create-react-app 脚手架构建的react项目 找到 config 配置文件下的 webpack.config.dev.js 和 webpack.co ...