【问题标题】:Spring AOP Pointcut not called未调用 Spring AOP 切入点
【发布时间】:2013-08-16 07:29:20
【问题描述】:

我使用 JSF 2.2 + Spring 框架 3.2.4

所以,我有这个 applicationContent.xml

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true" />
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <context:component-scan base-package="com.vulcan.controller" />
    <context:component-scan base-package="com.vulcan.service" />
    <context:component-scan base-package="com.vulcan.dao" />
    <context:component-scan base-package="com.vulcan.spring.aop" />

.....

然后我有方面组件 在

package com.vulcan.spring.aop;

@Aspect
public class LoggingService {
    private Log log = LogFactory.getLog(this.getClass());

    @Pointcut("execution(* *.*(..))")
    protected void loggingOperation() {}

    @Before("loggingOperation()")
    public void logJoinPoint()
    {
        System.out.println ("Hello");
     }
   ....

通过这种类型的执行,我假设每个方法都会触发这个切入点。但问题是,这个切入点没有被触发?知道为什么吗?谢谢

仅供参考,我使用 glassfish 4,当我部署我的网络应用程序时,我没有收到任何错误配置。所以我假设我的配置很好。

【问题讨论】:

  • Spring AOP 只建议公共方法。你的建议至少是针对那些触发的,还是根本没有触发?
  • 我已经尝试了 loginController 中的公共方法 e.x (@Controller 注释)。我尝试通过jsf页面调用公共方法doLogin() {return string},它仍然没有响应
  • 对您的 xml 配置 &lt;context:annotation-config /&gt; 的小注释已经通过使用 &lt;context:component-scan ... /&gt; 来暗示。 component-scan 的 base-package 属性可以包含要扫描的包的逗号分隔列表,因此使用 4 个元素代替 4 个元素并在其中放置一个逗号分隔的包列表。为您节省一些 xml。

标签: spring aspectj spring-aop


【解决方案1】:

@Aspect 注释类不会被 Spring 自动检测到,因为它没有被检测到,所以 &lt;aop:aspectj-autoproxy /&gt; bean 不知道它。所以基本上没有方面。

@Component 添加到您的@Aspect 注释类中,以便Spring 可以检测和使用方面。

@Compopnent
@Aspect
public class LoggingService { ... }

或在您的 xml 文件中明确声明方面

<bean class="LoggingService" />

无论哪种方式,&lt;aop:aspectj-autoproxy /&gt; bean 都会获取切面并运行建议。

【讨论】:

  • 在 Spring Boot 中注释 @Aspect 是必要的
【解决方案2】:

尝试使用execution(* *(..))

【讨论】:

  • 感谢您的回复,但仍然无法正常工作。顺便说一句,我在 web.xml 中使用 servlet 过滤器。这是导致这个问题的原因吗?
  • 不,这不会导致此问题。我修改了答案。退房。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多