0

我有一个方面的建议,可以跟踪用@Service 注释的类的执行。该代码目前正在运行,但我想将其更改为跟踪控制器上的 REST 端点,而不是自动连接的服务。这是代码:

@Aspect
public class AuditingAspect
{
    @Pointcut(
            //TODO Change pointcut from public methods in Services to REST endpoints in Controllers
            "execution(public * my.base.package..*.*(..))" //Must be in package
                    //+ " && @within(org.springframework.stereotype.Service)" //Has to be a service
                    + " && @within(org.springframework.stereotype.Controller)" //Has to be a controller
    )
    public void auditLoggingPointCut() {
        //no op
    }

    @Around(value ="auditLoggingPointCut()")
    public Object logAround(final ProceedingJoinPoint joinPoint) throws Throwable
    {
        System.out.println("Exection");

        returnVal = joinPoint.proceed();

        // Now Do The After Logging Part
        afterReturningLog(joinPoint, returnVal) ;

        return returnVal;
    }

    private void afterReturningLog(final JoinPoint joinPoint, final Object returnValue)
    {
        System.out.println("Exiting");
    }
}

当我将“内部”从@Service 更改为@Controller 时,我看不到建议的任何输出,但该方法在从 URL 访问时执行。忽略执行的控制器有什么不同?

控制器类如下所示:

@Controller
public class CaseReferralEndpoints {

    @Autowired
    CaseReferralFacade caseReferralFacade;

    @RequestMapping(value="/backgroundcheck/getcasereferrals", method = RequestMethod.GET)
    @ResponseBody
    public List<CaseReferralSection> getCaseReferrals(@RequestParam("caseID") Long caseID) {
        return caseReferralFacade.getCaseReferrals(caseID); 
    }
}

这是我的 applicationContext-aop.xml 完整的配置要大得多,但我相信这是最相关的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">



    <bean class="gov.dhs.uscis.elis2.backend.services.logging.AuditingAspect"/>

    <aop:aspectj-autoproxy proxy-target-class="false" />


</beans>
4

4 回答 4

1

假设您的 @within 配置是正确的,您的麻烦的潜在补救措施如下:

<aop:aspectj-autoproxy proxy-target-class="true" />

此外,您还必须将 CGLIB 添加到您的类路径中

由于您的控制器没有实现接口,因此需要上述步骤

最后,如果您有根上下文和 Web 上下文,则需要将与 aop 相关的内容应用于 Web 上下文(将其放在根上下文中不适用于在 Web 上下文中配置的控制器)

更新

在 Gradle 中将 CGLIB 添加到类路径中添加:

'cglib:cglib:2.2.2'

在 Maven 中它将是:

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>
于 2014-04-25T16:11:14.183 回答
0

当你的切入点表达式有

 @within(org.springframework.stereotype.Service)

使用 && 符号,建议将仅适用于您的服务包中。

我希望你的控制器类不在里面.Service 包,它可能在 .*.*Controller 包中,因此它不会为控制器执行

解决方案 删除内切点表达式

或在切入点表达式中添加控制器

于 2014-04-25T16:09:22.270 回答
0

假设您的切入点是正确的,并且您正在使用两个 spring 上下文,一个用于服务/daos(appcontext),一个用于控制器(servletcontext),我的提示是错误配置的方向。

AOP 配置是仅在声明/扫描的上下文中应用的 spring bean 之一。

因此,假设您的控制器有一个 servletcontext.xml,除非您在此上下文中声明 aop 上下文配置,否则您的切入点不会被应用。

(如果您想将切入点应用于您的服务,则需要应用程序上下文声明。)

于 2014-04-25T18:14:19.127 回答
0

在 applicationContext.xml 中发现错误

我的控制器被从上下文扫描中过滤掉了!我是该项目的众多开发人员之一,所以我最初没想到这里。

<context:component-scan>
    <!-- a bunch of packages -->

    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

但是,我最终添加了一个已被证明更接近我想要的拦截器。因为我们所有的用户操作都是 REST 驱动的,所以审计 REST 调用的调用比尝试和跟踪自动装配的服务方法更容易、更清晰。

于 2014-04-25T18:58:51.360 回答