我们经常会用到的有如下几种
1、基于代理的AOP
2、纯简单Java对象切面
3、@Aspect注解形式的
4、注入形式的Aspcet切面

一、需要的java文件

public class ChenLliNa implements Sleepable {  

    @Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("乖,该睡觉了!s");
} public void sleep2() {
// TODO Auto-generated method stub
System.out.println("乖,该睡觉了!2222");
}
}
public interface Sleepable {  

    /**
* 睡觉方法
* @author demo
* @version 2015年5月31日上午9:17:14
*/
void sleep();
}
import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice; public class SleepHelper implements MethodBeforeAdvice, AfterReturningAdvice { @Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("睡觉前要敷面膜");
} @Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("睡觉后要做美梦");
} }
public class SleepHelper02 {
public void beforeSleep(){
System.out.println("睡觉前要敷面膜");
}
public void afterSleep(){
System.out.println("睡觉后要做美梦");
}
}
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class SleepHelper03 { @Pointcut("execution(* *.sleep2(..))")
public void sleep2point(){} @Before("sleep2point()")
public void beforeSleep(){
System.out.println("睡觉前要敷面膜dddd");
} @AfterReturning("sleep2point()")
public void afterSleep(){
System.out.println("睡觉后要做美梦dd");
}
}

二、application.xml

<!--  applicationContext01.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 创建一个增强 advice -->
<bean id="sleepHelper" class="com.ideal.spdb.common.demo.SleepHelper" /> <bean id="lina" class="com.ideal.spdb.common.demo.ChenLliNa" />
<!-- 定义切点 匹配所有的sleep方法 -->
<bean id="sleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sleep"></property>
</bean> <!-- 切面 增强+切点结合 -->
<bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="sleepHelper" />
<property name="pointcut" ref="sleepPointcut" />
</bean> <!-- 定义代理对象 -->
<bean id="linaProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="lina" />
<property name="interceptorNames" value="sleepHelperAdvisor" />
<!-- <property name="proxyInterfaces" value="com.tgb.springaop.service.Sleepable"/> -->
</bean> </beans>
<!-- applicationContext02.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 创建一个增强 advice -->
<bean id="sleepHelper" class="com.ideal.spdb.common.demo.SleepHelper" />
<!-- 目标类 -->
<bean id="lina" class="com.ideal.spdb.common.demo.ChenLliNa" /> <!-- 配置切点和通知 -->
<bean id="sleepAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="sleepHelper"></property>
<property name="pattern" value=".*sleep" />
</bean> <!-- 自动代理配置 -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>
<!-- applicationContext03.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-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"> <!--扫描包 -->
<context:component-scan base-package="com.ideal.spdb.*.*" annotation-config="true"/>
<!-- ASPECTJ注解 -->
<aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 目标类 -->
<bean id="lina" class="com.ideal.spdb.common.demo.ChenLliNa"/> </beans>
<!-- applicationContext04.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 目标类 -->
<bean id="lina" class="com.ideal.spdb.common.demo.ChenLliNa"/>
<bean id ="sleepHelper" class="com.ideal.spdb.common.demo.SleepHelper02"/> <aop:config>
<aop:aspect ref="sleepHelper">
<aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
<aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
</aop:aspect>
</aop:config>
</beans>

