Spring-AOP and mixed using a variety of different types of section summary section

An overview of
our entire range of AOP study, we can conclude that the definition of section four kinds of ways:

Based on the way @AspectJ comment

Based <aop: aspect> manner

Based <aop: advisor> manner

Based Advisor class way

If the project uses JDK5.0 and above, can be used in preference @AspectJ;

If the project can only use the low version of the JDK, you can consider using the <aop: aspect>;

If you are upgrading a version of Spring AOP based on the low development of the project, you can consider using the <aop: advisor> Advice reuse existing classes;

If the project can only use the low version of the Spring, then you can only use the Advisor

In addition, it is worth noting that some of the section-based Advisor can only use the API constructs, such as process-based ControlFlowPointcut facets.

Mixed use of various types of cut
Spring Although the definition section provides four ways, but the underlying implementation technology is the same, and that is based on CGLib and JDK dynamic proxies, so in the same project can be mixed Spring Spring offers cut definition of the various ways.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<! - a mode: Use Advisor API way to achieve process control section ->

<- Method Advisor API flow process designated contact point and tangent point based processes tangent point -!>
<The bean ID = "ControlFlowPointcut" class = "org.springframework.aop.support.ControlFlowPointcut">
<constructor type-Arg = "java.lang.Class"
value = "com.xgj.aop.spring.advisor.ControlFlowAdvisor.WaiterDelegate" />
<constructor Arg-type = "java.lang.String" value = "-Service" />
</ the bean >

<!--Advisor API 切面 -->
<bean id="controlFlowAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"
p:pointcut-ref="controlFlowPointcut" p:advice-ref="greetingBeforeAdvice" />


<-! |||||||||||||||||| dividing line |||||||||||||||||| ->

<! - Second way: @AspectJ annotation define scanning section ->
<AOP: AspectJ the autoproxy-Proxy-target-class = "to true" />


<-! |||||||||||||||||| dividing line |||||||||||||||||| ->


<aop:config proxy-target-class="true">
<!-- 命名切点 -->
<aop:pointcut expression="execution(* com..*.Waiter.greetTo(..))"
id="beforeAdvice" />
<!-- 方式三: 基于<aop:advisor>的方式 -->
<aop:advisor advice-ref="greetingBeforeAdvice"
pointcut-ref="beforeAdvice" />
</aop:config>
<bean id="greetingBeforeAdvice"
class="com.xgj.aop.spring.advisor.schema.advisor.GreetingBeforeAdvice" />


<aop:config proxy-target-class="true">
<aop:pointcut id="bussinessBindParamProgram"
expression="target(com.xgj.aop.spring.advisor.schema.bindParameter.BussinessBindParam) and args(name,num,..)" />
<!-- 方式四:基于<aop:aspect>的方式 -->
<aop:aspect ref="adviceMethodsBindParam">
<aop:before pointcut-ref="bussinessBindParamProgram"
method="crossCutting" />
</aop:aspect>
</aop:config>

</ beans>

Although in the Spring may be a mixture of various types cut to the same effect, but generally not used simultaneously on a project, a single form as far as possible the actual needs of the project, to ensure technology single sex.

A summary of the various facets

Let's compare the definition under section 4 kinds of ways, essentially the same, are defined and enhanced cut-off point, just different manifestations

 

 https://img-blog.csdn.net/20170921114019036?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3NoYW5nd2Vp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast

 From the table, we can see <aop: advisor> actually <aop: aspect> hybrids and Advisor, its cut-point representation and <aop: aspect> the same, and the enhancement definition Advisor same manner.

 

The method of binding the connected point as a reference and Advisor, by enhancing the interface call into the reference method, the <aop: advisor> expression at the tangent point, the point of attachment should be noted that the method can not be bound into the reference point using the cutting function, otherwise it will generate an error.

 

Guess you like

Origin www.cnblogs.com/yazhong-java/p/11726352.html