Spring AOP essentials
1. ProxyFactoryBean is used for declaring AOP
2. ProxyFactory is for programming AOP
3. AspectJFactoryBean is for AspectJ integration
4. For interface, JDK proxy is used
5. For class, CGLib is used
6. Ref InvocationHandler
Defining AOP terminology
ADVICE
In AOP terms, the job of an aspect is called advice.
Spring aspects can work with five kinds of advice:
Before—The advice functionality takes place before the advised method is invoked.
After—The advice functionality takes place after the advised method completes, regardless of the outcome.
After-returning—The advice functionality takes place after the advised method successfully completes.
After-throwing—The advice functionality takes place after the advised method throws an exception.
Around—The advice wraps the advised method, providing some functionality before and after the advised method is invoked.
JOIN POINTS
A join point is a point in the execution of the application where an aspect can be plugged in. This point could
be a method being called, an exception being thrown, or even a field being modified.
These are the points where your aspect’s code can be inserted into the normal flow of
your application to add new behavior.
POINTCUTS
Pointcuts help narrow down the join points advised by an aspect.
If advice defines the what and when of aspects, then pointcuts define the where. A pointcut definition matches one or more join points at which advice should be woven.
ASPECTS
An aspect is the merger of advice and pointcuts. Taken together, advice and point-cuts define everything there is to know about an aspect—what it does and where and when it does it.
INTRODUCTIONS
An introduction allows you to add new methods or attributes to existing classesv
WEAVING
Weaving is the process of applying aspects to a target object to create a new proxied object. The aspects are woven into the target object at the specified join points. The weaving can take place at several points in the target object’s lifetime:
Compile time—Aspects are woven in when the target class is compiled. This requires a special compiler. AspectJ’s weaving compiler weaves aspects this way.
Classload time—Aspects are woven in when the target class is loaded into the
JVM. This requires a special ClassLoader that enhances that target class’s byte-code before the class is introduced into the application. AspectJ 5’s load-time weaving (LTW) support weaves aspects in this way.
Runtime—Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy object that will delegate to the target object while weaving in the aspects. This is how Spring AOP aspects are woven.
Spring’s AOP support
SPRING ADVICE IS WRITTEN IN JAVA
SPRING ADVISES OBJECTS AT RUNTIME
SPRING ONLY SUPPORTS METHOD JOIN POINTS
Selecting join points with pointcuts
In Spring AOP, pointcuts are defined using AspectJ’s pointcut expression language.
Spring leverages AspectJ’s pointcut expression language for defining Spring aspects.
args() Limits join point matches to the execution of methods whose arguments are instances of the given types
@args() Limits join point matches to the execution of methods whose arguments are annotated with the given annotation types
execution() Matches join points that are method executions
this() Limits join point matches to those where the bean reference of the AOP proxy is of a given type
target() Limits join point matches to those where the target object is of a given type
@target() Limits matching to join points where the class of the executing object has an annotation of the given type
within() Limits matching to join points within certain types
@within() Limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP)
@annotation Limits join point matches to those where the subject of the join point has the
given annotation
Writing pointcuts
Selecting the Instrument’s play() method with an AspectJ pointcut expression
execution(*com.springinaction.springidol.Instrument.play()) and bean(eddie)
execution(*com.springinaction.springidol.Instrument.play()) and !bean(eddie)
Declaring aspects in XML
Spring’s AOP configuration elements simplify declaration of POJO-based aspects.
<aop:advisor> Defines an AOP advisor.
<aop:after> Defines an AOP after advice (regardless of whether the advised method returns successfully).
<aop:after-returning> Defines an AOP after-returning advice.
<aop:after-throwing> Defines an AOP after-throwing advice.
<aop:around> Defines an AOP around advice.
<aop:aspect> Defines an aspect.
<aop:aspectj-autoproxy> Enables annotation-driven aspects using @AspectJ.
<aop:before> Defines an AOP before advice.
<aop:config> The top-level AOP element. Most <aop:*> elements must be contained within <aop:config>.
<aop:declare-parents> Introduces additional interfaces to advised objects that are transparently implemented.
<aop:pointcut> Defines a pointcut.
public classAudience{ public voidtakeSeats(){ System.out.println("Theaudienceistakingtheirseats."); } public voidturnOffCellPhones(){ System.out.println("Theaudienceisturningofftheircellphones"); } public voidapplaud(){ System.out.println("CLAPCLAPCLAPCLAPCLAP"); } public voiddemandRefund(){ System.out.println("Boo!Wewantourmoneyback!"); } }
<bean id="audience" class="com.springinaction.springidol.Audience"/>
<aop:config> <aop:aspect ref="audience"> <aop:before pointcut= "execution(* com.springinaction.springidol.Performer.perform(..))" method="takeSeats"/> <aop:before pointcut= "execution(* com.springinaction.springidol.Performer.perform(..))" method="turnOffCellPhones"/> <aop:after-returning pointcut= "execution(* com.springinaction.springidol.Performer.perform(..))" method="applaud"/> <aop:after-throwing pointcut= "execution(* com.springinaction.springidol.Performer.perform(..))" method="demandRefund"/> </aop:aspect> </aop:config>
Defining a named pointcut to eliminate redundant pointcut definitions
<aop:config> <aop:aspect ref="audience"> <aop:point cutid="performance"expression= "execution(* com.springinaction.springidol.Performer.perform(..))" /> <aop:before pointcut-ref="performance" method="takeSeats" /> <aop:before pointcut-ref="performance" method="turnOffCellPhones"/> <aop:after-returning pointcut-ref="performance" method="applaud" /> <aop:after-throwing pointcut-ref="performance" method="demandRefund" /> </aop:aspect> </aop:config>
Declaring around advice
public voidwatchPerformance(ProceedingJoinPointjoinpoint){ try { System.out.println("Theaudienceistakingtheirseats."); System.out.println("Theaudienceisturningofftheircellphones"); longstart=System.currentTimeMillis(); joinpoint.proceed(); longend=System.currentTimeMillis(); System.out.println("CLAPCLAPCLAPCLAPCLAP"); System.out.println("Theperformancetook"+(end-start) + "milliseconds."); } catch(Throwablet){ System.out.println("Boo!Wewantourmoneyback!"); } }
<aop:config> <aop:aspectref="audience"> <aop:pointcutid="performance2"expression= "execution(* com.springinaction.springidol.Performer.perform(..))" /> <aop:around pointcut-ref="performance2" method="watchPerformance()"/> </aop:aspect> </aop:config>
Passing parameters to advice
package com.springinaction.springidol; public interface MindReader{ void interceptThoughts(Stringthoughts); String getThoughts(); }
package com.springinaction.springidol; public class Magician implements MindReader{ privateStringthoughts; public voidinterceptThoughts(Stringthoughts){ System.out.println("Interceptingvolunteer'sthoughts"); this.thoughts=thoughts; } public StringgetThoughts(){ returnthoughts; } }
package com.springinaction.springidol; public interface Thinker{ void thinkOfSomething(Stringthoughts); }
package com.springinaction.springidol; public class Volunteer implements Thinker{ privateStringthoughts; public voidthinkOfSomething(Stringthoughts){ this.thoughts=thoughts; } public StringgetThoughts(){ returnthoughts; } }
<aop:config> <aop:aspect ref="magician"> <aop:pointcut id="thinking" expression="execution(* com.springinaction.springidol.Thinker.thinkOfSomething(String)) and args(thoughts)"/> <aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts" /> </aop:aspect> </aop:config>
@Test public voidmagicianShouldReadVolunteersMind(){ volunteer.thinkOfSomething("QueenofHearts"); assertEquals("QueenofHearts",magician.getThoughts()); }
Introducing new functionality with aspects
Annotating aspects
A key feature introduced in AspectJ 5 is the ability to use annotations to create aspects.
Prior to AspectJ 5, writing AspectJ aspects involved learning a Java language extension.
But AspectJ’s annotation-oriented model makes it simple to turn any class into an
aspect by sprinkling a few annotations around. This new feature is commonly referred
to as @AspectJ.
package com.springinaction.springidol; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public classAudience{ @Pointcut( "execution(* com.springinaction.springidol.Performer.perform(..))") public voidperformance(){ } @Before("performance()") public voidtakeSeats(){ System.out.println("Theaudienceistakingtheirseats."); } @Before("performance()") public voidturnOffCellPhones(){ System.out.println("Theaudienceisturningofftheircellphones"); } @AfterReturning("performance()") public voidapplaud(){ System.out.println("CLAPCLAPCLAPCLAPCLAP"); } @AfterThrowing("performance()") public voiddemandRefund(){ System.out.println("Boo!Wewantourmoneyback!"); } }
<aop:aspectj-autoproxy/> <aop:aspectj-autoproxy/>
Annotating around advice
@Around("performance()") public voidwatchPerformance(ProceedingJoinPointjoinpoint){ try { System.out.println("Theaudienceistakingtheirseats."); System.out.println("Theaudienceisturningofftheircellphones"); longstart=System.currentTimeMillis(); joinpoint.proceed(); longend=System.currentTimeMillis(); System.out.println("CLAPCLAPCLAPCLAPCLAP"); System.out.println("Theperformancetook"+(end-start) + "milliseconds."); } catch(Throwablet){ System.out.println("Boo!Wewantourmoneyback!"); } }
Passing arguments to annotated advice
package com.springinaction.springidol; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public classMagicianimplementsMindReader{ privateStringthoughts; @Pointcut("execution(*com.springinaction.springidol." + "Thinker.thinkOfSomething(String))&&args(thoughts)") public voidthinking(Stringthoughts){ } @Before("thinking(thoughts)") public voidinterceptThoughts(Stringthoughts){ System.out.println("Interceptingvolunteer'sthoughts:"+thoughts); this.thoughts=thoughts; } public StringgetThoughts(){ returnthoughts; } }
Annotating introductions
package com.springinaction.springidol; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.DeclareParents; @Aspect public classContestantIntroducer{ @DeclareParents( value="com.springinaction.springidol.Performer+", defaultImpl=GraciousContestant.class) public staticContestantcontestant; }
Injecting AspectJ aspects
相关推荐
《Spring by Example》是一本由David Winterfeldt编写的关于Spring框架的详细教程,旨在帮助读者深入理解并熟练使用Spring框架。此书自版本1.5以来,深受开发者喜爱,版权属于作者,从2008年持续更新至2015年。 ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. **AOP(面向切面编程)**:Spring支持切面编程,可以方便地实现日志记录、事务管理等横切关注点。 4. **数据访问**:Spring提供了JDBC模板、Hibernate、MyBatis等工具,简化了数据库操作。 5. **MVC架构**:...
在Spring MVC中,DI帮助管理对象之间的依赖关系,而AOP则允许我们在不修改代码的情况下实现横切关注点,如日志、事务管理等。 开始开发Spring MVC应用前,确保已安装以下工具: 1. Java Development Kit (JDK):...
Learn how to replace common EJB features with Spring alternatives, including Spring's comprehensive AOP-based transaction management framework. Learn to integrate and use these other open source ...
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="byName"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method=...
Spring的面向切面编程(AOP)可以与RMI集成,提供日志记录、性能监控、安全控制等功能。通过定义切点和通知,可以在RMI调用前后执行特定逻辑。 ### 5. Spring RMI优化 - **RMI超时设置**:可以配置RMI连接和操作的...
在IT行业中,Spring框架是Java开发中的一个基石,它提供了丰富的功能来简化应用程序的构建,包括依赖注入(DI)和面向切面编程(AOP)。而Mybatis则是一个轻量级的持久层框架,它专注于SQL映射和数据库操作。将这两...
Spring 框架是Java开发中的一个核心组件,它提供了依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)等特性,极大地简化了企业级应用的开发工作。Spring配置文件是Spring...
- **AOP术语**:了解Spring AOP的关键术语,如切面(Aspect)、连接点(JoinPoint)、通知(Advice)等。 ### Spring AOP示例 #### Annotation方式实现AOP - **步骤1**:创建Web项目并引入必要的库。 - **步骤2**...
`autowire`属性可以设置为`byName`、`byType`、`constructor`或`default`,让Spring根据bean的名称、类型或构造器自动查找并注入依赖。 七、Spring与其他技术的集成 Spring支持与各种技术的集成,如JDBC、JMS、...
Spring 的核心特性包括依赖注入(Dependency Injection, DI)、面向切面编程(Aspect-Oriented Programming, AOP)、事务管理等。 #### 二、Spring之旅 ##### 2.1 建立开发环境 在开始学习 Spring 之前,需要搭建...
Spring是一个轻量级的应用框架,其核心功能包括依赖注入(DI)和面向切面编程(AOP)。Spring3.0版本增加了对JSR-303 Bean验证的支持,并且改进了对注解的支持。Spring与Struts2结合使用时,可以通过Spring容器管理...
AOP(Aspect Oriented Programming)模块是Spring框架中的一个重要组成部分,它提供了一种在运行时动态插入代码的能力,可以用来实现诸如日志记录、性能监控等功能,而不必修改业务逻辑代码本身。 **1.8 解释JDBC...
5. **ByName和ByType注入**:Spring可以自动根据属性名称(ByName)或类型(ByType)寻找匹配的bean进行注入。这在不提供显式配置的情况下很有用。 6. **SPEL(Spring Expression Language)**:SPEL是Spring的...
- **依赖注入**:Spring通过构造函数、setter方法、自动装配(byName或byType)等方式,将其他Bean注入到当前Bean中。 3. **Bean的作用域**: - Singleton:Spring默认的Bean作用域,每个Bean只有一个实例。 - ...
4. **自动装配**:Spring提供了自动装配功能,通过`autowire`属性,如`<bean autowire="byName|byType|constructor|default|no"`,可以简化配置。例如,`byType`会根据类型自动匹配并注入Bean。 5. **AOP配置**:...
- **QBE**(Query By Example):通过实例对象来构建查询条件。 - **离线查询**:在不连接数据库的情况下预编译查询语句。 - **复合查询**:组合多个查询条件。 - **分页查询**:对查询结果进行分页处理。 3. *...