三、测试

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ideal.spdb.common.demo.ChenLliNa;
import com.ideal.spdb.common.demo.Sleepable; public final class Boot { @Test
public void test0() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
FooService foo = (FooService) ctx.getBean("fooService");
foo.getFoo("Pengo", 12);
foo.setFoo("d", 1); } // 通过代理
@Test
public void test1() {
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext01.xml"); Sleepable sleeper = (Sleepable) ct.getBean("linaProxy"); sleeper.sleep(); }
// 简答的java对象
@Test
public void test2() {
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext02.xml"); Sleepable sleeper = (Sleepable) ct.getBean("lina"); sleeper.sleep(); }
// 通过aspect注解
@Test
public void test3() {
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext03.xml"); ChenLliNa sleeper = (ChenLliNa) ct.getBean("lina"); sleeper.sleep2();
}
// 通过apsect配置文件
@Test
public void test4() {
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext04.xml");
Sleepable sleeper = (Sleepable) ct.getBean("lina"); sleeper.sleep();
}
}

四、环绕模式

public class DefaultFooService implements FooService {

    public Foo getFoo(String name, int age) {
try {
new Thread().sleep(1000);
} catch (Exception e) { }
return new Foo(name, age);
} @Override
public Foo setFoo(String fooName, int age) {
return null;
}
}
public class Foo {
/**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the age
*/
public int getAge() {
return age;
} /**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
} private String name;
private int age; public Foo(String name, int age) {
super();
this.name = name;
this.age = age;
} }
public interface FooService {
Foo getFoo(String fooName, int age);
Foo setFoo(String fooName, int age);
}
import java.text.SimpleDateFormat;
import java.util.Date; import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;
public class SimpleProfiler { public void monit(ProceedingJoinPoint call) throws Throwable {
StopWatch clock = new StopWatch();
try {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
clock.start(call.toShortString());
call.proceed();
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
} finally {
clock.stop();
System.out.println(clock.getLastTaskName());
System.out.println(clock.getTotalTimeSeconds());
}
}
}
<!-- applicationContext.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="fooService" class="com.ideal.spdb.common.demo.DefaultFooService" /> <bean id="profiler" class="com.ideal.spdb.common.demo.SimpleProfiler" /> <aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="theExecutionOfSomeFooServiceMethod"
expression="execution(* com.ideal.spdb.*.*.*.*(..))" />
<aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"
method="monit" />
</aop:aspect>
</aop:config> </beans>

测试在上面的

Boot -> method = test0
点我下载

spring AOP 的几种实现方式(能测试)的更多相关文章

  1. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  2. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  3. spring ----> aop的两种实现方式

    实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...

  4. JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解

    在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...

  5. spring AOP的两种代理

    本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理  2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...

  6. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  7. spring Bean的三种配置方式

    Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...

  8. Spring Boot 项目几种启动方式

    Spring Boot 项目几种启动方式 1. 使用 main 启动 jar xxxx.jar 2. 使用 mvn 启动 mvn spring-boot:run 3. 使用 Spring Boot c ...

  9. Spring AOP源码分析(二):AOP的三种配置方式与内部解析实现

    AOP配置 在应用代码中,可以通过在spring的XML配置文件applicationContext.xml或者基于注解方式来配置AOP.AOP配置的核心元素为:pointcut,advisor,as ...

随机推荐

  1. 第31天 mvp

    interactor调用接口 Activity包含Presenter,这样界面上的操作就会通知到Presenter. Presenter调用view接口, Activity实现view接口,这样Pre ...

  2. Javascript高级程序设计——面向对象之理解对象

    在面向对象语言中都有类的概念,通过类来创建具有属性和方法的对象.而ECMAScript中没有类的概念,ECMAScript中定义了对象:无需属性的集合,其属性值可以包含基本值.对象.或者函数. 在Ja ...

  3. Object.prototype.toString.call()进行类型判断

    为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...

  4. 流畅web动画的十个法则

    from me: web动画能够带来一个非常酷炫的效果,能够让页面有一个更好的用户体验.对于良好的动画性能没有高招,除了将大量的时间放在测试和优化,当然最重要的还是要易于维护. 流畅web动画的十大法 ...

  5. 跟着百度学PHP[4]OOP面对对象编程-6-构造方法(__construct)和构析方法(__destruct)

    函数就是成员方法(方法有三:构造方法.成员方法.析构方法) 下面是两种方法. 构造方法和构析方法 00x1 构造方法 构造方法会在创建对象之后自动调用.其名称为__construct <?php ...

  6. Caffe学习系列(12):不同格式下计算图片的均值和caffe.proto

    均值是所有训练样本的均值,减去之后再进行训练会提高其速度和精度. 1.caffe下的均值 数据格式是二进制的binaryproto,作者提供了计算均值的文件compute_image_mean, 计算 ...

  7. Valgrind 3.11.0编译安装

    Valgrind 3.11.0编译安装 Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具. Valgrind遵守GNU通用公共许可证条款,是一款自由软件. 到3.3.0版本为 ...

  8. SVN的目录说明

    subversion目录说明:*dav目录:是提供apache与mod_dav_svn使用的目录,让他们存储内部数据*db目录:就是所有版本控制的数据存放文件*hooks目录:放置hook脚本文件的目 ...

  9. putty快速设置本地代理

    sudo plink -D 127.0.0.1:8888 -l root -P 443 -pw xxx 104.xxx.xxx.xxx

  10. IntelliJ IDEA 常用快捷键列表及技巧大全

    IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N  查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和 ...