Spring AOP (下)
4、方式二:schema配置
a、业务类:

/**
* 业务类
*
* @author yanbin
*
*/
public class AspectBusiness { /**
* 切入点
*/
public String delete(String obj) {
System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n");
return obj + ":瞄~";
} public String add(String obj) {
System.out.println("================这个方法不能被切。。。============== \n");
return obj + ":瞄~ 嘿嘿!";
} public String modify(String obj) {
System.out.println("=================这个也设置加入切吧====================\n");
return obj + ":瞄改瞄啊!";
} }

b、切面类:切面类中,包含了所有的通知

/**
* 定义一个切面
*
* @author yanbin
*
*/
public class AspectAdvice { /**
* 前置通知
*
* @param jp
*/
public void doBefore(JoinPoint jp) {
System.out.println("===========进入before advice============ \n"); System.out.print("准备在" + jp.getTarget().getClass() + "对象上用");
System.out.print(jp.getSignature().getName() + "方法进行对 '");
System.out.print(jp.getArgs()[0] + "'进行删除!\n\n"); System.out.println("要进入切入点方法了 \n");
} /**
* 后置通知
*
* @param jp
* 连接点
* @param result
* 返回值
*/
public void doAfter(JoinPoint jp, String result) {
System.out.println("==========进入after advice=========== \n");
System.out.println("切入点方法执行完了 \n"); System.out.print(jp.getArgs()[0] + "在");
System.out.print(jp.getTarget().getClass() + "对象上被");
System.out.print(jp.getSignature().getName() + "方法删除了");
System.out.print("只留下:" + result + "\n\n");
} /**
* 环绕通知
*
* @param pjp
* 连接点
*/
public void doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===========进入around环绕方法!=========== \n"); // 调用目标方法之前执行的动作
System.out.println("调用方法之前: 执行!\n"); // 调用方法的参数
Object[] args = pjp.getArgs();
// 调用的方法名
String method = pjp.getSignature().getName();
// 获取目标对象
Object target = pjp.getTarget();
// 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
Object result = pjp.proceed(); System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + result + "\n");
System.out.println("调用方法结束:之后执行!\n");
} /**
* 异常通知
*
* @param jp
* @param e
*/
public void doThrow(JoinPoint jp, Throwable e) {
System.out.println("删除出错啦");
} }

c、配置文件:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName"> <!-- ==============================利用spring 利用aspectj来配置AOP================================ --> <!-- 声明一个业务类 -->
<bean id="aspectBusiness" class="aop.schema.AspectBusiness" /> <!-- 声明通知类 -->
<bean id="aspectAdvice" class="aop.schema.advice.AspectAdvice" /> <aop:config>
<aop:aspect id="businessAspect" ref="aspectAdvice">
<!-- 配置指定切入的对象 -->
<aop:pointcut id="point_cut" expression="execution(* aop.schema.*.*(..))" />
<!-- 只匹配add方法作为切入点
<aop:pointcut id="except_add" expression="execution(* aop.schema.*.add(..))" />
--> <!-- 前置通知 -->
<aop:before method="doBefore" pointcut-ref="point_cut" />
<!-- 后置通知 returning指定返回参数 -->
<aop:after-returning method="doAfter"
pointcut-ref="point_cut" returning="result" />
<aop:around method="doAround" pointcut-ref="point_cut"/>
<aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/>
</aop:aspect>
</aop:config>
</beans>

d、测试类:

public class Debug {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop/schema_aop.xml");
AspectBusiness business = (AspectBusiness) context.getBean("aspectBusiness");
business.delete("猫");
}
}

5、方式三:aspectj注解
注解在项目中已经到处都是了,撇开一些优劣不提,开发的便利性和可读性是非常的方便的。用来配置Spring AOP也非常简单便利
a、业务类:

/**
* 业务类
*
* @author yanbin
*
*/
@Component
public class Business { /**
* 切入点
*/
public String delete(String obj) {
System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n");
return obj + ":瞄~";
} public String add(String obj) {
System.out.println("================这个方法不能被切。。。============== \n");
return obj + ":瞄~ 嘿嘿!";
} public String modify(String obj) {
System.out.println("=================这个也设置加入切吧====================\n");
return obj + ":瞄改瞄啊!";
} }

b、切面类:

