参考: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. Apache不能启动解决办法

    这是我这两天频繁遇到的问题.Apache服务器还真是问题少年!任何点改动都可能导致它无法使用. 原因一:80端口占用例如IIS,另外就是迅雷.我的apache服务器就是被迅雷害得无法启用! 原因二:软 ...

  2. 给ThinkPHP5增加验证码功能

    就在这几天,TP5进行的RC3的大规模更新,虽然我们都狠狠地骂了一百遍,但是我的内心是无比的激动,TP终于走上了"上流社会"的模式:composer! 为什么说composer是上 ...

  3. 如何判断js是否加载完全

    var script=document.createElement('script'); if(script.onreadystatechange){ script.onreadystatechang ...

  4. 在Android上实现SSL握手(客户端需要密钥和证书),实现服务器和客户端之间Socket交互

    Android的私钥和信任证书的格式必须是BKS格式的,通过配置本地JDK,让keytool可以生成BKS格式的私钥和信任证书,java本身没有BouncyCastle密库 服务端: Java代码  ...

  5. tempnam问题

    tempnam()函数创建一个具有唯一文件名的临时文件 若成功,则返回新的临时文件名,若失败,则返回false 失败原因 c:\windows\temp文件夹不具备读写权限(即 不是超级管理员)

  6. cronolog 对 tomcat 7 进行日志切割

    一.安装 软件 cronolog-1.6.2.tar.gz tar zxvf cronolog-1.6.2.tar.gz cd cronolog-1.6.2 ./configure && ...

  7. ListView与RadioButton组合——自定义单选列表

      标签: radiobuttonlistviewandroidlayout 2013-09-10 11:13 19396人阅读 评论(8) 收藏 举报  分类: Android(19)  版权声明: ...

  8. PHP访问接口获取数据

    如:http://localhost/operate.php?act=get_user_list&type=json 在这里operate.php相当于一个接口,其中get_user_list ...

  9. Linux下简单的取点阵字模程序

    源:Linux下简单的取点阵字模程序 Linux操作系统下进行简单的图形开发,经常会用到取字模的软件,但是Linux并没有像Windows下的小工具可用,我们也并不希望为了取字模而频繁地切换操作系统. ...

  10. javascript DOM 学习总结 (1)

    摘自javascript DOM编程艺术 1.首先介绍DOM的三个字母的含义: 1.1  D 如果没有document(文档),DOM 也无从谈起,当创建了一个网页并把他加载到web浏览器中时,DOM ...