java - spring AOP 不生效
PHPz
PHPz 2017-04-18 10:49:46
0
1
642

写了个切面, 如果切点定义声明在Controller上面的方法,这对应的通知能够执行, 如果不是Controller直接调用的则通知无法执行.

切面声明:


@Aspect
@Component
public class SessionAspect {

    @Pointcut("execution(* cn.test.service.impl.ShopServiceImpl.myShops(..))")
    private void myShops() {
    }
    
    @Pointcut("execution(* cn.test.service.impl.ShopServiceImpl.test(..))")
    private void test() {
    }

    @Before("myShops()")
    public void doBefore() {
        System.out.println("hello");
    }
    
    @Before("test()")
    public void doBefore() {
        System.out.println("test");
    }
}

controller 的方法

@RequestMapping(value = "/my", method = RequestMethod.GET)
public Object myShops(String userSid, ModelMap result) {
    return this.shopService.myShops(userSid);
}

因为myShops在controller中直接调用, 通知能够触发执行, 打印出hello, 而test方法没有在controller中显示调用, 所有即便执行了test方法也不会通知也没有被触发执行.
基于Spring MVC.

PHPz
PHPz

学习是最好的投资!

répondre à tous(1)
伊谢尔伦

Spring AOP uniquement des proxys Bean. Si votre instance n'est pas obtenue à partir de Spring mais que Bean est votre propre instance, elle ne peut pas être proxy.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!