spring AOP 的几种实现方式(能测试)
2016-06-13 18:12
567 查看
我们经常会用到的有如下几种
1、基于代理的AOP
2、纯简单Java对象切面
3、@Aspect注解形式的
4、注入形式的Aspcet切面
一、需要的java文件
二、application.xml
三、测试
四、环绕模式
测试在上面的
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 点我下载
相关文章推荐
- 并发编程学习总结(六) :java 显式锁ReentrantLock使用详解之测试锁与超时
- myeclipse中的js文件报错
- Java中Class对象详解、类名.class、class.forName()、 getClass()区别
- 编程之美2.6精确表达浮点数Java版
- Eclipse Myeclipse 设定文件的默认打开方式
- spring 时间格式化注解@DateTimeFormat @JsonFormat
- java8中switch(String)的错误解决
- JAVA相关博客
- 主动获取spring容器工具类SpringContextUtil
- eclipse老版本加入新SDK后出现问题
- javacv开发详解之1:调用本机摄像头视频(建议使用javaCV1.3.3版本)
- Java List<>排序——重载Collections.sort方法
- 把eclipse工程迁移到Android studio,build成功但run时报butterknife的错,error: duplicate class: class_name$$View(类重复)
- java的一些知识(六)
- red服务在Eclipse中配置
- java中用FileDialog打开文件并将内容添加到文本
- java对象转json
- java 快速排序
- 【原创】增强版冒泡排序算法-java版
- SpringBoot之Hypermedia-Driven RESTful Web Service