- 浏览: 38205 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
Zhang987526341:
楼主你好,我复制您的代码后,在test类里面运行出现:java ...
java发送邮件 -
Wentasy:
不错的文章,学习了。
Java定时器
引用
配置可以通过xml文件来进行,大概有四种方式:
1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等
2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象
3. 通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点
4. 通过<aop:config>来配置
1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等
2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象
3. 通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点
4. 通过<aop:config>来配置
一、基于代理的AOP
1、创建通知
package com.demo.aop.bean; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; /** * 通知 * */ public class ActionAdvice implements MethodBeforeAdvice,AfterReturningAdvice{ public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println("执行方法之后调用"+System.currentTimeMillis()); } public void before(Method method, Object[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println("执行方法之前调用"+System.currentTimeMillis()); } }
2、Service层
package com.demo.aop.service; public interface IPersonService { public void doUpdate(); }
3、Service实现层
package com.demo.aop.service.impl; import org.springframework.stereotype.Component; import com.demo.aop.service.IPersonService; public class PersonService implements IPersonService { public void doUpdate() { // TODO Auto-generated method stub System.out.println("执行更新方法"+System.currentTimeMillis()); } }
4、文件配置
第一种:不实用自动代理
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <!-- 基于代理的AOP --> <!-- 创建通知 --> <bean id="actionAdvice" class="com.demo.aop.bean.ActionAdvice" /> <!-- 使用正则表达式切点 --> <bean id="actionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*doUpdate" /> </bean> <!-- 通知 与切点结合--> <bean id="actionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="actionAdvice" /> <property name="pointcut" ref="actionPointcut" /> </bean> <!-- 代理对象 --> <bean id="actionProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="personService" /> <property name="interceptorNames" value="actionAdvisor" /> <property name="proxyInterfaces" value="com.demo.aop.service.IPersonService" /> </bean><!--此种代理,创建:IPersonService actionProxy声明,使用代理进行调用 --> <bean id="personService" class="com.demo.aop.service.impl.PersonService"/> </beans>
第二种:使用自动代理
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <!-- 基于代理的AOP --> <!-- 创建通知 --> <bean id="actionAdvice" class="com.demo.aop.bean.ActionAdvice" /> <!-- 使用正则表达式切点 --> <bean id="actionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*doUpdate" /> </bean> <!-- 通知 与切点结合--> <bean id="actionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="actionAdvice" /> <property name="pointcut" ref="actionPointcut" /> </bean> <!-- 代理对象 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <!--自动代理,创建对象正常使用,会在调用指定方法时进行执行--> <bean id="personService" class="com.demo.aop.service.impl.PersonService"/> </beans>
5、测试
第一种:不使用自动代理情况下,注意创建对象时要将代理注入
package com.demo.aop.AopTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.demo.aop.service.IPersonService; /** * 测试基于代理的AOP * @author wwl * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" }) public class AopTest { /** * 用接口声明,注入代理对象 */ @Autowired private IPersonService actionProxy; @Test public void testAop(){ actionProxy.doUpdate(); } }
第二种:使用自动代理
package com.demo.aop.AopTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.demo.aop.service.IPersonService; /** * 测试基于代理的AOP * @author wwl * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" }) public class AopTest { /** * 自动创建代理 */ @Autowired private IPersonService personService; @Test public void testAop(){ personService.doUpdate(); } }
二、使用AspectJ提供的注解
1、创建通知
package com.demo.aop.bean; 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; /** * 通知 * */ @Component @Aspect public class Action1Advice{ @Pointcut("execution(* *.doUpdate())") public void actionPoint(){} @AfterReturning("actionPoint()") public void afterAction(){ System.out.println("执行方法之后调用"+System.currentTimeMillis()); } @Before("actionPoint()") public void beforeAction(){ System.out.println("执行方法之前调用"+System.currentTimeMillis()); } }
2、Service层
package com.demo.aop.service; public interface IPersonService { public void doUpdate(); }
3、Service实现层
package com.demo.aop.service.impl; import org.springframework.stereotype.Component; import com.demo.aop.service.IPersonService; @Component public class PersonService implements IPersonService { public void doUpdate() { // TODO Auto-generated method stub System.out.println("执行更新方法"+System.currentTimeMillis()); } }
4、文件配置
<?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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!-- @AspectJ注解 --> <!-- 激活组件扫描功能--> <context:component-scan base-package="com.demo"/> <!-- 激活自动代理功能 --> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
5、测试
package com.demo.aop.AopTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.demo.aop.service.IPersonService; /** * 测试使用AspectJ提供的注解切面 * @author wwl * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/context-aop1.xml" }) public class Aop1Test { @Autowired private IPersonService personService; @Test public void testAop(){ personService.doUpdate(); } }
三、纯POJO切面(不使用注解,只在配置中注入)
1、创建通知
package com.demo.aop.bean; import org.springframework.stereotype.Component; /** * 通知 * */ @Component public class Action1Advice{ public void afterAction(){ System.out.println("执行方法之后调用"+System.currentTimeMillis()); } public void beforeAction(){ System.out.println("执行方法之前调用"+System.currentTimeMillis()); } }
2、Service层
...........
3、Service实现层
...........
4、文件配置
<?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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!-- @AspectJ注解 --> <!-- 激活组件扫描功能 --> <context:component-scan base-package="com.demo" /> <aop:config> <aop:aspect ref="action1Advice"> <aop:before method="beforeAction" pointcut="execution(* *.doUpdate(..))" /> <aop:after method="afterAction" pointcut="execution(* *.doUpdate(..))" /> </aop:aspect> </aop:config> <!--或者--> <!-- <aop:config> <aop:pointcut id="myPointcut" expression="execution(* *.doUpdate(..))"/> <aop:aspect id="action1Advice" ref="action1Advice" > <aop:before method="beforeAction" pointcut-ref="myPointcut"/> <aop:after method="afterAction" pointcut-ref="myPointcut"/> </aop:aspect> </aop:config> --> </beans>
5、测试
引用
...
总结:总体看其实两种方式,一种是使用代理,一种是使用AOP(是否使用注解)。
流程:先创建通知,创建切点,再将通知与切点绑定使用代理进行处理调用
相关推荐
Spring实现AOP的4种方式
Spring中实现AOP有很多方式,这里总结了四种,有基于代理的实现方式,也有基于AspectJ 的注解方式,还有基于AOP标签的配置
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
配置可以通过xml文件来进行,大概有四种方式: 1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等(基于代理的AOP ) 2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从...
使用Spring的注解方式实现AOP的细节
NULL 博文链接:https://zhang-yingjie-qq-com.iteye.com/blog/319927
Spring-Aop源码实现
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
演示了spring基于AOP代理TransactionProxyFactoryBean实现事务
Spring实现AOP的四种方式.pdf
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
spring aop demo 通过注解和配置文件的两种实现方式 包含jar包 下载直接运行
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
初探spring aop内部实现 、从源代码解读spring之DataSource实现和FactoryBean模式
使用Spring的AOP技术,实现日志记录功能。记录如下信息: 在哪个时间访问了哪个方法,方法传入了哪些参数
spring_AOP实现方法大全spring_AOP实现方法大全
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。