xml文件的方式实现动态代理基于SpringAOP
1.配置spring容器
导入jar包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
spring-aop-4.2.2.RELEASE.jar
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
配置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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="arithmeticCaludaImpXML" class="com.eduask.liusheng.aop.xml.ArithmeticCaludaImp"></bean>
<bean id="logXML" class="com.eduask.liusheng.aop.xml.Log"></bean> <aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.eduask.liusheng.aop.xml.*.*(..))" id="pointcut"/>
<!-- 配置切面 -->
<aop:aspect id="logAspect" ref="logXML">
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
</aop:aspect>
</aop:config> </beans>
applicationContext-aop-xml.xml
2.ArithmeticCaluda.java
public interface ArithmeticCaluda {
/**
* 加
* @param a
* @param b
* @return 和
*/
double add(double a, double b);
/**
* 减
* @param a
* @param b
* @return 差
*/
double sub(double a, double b);
/**
* 乘
* @param a
* @param b
* @return 积
*/
double mul(double a, double b);
/**
* 除
* @param a
* @param b
* @return 商
*/
double div(double a, double b);
}
ArithmeticCaluda.java
3.ArithmeticCaludaImp.java
public class ArithmeticCaludaImp implements ArithmeticCaluda {
/**
* 加
* @param a
* @param b
* @return 和
*/
public double add(double a,double b){
double result=a+b;
return result;
}
/**
* 减
* @param a
* @param b
* @return 差
*/
public double sub(double a,double b){
double result=a-b;
return result;
}
/**
* 乘
* @param a
* @param b
* @return 积
*/
public double mul(double a,double b){
double result=a*b;
return result;
}
/**
* 除
* @param a
* @param b
* @return 商
*/
public double div(double a,double b){
double result=a/b;
// int i=10/0;
return result;
}
}
ArithmeticCaludaImp.java
4.Log.java
import java.util.Arrays;
import org.aspectj.lang.JoinPoint; public class Log { public void before(JoinPoint jp){
System.out.println("the method "+jp.getSignature().getName()+"() begin with "+Arrays.asList(jp.getArgs()));
} public void after(){
System.out.println("the method after ");
} public void afterReturning(JoinPoint jp,Object result){
System.out.println("the method "+jp.getSignature().getName()+"() end with ["+result+"]");
} public void afterThrowing(Exception e){
System.out.println("afterThrowing"+e.getMessage());
}
}
Log.java
5.Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* aop之xml结合JoinPoint实现动态代理
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext-aop-xml.xml");
ArithmeticCaluda arithmeticCaluda=(ArithmeticCaluda) app.getBean("arithmeticCaludaImpXML");
arithmeticCaluda.div(10, 2);
}
}
Test.java
运行效果与注解的形式一致
需要注意的是:
配置切点<aop:pointcut />要在配置切面<aop:aspect />之前,否则报错;
配置<aop:after />与<aop:after-returning />时,顺序不同,执行结果也不同,顺序在前先执行
xml文件的方式实现动态代理基于SpringAOP的更多相关文章
- Spring中Bean的配置:基于XML文件的方式
Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...
- (一)Mybatis基本配置,Statement方式,动态代理增删改查
首先明白Mybatis是干什么的,之前使用jdbc操作数据库时候要写很多语句,获取光标,连接,获取具体对象进行相应操作,代码过于繁琐,所以现在有了Mybatis,它将这个操作整合在了一起,你不需要关心 ...
- android解析xml文件的方式
android解析xml文件的方式 作者:东子哥 ,发布于2012-11-26,来源:博客园 在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xm ...
- Java解析XML文件的方式
在项目里,我们往往会把一些配置信息放到xml文件里,或者各部门间会通过xml文件来交换业务数据,所以有时候我们会遇到“解析xml文件”的需求.一般来讲,有基于DOM树和SAX的两种解析xml文件的方式 ...
- 做参数可以读取参数 保存参数 用xml文件的方式
做参数可以读取参数 保存参数 用xml文件的方式 好处:供不同用户保存适合自己使用的参数
- Spring中加载ApplicationContext.xml文件的方式
Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...
- cglib动态代理是通过继承父类的方式进行代理的 不是通过接口方式进行动态代理的 因此可以对普通的类进行代理
cglib动态代理是通过继承父类的方式进行代理的 不是通过接口方式进行动态代理的
- XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)
1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
**这个整合.仅仅是最主要的整合,而且是xml配置文件的方式之中的一个,即当中的mybatis是採用非mapper接口的方式.(第二遍採用mapper接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
随机推荐
- hibernate中Query的list和iterator区别
1.Test_query_list类 public class Test_query_iterator_list { public static void main(String[] args) { ...
- Mybatis源码解析-DynamicSqlSource和RawSqlSource的区别
XMLLanguageDriver是ibatis的默认解析sql节点帮助类,其中的方法其会调用生成DynamicSqlSource和RawSqlSource这两个帮助类,本文将对此作下简单的简析 应用 ...
- 深入理解计算机系统chapter2
---恢复内容开始--- 整数表示: 反码和原码都会有正零和负零 有符号整数和无符号整数之间的转换 反之 扩展一个数字的位级表示 截断操作 无符号加法的益处 补码的加法 规格化的值:E=e-bias ...
- NodeJS中的事件
/** * Created by xiwu.xxw on 2015/7/22. */ /** * EventEmitter 的每个事件由一个事件名和若干个参数组成, * 事件名是一个字符串,通常表达一 ...
- D. Mysterious Present (看到的一个神奇的DP,也可以说是dfs)
D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes input standar ...
- PIC24 通过USB在线升级 -- USB HID bootloader
了解bootloader的实现,请加QQ: 1273623966 (验证填bootloader):欢迎咨询或定制bootloader; 我的博客主页www.cnblogs.com/geekygeek ...
- 集群提交spark任务命令
>>spark-submit --class WordCount DataMining.jar /dept_ana/part-00000 /dept_ana/output/wordCou ...
- ASP.NET没有魔法——为什么使用ASP.NET
ASP.NET是什么? ASP.NET是一个使用HTML.CSS.Javascript来构建动态网站或者网站应用程序的Web框架,并且也可以使用它来构建web API和实时通信技术如web soket ...
- JSONP、图片Ping、XMLHttpRequest2.0等跨域资源请求(CORS)
跨域:当协议.主域名.子域名.端口号中任意一个不相同时都不算同一个域,而在不同域之间请求数据即为跨域请求.解决方法有以下几种(如有错误欢迎指出)以请求图片url为例: 1.通过XMLHttpReque ...
- Set 和 Map 数据结构
Set Set 对象允许你存储任何类型的 唯一值, 无论是 原始值(一共6种,string, number, boolean, undefined, null,和 es6 新增的 symbol) 还是 ...