Spring学习(22)--- AOP之Advice应用(下)
(六)Advice parameters(advice带参数的情况)
例子:
修改MyAspect(添加around_init方法):
package com.aop.schema; import org.aspectj.lang.ProceedingJoinPoint; /**
*
* 切面类
*
*/
public class MyAspect { public void before(){
System.out.println("MyAspect.before");
} public void afterreturning(){
System.out.println("MyAspect.afterreturning");
} public void afterthrowing(){
System.out.println("MyAspect.afterthrowing");
} public void after(){
System.out.println("MyAspect.after");
} public void around(ProceedingJoinPoint pjp) {
try {
System.out.println("MyAspect.around_1");
Object obj=pjp.proceed();
System.out.println("MyAspect.around_2");
} catch (Throwable e) {
e.printStackTrace();
}
} public void around_init(ProceedingJoinPoint pjp,String name,int age) {
System.out.println(name+" "+age);
try {
System.out.println("MyAspect.around_1");
Object obj=pjp.proceed();
System.out.println("MyAspect.around_2");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
修改ApsectBiz类(添加init方法):
package com.aop.schema;
/**
*
* 业务类
*
*/
public class ApsectBiz { public void biz(){
System.out.println("ApsectBiz.biz");
//throw new RuntimeException(); //故意抛出异常
} public void init(String name,int age){
System.out.println("ApsectBiz.init : "+ name +" " +age);
}
}
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: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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <bean id="myAspect" class="com.aop.schema.MyAspect"></bean> <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean> <aop:config>
<aop:aspect id="myAspectAOP" ref="myAspect">
<!-- 先注释掉,便于观察结果
<aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" />
<aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after-returning method="afterreturning" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterthrowing" pointcut-ref="myPointcut"/>
<aop:after method="after" pointcut-ref="myPointcut"/>
<aop:around method="around" pointcut-ref="myPointcut"/>
--> <aop:around method="around_init" pointcut="execution(* com.aop.schema.ApsectBiz.init(String,int)) and args(name,age)"/>
</aop:aspect>
</aop:config> </beans>
单元测试:
package com.aop.schema; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UnitTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");
ApsectBiz biz = (ApsectBiz)context.getBean("apsectBiz");
biz.init("Json",25);
}
}
结果:
七月 09, 2015 11:48:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@118e0f0f: startup date [Thu Jul 09 23:48:42 CST 2015]; root of context hierarchy
七月 09, 2015 11:48:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
Json 25
MyAspect.around_1
ApsectBiz.init : Json 25
MyAspect.around_2
Spring学习(22)--- AOP之Advice应用(下)的更多相关文章
- Spring学习之AOP的实现方式
Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- Spring学习之Aop的基本概念
转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
- Spring学习之AOP与事务
一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...
- spring学习笔记-AOP
1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用: 提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...
- Spring 学习二-----AOP的原理与简单实践
一.Spring AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
- Spring学习之AOP
Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...
- Spring 学习之AOP
1. 走进面前切面编程 编程范式: 面向过程编程,c语言: 面向对象编程:c++,java,c#; 函数式编程: 事件驱动编程: 面向切面编程: AOP是一种编程范式,不是编程语言:解决特定问题,不能 ...
随机推荐
- Hibernate学习--hibernate延迟加载原理(动态代理)
在正式说hibernate延迟加载时,先说说一个比较奇怪的现象吧:hibernate中,在many-to-one时,如果我们设置了延迟加载,会发现我们在eclipse的调试框中查看one对应对象时,它 ...
- Javascript中call和apply
在上一章http://www.cnblogs.com/meiyh/p/6207671.html 我有提到javascript很重要的this关键字,现在我们结合这个关键字使用的下面的两个关键字: ca ...
- js表白心形特效
好久没有仔细钻研技术了,闲下来借鉴一下做出一些效果 友情链接: http://tiepeng.applinzi.com/love_you/ ;;background:#ffe;font-size:12 ...
- tomcat的常用配置
1.解決get请求的中文乱码问题 解决办法: 首先找到tomcat路径下的apache-tomcat-7.0.52\conf文件夹,打开server.xml文件,编辑如下内容: <Connect ...
- ETL开发面试问题加吐槽加职业发展建议
写在前面: 作为甲方,对于乙方派来的开发人员,我是会自己面一下.总体来说遇到的水平不一,于是经过这三年多的面(cui)试(can),总结了一套自己的面试套路,中间也遇到过很多想吐槽的东西,于是大概记录 ...
- java并发程序——Excutor
概述 Excutor这个接口用的不多,但是ThreadPoolExcutor这个就用的比较多了,ThreadPoolExcutor是Excutor的一个实现.Excutor体系难点没有,大部分的关键点 ...
- base 镜像 - 每天5分钟玩转容器技术(10)
上一节我们介绍了最小的 Docker 镜像,本节讨论 base 镜像. base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 所以,能称作 base ...
- mysql for windows(服务器)上的配置安装--实例
mysql for windows(服务器)上的配置安装 **** 下载 官网网址:https://www.mysql.com/downloads/ 选择左上角Community 再选择MySQL C ...
- Linux目录与文件的权限
零.Linux中的权限为什么重要? 权限直接关系数据安全! 一.用户基础概念: 所有者(owner):拥有这个文件的用户.一般拥有目录或文件的所有权限. 用户组(group):几个用户组成一个用户组, ...
- JS中this到底指向谁?
关于this的指向,是一个令人很头疼的问题.但是,你运气好,碰到了我.老夫这儿有本祖传秘籍,看懂这个,妈妈再也不用担心你的this指向不对啦! 归根结底,this指向就一句话:谁最终调用函数,this ...