参考:http://blog.csdn.net/cdl2008sky/article/details/6268628

参考:http://www.360doc.com/content/12/0602/15/7656232_215420487.shtml 中的proxy-target-class

Spring @Aspect实现切面编程:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 这个声明会创建AnnotationAwareAspectJAutoProxyCreator,进行切面Bean的代理 -->
<aop:aspectj-autoproxy />
<!-- 必须将切面类声明为一个Bean -->
<bean id="audience" class="com.stono.sprtest3.Audience"></bean>
<bean id="singer" class="com.stono.sprtest3.Singer"></bean>
</beans>

AppBean:

package com.stono.sprtest3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans11 { @SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans11.xml");
Performer singer = (Performer) context.getBean("singer");
singer.perform();
} }

切面类:

package com.stono.sprtest3;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Audience {
// 为一个类进行切点的声明也是可以的;
// @Pointcut("execution(* com.stono.sprtest3.Singer.perform()(..))")
@Pointcut("execution(* com.stono.sprtest3.Performer.perform(..))")
public void pointcut() {// 方法名称就是一个标记
}
@Before("pointcut()")
public void takeSeat() {
System.out.println("com.stono.sprtest2.Audience.takeSeat()");
}
@Before("pointcut()")
public void turnOffPhone() {
System.out.println("com.stono.sprtest2.Audience.turnOffPhone()");
}
@AfterReturning("pointcut()")
public void applaud() {
System.out.println("com.stono.sprtest2.Audience.applaud()");
}
@AfterThrowing("pointcut()")
public void refund() {
System.out.println("com.stono.sprtest2.Audience.refund()");
}
@Around("pointcut()")
public void watchPerformance(ProceedingJoinPoint joinPoint) {
try {
System.out.println("The audience is taking their seats.");
System.out.println("The audience is turning off their cellphones.");
long start = System.currentTimeMillis();
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
System.out.println("The performance took " + (end - start) + " milliseconds.");
} catch (Throwable e) {
e.printStackTrace();
System.out.println("Boo! We want our money back!");
}
}
}

接口和类:

package com.stono.sprtest3;
public interface Performer {
void perform();
}
package com.stono.sprtest3;
public class Singer implements Performer {
public void perform() {
System.out.println("com.stono.sprtest3.Singer.perform()");
}
}

Spring @Aspect实现切面编程的更多相关文章

  1. Spring(4)——面向切面编程(AOP模块)

    Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...

  2. Spring IOP 面向切面编程

    Spring IOP  面向切面编程 AOP操作术语 Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.(类里面 ...

  3. 利用例子来理解spring的面向切面编程

    最近学习了spring的面向切面编程,在网上看到猴子偷桃的例子,觉得这种方式学习比书本上讲解有趣多了,也便于理解.现在就来基于猴子偷桃写个基本的例子. maven工程:

  4. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  5. 利用例子来理解spring的面向切面编程(使用@Aspect)

    上篇的例子,自动装配和自动检测Bean是使用注解的方式处理的,而面向切面编程是使用aop标签处理的,给我感觉就像中西医参合一样. 现在就来优化优化,全部使用注解的方式处理. 1.工程图:

  6. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  7. Spring AOP面向切面编程的实现

    1.涉及到的几个概念 切面类.被切对象.切入点.切入时间.切入内容:(自己命的名,好理解点) 2.看配置文件 <?xml version="1.0" encoding=&qu ...

  8. Spring基础篇——Spring的AOP切面编程

    一  基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...

  9. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

随机推荐

  1. 关于js的几道经典题(作用域、原型链等)自己做的

    1. function test() { var a = 1; setTimeout(function() { alert(a); a = 3; }, 1000); a = 2; setTimeout ...

  2. ural1650 Billionaires

    Billionaires Time limit: 3.0 secondMemory limit: 64 MB You probably are aware that Moscow holds the ...

  3. treeview 与tabControl组合使用

    1.左边一个treeview,右边一个tabcontrol: 2.调整控件让tree在tabcontrol上,并让treeview压住tab页 3.将tab页的name设置成treeview的node ...

  4. JavaScript 逗号表达式

    逗号表达式的一般形式是:表达式1,表达式2,表达式3……表达式n  逗号表达式的求解过程是:先计算表达式1的值,再计算表达式2的值,……一直计算到表达式n的值.最后整个逗号表达式的值是表达式n的值.  ...

  5. Firefox 插件 JSview是一套比较实用的JS,CSS文件查看工具,很方便,很快捷地查看页面引用了哪些文件,作为Web前端开发者是一套必备的插件,由于Firefox升级过快,插件很快不兼容了,这里对插件做了一些调整,可以兼容最新Firefox浏览器(目前FireFox 21)

    JSView Firefox Plugins Download  点击下载

  6. EF 报【序列包含一个以上的元素】解决办法

    1.检查模型是否存在重复的字段,eg: public class AggregateRoot { public System.Guid Guid { get; set; } } public part ...

  7. nginx实战

    原文:http://www.cnblogs.com/yucongblog/p/6289628.html nginx实战   (一) nginx环境的搭建安装流程: 1 通过ftp将nginx-1.11 ...

  8. HTML学习(三)文本格式化

    HTML文本格式化HTML 可定义很多供格式化输出的元素,比如粗体和斜体字.例1:此例演示如何在一个 HTML 文件中对文本进行格式化<html> <body> <b&g ...

  9. 51nod贪心算法教程

    51nod确实是一个好oj,题目质量不错,wa了还放数据,学习算法来说挺好的,这次我做了几个水的贪心,虽然水,但是确实都很典型. 教程链接:http://www.51nod.com/tutorial/ ...

  10. 火狐上的一个post提交工具(主要用于测试接口时候)

    添加的过程 安装完后,就可以在下图上,看到一个poster 点击poster就可以看到下图 图中红线圈好的,是必须要填写的 Url是访问路径 Name是参数名称 Value是参数值 需要注意一点的是: ...