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接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
随机推荐
- 初触hibernate01--MYSQL建表时type=InnoDB和Engine=InnoDB注意点
第一次运行程序时,将Hibernte中的hibernate.hbm2ddl.auto设置成create(或者是update),让Hibernate帮助自动建表,但不成功,报了如下信息: You hav ...
- 将数组分割为几个等长度的子数组(使用slice)
先了解一下slice方法: slice() 1.定义:slice()可从已有数组中截取返回指定的元素,形成一个新的数组: 语法:arrayObject.slice(start,end): 参数 描述 ...
- input type="hidden" js获取不到值(document.getelementbyid OR $(#).val())
<head> <input type="hidden" name="aplStatus" id="aplStatus" v ...
- SQL性能优化十条经验(转)
1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'-- 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便会 ...
- 为ASP.NetCore程序启用SSL
紧接着上一篇搭建连接MySql的三层架构的ASP.NetCore2.0的WebApi的案例,这篇来实现为ASP.NetCore启用SSL支持 由于ASP.NetCore默认服务器Kestrel不像ii ...
- 理解AngularJS中的依赖注入
点击查看AngularJS系列目录 理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS ...
- PS 软件操作应用处理——粒子化任务效果
前 言 JRedu 上次分享中,给大家介绍了一些图片的处理方法,主要是通过滤镜里的功能,把图片处理成素描效果或者水彩画效果,营造出不同的氛围. PS是一款非常强大的软件,包含了非常多的功能,合成 ...
- 简单Elixir游戏服务器-安装Elixir
用WebInstaller 安装半天也没下载成功文件. 改成直接下载erlang 和 elixir 预编译包了. 安装很简单,最后设置好环境变量. cmd 执行 elixir -v 最后顺便下载了个g ...
- 一个强迫症的Git 选择
选择 1,经常性的commit or 干净的历史 在本地(私有)的开发分支中,选择经常性的commit,以便于实时记录修改,回退等操作.eg.develop,feature... 实现方式: comm ...
- Sublime Text保存文件时自动去掉行末空格
修改一个Sublime Text的用户配置,其中这个配置就是"保存文件时自动去掉每行结束后多余的空格",具体操作如下: 在Sublime Text菜单栏中找到preferences ...