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接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
随机推荐
- [01] Java语言的基本认识
0.写在前面的话 我们都知道在计算机的底层,它是识别二进制的,也就是说,计算机只能认识0和1.这主要是因为电路的逻辑只有两种状态,所以只需要0和1两个数字就可以表示低电平和高电平.而计算机是由数不清的 ...
- 解决Maven管理的项目下"Missing artifact xxx bundle"问题
例如使用maven编译使用了mina的包的工程,出现如下提示: [INFO] Scanning for projects... [INFO] ...
- Dstl Satellite Imagery Feature Detection-Data Processing Tutorial
如何读取WKT格式文件 我们找到了这些有用的包: Python - shapely.loads() R - rgeos 如何读取geojson格式文件 我们找到了这些有用的包: Python - j ...
- SpringMVC基础入门,创建一个HelloWorld程序
ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...
- AngularJS - 依赖注入(Dependency Injection)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 依赖注入 依赖注入是软件设计模式中的一部分,用于处理组件是如何得到它说依赖的其它组件的. ...
- Ubuntu16.04 Using Note
I meet lots of problems when i installed and use ubuntu 16.04.below is my using note: (my operating ...
- The Nerd Factor SPOJ - MYQ5
The Nerd Factor Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Submi ...
- PHP CodeBase: 生成N个不重复的随机数
有25幅作品拿去投票,一次投票需要选16幅,单个作品一次投票只能选择一次.前面有个程序员捅了漏子,忘了把投票入库,有200个用户产生的投票序列为空.那么你会如何填补这个漏子? <?php /* ...
- dets
模块说明 提供基于文件的项式存储,项式以元组表示,其中某个位置为键,默认第1位置 Dets为Mniesia所用,后者增加了事务.查询.和分布式支持. Dets文件不能超过2GB. Dets只有set ...
- DAO与DTO
DAO叫数据访问对象(data access object) DTO是数据传输对象(data transfer object) DAO通常是将非对象数据(如关系数据库中的数据)以对象的方式操纵.(即一 ...