/**
* 定义切面
*
* @Aspect : 标记为切面类
* @Pointcut : 指定匹配切点
* @Before : 指定前置通知,value中指定切入点匹配
* @AfterReturning :后置通知,具有可以指定返回值
* @AfterThrowing :异常通知
*
* @author yanbin
*
*/
@Component
@Aspect
public class AspectAdvice { /**
* 指定切入点匹配表达式,注意它是以方法的形式进行声明的。
*/
@Pointcut("execution(* aop.annotation.*.*(..))")
public void anyMethod() {
} /**
* 前置通知
*
* @param jp
*/
@Before(value = "execution(* aop.annotation.*.*(..))")
public void doBefore(JoinPoint jp) {
System.out.println("===========进入before advice============ \n"); System.out.print("准备在" + jp.getTarget().getClass() + "对象上用");
System.out.print(jp.getSignature().getName() + "方法进行对 '");
System.out.print(jp.getArgs()[0] + "'进行删除!\n\n"); System.out.println("要进入切入点方法了 \n");
} /**
* 后置通知
*
* @param jp
* 连接点
* @param result
* 返回值
*/
@AfterReturning(value = "anyMethod()", returning = "result")
public void doAfter(JoinPoint jp, String result) {
System.out.println("==========进入after advice=========== \n");
System.out.println("切入点方法执行完了 \n"); System.out.print(jp.getArgs()[0] + "在");
System.out.print(jp.getTarget().getClass() + "对象上被");
System.out.print(jp.getSignature().getName() + "方法删除了");
System.out.print("只留下:" + result + "\n\n");
} /**
* 环绕通知
*
* @param pjp
* 连接点
*/
@Around(value = "execution(* aop.annotation.*.*(..))")
public void doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===========进入around环绕方法!=========== \n"); // 调用目标方法之前执行的动作
System.out.println("调用方法之前: 执行!\n"); // 调用方法的参数
Object[] args = pjp.getArgs();
// 调用的方法名
String method = pjp.getSignature().getName();
// 获取目标对象
Object target = pjp.getTarget();
// 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
Object result = pjp.proceed(); System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + result + "\n");
System.out.println("调用方法结束:之后执行!\n");
} /**
* 异常通知
*
* @param jp
* @param e
*/
@AfterThrowing(value = "execution(* aop.annotation.*.*(..))", throwing = "e")
public void doThrow(JoinPoint jp, Throwable e) {
System.out.println("删除出错啦");
} }

c、配置:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName"> <context:component-scan base-package="aop.annotation" />
<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy /> </beans>

d、测试类:

/**
* 测试类
*
* @author yanbin
*
*/
public class Debug { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("aop/annotation_aop.xml");
Business business = (Business) context.getBean("business");
business.delete("猫");
} }

原文出处:http://www.cnblogs.com/yanbincn/archive/2012/08/13/2636961.html
Spring AOP (下)的更多相关文章
- 转载:Spring AOP (下)
昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...
- (转)spring aop(下)
昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...
- Spring AOP实现方式四之注入式AspectJ切面【附源码】
现在我们要讲的是第四种AOP实现之注入式AspectJ切面 通过简单的配置就可以实现AOP了. 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.ao ...
- Spring AOP实现方式三之自动扫描注入【附源码】
注解AOP实现 这里唯一不同的就是application 里面 不需要配置每个bean都需要配置了,直接自动扫描 注册,主要知识点是怎么通过配置文件得到bean, 注意类前面的@注解. 源码结构: ...
- Spring AOP实现方式三【附源码】
注解AOP实现 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.aop; /** * 谈恋爱接口 * * @author Administrator ...
- 曹工说Spring Boot源码(18)-- Spring AOP源码分析三部曲,终于快讲完了 (aop:config完整解析【下】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 关于Jersey框架下的Aop日志 和Spring 框架下的Aop日志
摘要 最近新接手的项目经常要查问题,但是,前面一拨人,日志打的非常乱,好多就根本没有打日志,所以弄一个AOP统一打印一下 请求数据和响应数据 框架 spring+springmvc+jersey 正文 ...
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
随机推荐
- <未测>源码升级安装glibc和rpm升级glibc
源码升级安装glibc和rpm升级glibc http://jacklin9.spaces.live.com/blog/cns!A891B52E1182AFB2!346.entry http://bl ...
- POJ 3436 ACM Computer Factory
题意: 为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产线来生产这样的电脑,随着比赛规模的越来越大,生产线的生产能力不能满足需要,所以说ACM董事会想 ...
- FFT(快速傅里叶变换):HDU 5307 He is Flying
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8IAAAPeCAIAAABInTQaAAAgAElEQVR4nOy9fZReVXk3vP8ia+HqCy
- 【模拟】【数学】CSU 1803 2016 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1803 题目大意: 给定n,m(n,m<=109)1<=i<=n,1& ...
- 将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
1. 我们有一个test.exe的WinForm程序,这是我们要加壳的目标程序. 2. 新建一个WinForm工程,删除Form1,然后新建一个类.如下. 3. 将test.exe 拷贝到该工程目录, ...
- 重载(Overloading)以及模板(Template)
继续<C++ premier plus>的学习 (1)函数重载,通俗来说,就是相同的函数名字名下,存在多个函数,要使得这成立,各个同名函数必须形参列表(也称为"签名", ...
- [Locked] Paint Fence
Paint Fence There is a fence with n posts, each post can be painted with one of the k colors. You ha ...
- [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II
Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the ...
- POI使用详解
Apache POI使用详解 1.POI结构与常用类 (1)POI介绍 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案 ...
- ArrayList、LinkedList、HashMap底层实现
ArrayList 底层的实现就是一个数组(固定大小),当数组长度不够用的时候就会重新开辟一个新的数组,然后将原来的数据拷贝到新的数组内. LinkedList 底层是一个链表,是由java实现的一个 ...