在建议后更改AOP中的目标对象

时间:2017-01-24 17:53:53

标签: spring spring-aop

我正在改变AOP @After建议

中的目标对象

我的Target对象是Trade,我的方法是setPrice(...)和getPrice(..),getVolume()等

1)情况1:在getPrice()的After Advice中,如果我修改价格并设置为Trade对象并在客户端访问 - 我没有得到修改值

2)情况2:在getVolume()的After Advice中,如果我修改价格并设置为Trade对象并在客户端访问 - 我获得修改后的值

你能解释一下这种行为吗?

1 个答案:

答案 0 :(得分:0)

我能够解决它。当我尝试创建测试代码时,它发现了不同的东西。

在我看来,当我使用

     @Before(value="execution( * *.getPrice(..))") and
     @After(value="execution( * *.getPrice(..))")


    public class MyAspect {


    @Before(value="execution( * *.getPrice(..))")
    public void logBefore(JoinPoint joinpoint){
        System.out.println("In Before Advice : And the target method is : "                         +joinpoint.getSignature().getName() );

        Object obj = joinpoint.getTarget();


        TradeDaoImpl dao = (TradeDaoImpl)obj;
        System.out.println("In Before Advice : Current DAO price is  : " +dao);
        dao.setPrice(100);
        System.out.println("In Before Advice : New price is " + dao);

    }



    @After(value="execution( * *.getPrice(..))")
    public void logAfter(JoinPoint joinpoint){
        System.out.println("In After Advice : And the target method is : "                         +joinpoint.getSignature().getName() );

        Object obj = joinpoint.getTarget();

        TradeDaoImpl dao = (TradeDaoImpl)obj;
        System.out.println("In After Advice : Current DAO price is  : " +dao);
        dao.setPrice(200);
        System.out.println("In After Advice : New price is " + dao);

    }

}

输出是我的预期

    In Before Advice : And the target method is : getPrice
    In Before Advice : Current DAO price is  : TradeDAOImpl - toString , price is 0
    In Before Advice : New price is TradeDAOImpl - toString , price is 100
    In After Advice : And the target method is : getPrice
    In After Advice : Current DAO price is  : TradeDAOImpl - toString , price is 100
    In After Advice : New price is TradeDAOImpl - toString , price is 200
    **********************************In Junit
    In junit : retuned price is 100
    In Junit Test case  : TradeDAOImpl - toString , price is 200

返回价格为100,因为After Signvice在返回值后工作

但如果我在TradeDAOImpl上做串行,我可以看到,现在TradeDAOImpl的当前价格值是200

但如果我使用

    @Before(value="execution( * com.dimple.dao.TradeDaoImpl.*(..))") and
    @After(value="execution( * com.dimple.dao.TradeDaoImpl.*(..))")

    In Before Advice : And the target method is : getPrice
    In Before Advice : Current DAO price is  : TradeDAOImpl - toString , price is 0
    In Before Advice : New price is TradeDAOImpl - toString , price is 100
    In After Advice : And the target method is : getPrice
    In After Advice : Current DAO price is  : TradeDAOImpl - toString , price is 100
    In After Advice : New price is TradeDAOImpl - toString , price is 200
     **********************************In Junit
    In junit : retuned price is 100
    In Before Advice : And the target method is : toString
    In Before Advice : Current DAO price is  : TradeDAOImpl - toString , price is 200
    In Before Advice : New price is TradeDAOImpl - toString , price is 100
    In After Advice : And the target method is : toString
    In After Advice : Current DAO price is  : TradeDAOImpl - toString , price is 100
    In After Advice : New price is TradeDAOImpl - toString , price is 200
    In Junit Test case  : TradeDAOImpl - toString , price is 100

我已经明白我的toString()在建议之前和之后进一步调用 因此,我得到的返回值为100

虽然我已经在建议之后将价格设定为200,但如果我试图以某种方式得到它,我将无法做到这一点,因为我的切入点表达太宽

很抱歉由于没有测试代码的问题而造成的不便。 感谢@SergeBallesta提供的有用评论。