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接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
随机推荐
- MongoDB的备份和部署 高级功能索引,聚合复制,分片
创建备份 MongoDB 数据转储 为了在 MongoDB 中创建数据库备份,需要使用 mongodump 命令.该命令会将服务器上的所有数据都转储到 dump 目录中.你可以使用很多选项来限制转储的 ...
- nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed
在用AOP 的时候出现了如下的错误, 警告: Exception encountered during context initialization - cancelling refresh atte ...
- datepickerpopup时间限制选取
使用popup组件的过程中遇到时间选取的问题 官方文档大致说使用date和mode 可以解决,奈何老夫是看不懂,写的时候参考的有 官方文档.echo2016的博文.liumang361的博文 先看图 ...
- mybatis(三)
上面2章写了mybatis的基本操作,今天就写写mybatis的动态代理吧. 动态代理有4个基本原则: 1.userMapper.xml里面的namespace="cn.my.dao.Use ...
- 阿里云服务器解决mysql远程连接失败问题
嗯,自己买了个阿里云的学生机服务器,奈何装了mysql以后一直不能连接,也是够笨的. 记录一下自己遇到的问题. 当然了,首先需要在阿里云安全组开放3306端口,第一次玩儿云服务器差点把我搞坏了.... ...
- Day4 闭包、装饰器decorator、迭代器与生成器、面向过程编程、三元表达式、列表解析与生成器表达式、序列化与反序列化
一.装饰器 一.装饰器的知识储备 1.可变长参数 :*args和**kwargs def index(name,age): print(name,age) def wrapper(*args,**k ...
- Python s12 Day2 笔记及作业
1. 元组的元素不可修改,但元组的元素的元素可以被修改. 2. name="eric" print(name.center(20, "*") 3. list=[ ...
- java数据库编程之事务、视图、索引、备份、恢复
第五章:事务.视图.索引.备份和恢复 5.1:事务 事务的概念:事务(transcation)是讲一系列数据操作捆绑成为一个整体进行统计管理. 如果某一事务执行成功了,则该事务进行操作的所有数据将会提 ...
- python的urlparse
urlparse主要是URL的分解和拼接,分析出URL中的各项参数,可以被其他的URL使用. 主要的函数有: 1.urlparse 将URL分解为6个片段,返回一个元组,包括协议.基地址.相对地址等等 ...
- c# Linq操作XML,查找节点数据
/*查找XML*/ var filePath = Server.MapPath("~/xml/sample.xml"); XDocument doc = XDocument.Loa ...