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接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
随机推荐
- python实例编写(6)--引入unittest测试框架,构造测试集批量测试(以微信统一管理平台为例)
---恢复内容开始--- 一.python单元测试实例介绍 unittest框架又叫PyUnit框架,是python的单元测试框架. 先介绍一个普通的单元测试(不用unittest框架)的实例: 首先 ...
- HTML5可预览多图片ajax上传(使用formData传递数据)
HTML5可预览多图片ajax上传(使用formData传递数据) 在介绍上传图片之前,我们简单的来了解下FormData的基本使用:介绍完成后这些基本知识后,我们会在文章最后提供一个demo,就是a ...
- SAP Gateway简介
SAP Gateway在S4/HANA时代的ABAP开发模型中有着重要的地位.SAP Gateway是什么?它对ABAP开发有怎样的影响?可以为我们提供哪些方便?这篇译文将浅要地讨论这些话题. SAP ...
- ThinkPHP中:RBAC权限控制的实习步骤
使用版本ThinkPHP3.1.3 第一步,建表及数据 第二步,建关联模型 第三步,控制器使用关联模型.配置文件 第四步,模板显示数据 第一步,建表及数据 在数据库中,建立一个companysvn数据 ...
- Navicat for MySQL:快捷键整理
使用快捷键,提升工作效率! ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+r 只运行选中 ...
- 让MessageBox对话框总在最前面
调用MessageBox的时候,如果最后一个参数用上MB_SYSTEMMODAL的话,可以让对话框在最前面
- 基于maven创建和部署Webx项目
1.准备工作 下载 Webx Maven 项目的目录结构Artifact插件. archetype-webx-quickstart-1.0.tar.gz插件:http://central.maven. ...
- Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- python中strip函数的用法
python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...
- PBOC圈存时用到3DES加密解密以及MAC计算方法
最近在做PBOC圈存时用到了3DES的加密解密以及MAC计算问题,在网上了解一些知识,复制了一些demo学习,我这里没有深入研究,只是把我用到的和了解的做个总结,便于以后使用和学习. 3DES分双倍长 ...