Programming in spring aop aop: advisor of the difference: aspect and aop

Forwarded From: https: //www.jianshu.com/p/40f79da0cdef

During development, there are a lot of use Spring Aop, when Oriented Programming, we will use <aop: aspect>; during transaction management, we will use <aop: advisor>. So, for the <aop: aspect> and: the difference <aop advisor>, specifically what is it?

As for the difference between the two, there is a lot of information online, but can not seem to make it clear.
First, we need a clear concept of both.

<Aop: aspect>: defining aspects (including notification section and the tangent point)
<AOP: Advisor>: define notifier (notifier like section, including notification and tangent point)
below, we are a few differences between the two.

1, different implementations

<Aop: aspect> the definition section, only need to define the general bean on the line, defined <aop: advisor> notification referenced in the notice must implement the Advice interface.

Here we illustrate.
First, we define a Human Interface Sleepable and implement this interface, the code is as follows:

public interface Sleepable {
    public void sleep();
}

public class Human implements Sleepable {

    @Override
    public void sleep() {
        System.out.println("我要睡觉了!");
    }
}

The following is <aop: advisor> implementations:

//定义通知
public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{
    @Override
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("睡觉前要脱衣服!");
    }

    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
                               Object arg3) throws Throwable {
        System.out.println("起床后要穿衣服!");
    }
}

//aop配置
<bean id="sleepHelper" class="com.ghs.aop.SleepHelper"></bean>

<aop:config>
<aop:pointcut expression="execution(* *.sleep(..))" id="sleepPointcut"/>
<aop:advisor advice-ref="sleepHelper" pointcut-ref="sleepPointcut"/>
</aop:config>

<bean id="human" class="com.ghs.aop.Human"/>

The following is <aop: aspect> implementations:

//定义切面
public class SleepHelperAspect{
    public void beforeSleep(){
        System.out.println("睡觉前要脱衣服!");
    }

    public void afterSleep(){
        System.out.println("起床后要穿衣服!");
    }
}

//aop配置
<bean id="sleepHelperAspect" class="com.ghs.aop.SleepHelperAspect"></bean>

<aop:config>
<aop:pointcut expression="execution(* *.sleep(..))" id="sleepPointcut"/>
<aop:aspect ref="sleepHelperAspect">
<!--前置通知-->
<aop:before method="beforeSleep" pointcut-ref="sleepPointcut"/>
<!--后置通知-->
<aop:after method="afterSleep" pointcut-ref="sleepPointcut"/>
</aop:aspect>
</aop:config>

<bean id="human" class="com.ghs.aop.Human"/>

2, the use of different scenes

<Aop: advisor> mostly used for transaction management.
E.g:

<!-- 会重复读,不会脏读事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" timeout="120" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>

<aop:config proxy-target-class="true">
<aop:pointcut id="txPointCut" expression="..."/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>

<Aop: aspect> mostly used for logging, caching

In fact, whether it is <aop: advisor> or <aop: aspect> final implementation logic is the same.

Summary:
As can be seen, <aop: advisor> and <aop: aspect> in fact will be notified and cut the package, the principle is basically the same, just different ways to use it.

Published 14 original articles · won praise 0 · Views 314

Guess you like

Origin blog.csdn.net/qq_38205881/article/details/104488681