Spring切面类型比较

1.Spring可以定义4种切面

(1)基于@AspjectJ注解的方式

<aop:aspectj-autoproxy proxy-target-class="true"/>

 

 

(2)基于<aop:aspect>的方式(基于Schema)

<aop:config proxy-target-class="true">
		<aop:aspect ref="adviceMethods">
			<aop:before method="preGreeting"
				pointcut="target(com.baobaotao.NaiveWaiter) and args(name)"
				arg-names="name" />
			<aop:after-returning method="afterReturning"
				pointcut="target(com.baobaotao.SmartSeller)" returning="retVal" />
			<aop:around method="aroundMethod"
				pointcut="execution(* serveTo(..)) and within(com.baobaotao.Waiter)" />
			<aop:after-throwing method="afterThrowingMethod"
				pointcut="target(com.baobaotao.SmartSeller) and execution(* checkBill(..))"
				throwing="iae" />
						<!--
			<aop:after method="afterMethod"
				pointcut="execution(* com..*.Waiter.greetTo(..))" />
		
			<aop:declare-parents
				implement-interface="com.baobaotao.Seller"
				default-impl="com.baobaotao.SmartSeller"
				types-matching="com.baobaotao.Waiter+" />
				-->
            <aop:before method="bindParams" 
                   pointcut="target(com.baobaotao.NaiveWaiter) and args(name,num,..)"/>
		</aop:aspect>
	</aop:config>

 

 

(3)基于<aop:advisor>的方式(基于Schema)

 

<aop:config proxy-target-class="true">
	    <aop:advisor advice-ref="testAdvice"  pointcut="execution(* com..*.Waiter.greetTo(..))"/>  
	</aop:config>

 

 

(4)基于Advisor类的方式

	<bean id="nameAdvisor"
		class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
		p:advice-ref="greetingAdvice">
		<property name="mappedName">
			<value>serve*</value>
		</property>
		
	</bean>

 

 

如果项目采用JDK5.0,可以优先考虑使用@AspectJ,如果项目只能使用低版本的JDK,可以考虑使用<aop:aspect>;如果正在升级一个基于低版本的Spring AOP开发的项目,可以考虑使用<aop:advisor>复用已经存在的advice类;如果项目只能使用低版本的Spring,只能使用Advisor .

 

 2.切面不同定义方式具体实现比较

 

  @AspectJ <aop:aspect> Advisor <aop:advisor>
前置增强 @Before <aop:before> MethodBeforeAdvice 同Advisor
后置增强 @AfterReturning <aop:after-returning> AfterReturningAdvice 同Advisor
环绕增强 @Around <aop:around> MethodInterceptor 同Advisor
抛出异常增强 @AfterThrowing <aop:after-throwing> ThrowsAdvice 同Advisor
final增强 @After <aop:after> 同Advisor
引介增强 @DeclareParents <aop:declare-parents> IntroductionInterceptor 同Advisor

你可能感兴趣的:(Spring切面类型比较)