Aop difference in aspect and advisor of

Original link: https://blog.csdn.net/u011983531/article/details/70504281

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 tangent point)
  • <Aop: advisor>: define notifier (notifier like section, including notification and tangent point)

Below, we list some difference 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("我要睡觉了!");
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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"/>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

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"/>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

Test code is as follows:

public class TestAOP {
    public static void main(String[] args) {
        method1();
//      method2();
    }

    private static void method1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
        Sleepable sleeper = (Sleepable) context.getBean("human");
        sleeper.sleep();
    }

    private static void method2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Sleepable sleeper = (Sleepable) context.getBean("human");
        sleeper.sleep();
    }

//执行结果
睡觉前要脱衣服!
我要睡觉了!
起床后要穿衣服!
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

<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.

Attachment:
understanding of this problem, in fact, has not been an in-depth understanding, we recommend the following several blog posts, share their hopes.
http://www.iteye.com/problems/69785
http://blog.sina.com.cn/s/blog_5198c7370100hw1p.html
http://blog.csdn.net/huitoukest/article/details/46469177
HTTP: / /www.tz365.cn/ask/shenghuo/2016/0804/739237.html
https://zhidao.baidu.com/question/371238289198208804.html

Guess you like

Origin blog.csdn.net/qq_44750696/article/details/101638722