2

我正在使用 spring-security 3.2.4.RELEASE、spring-security-aspects 3.2.4.RELEASE、AspectJ maven 插件版本 1.6、Java 7。

我使用 AspectJ 的编织而不是 SpringAOP,因此我的 aspectj maven 插件如下所示:

           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>                     
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                    </execution>
                </executions>
                <configuration>
                    <Xlint>ignore</Xlint>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <complianceLevel>${org.aspectj-version}</complianceLevel>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework.security</groupId>
                            <artifactId>spring-security-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
          </plugin>

我有另一个看起来像这样的方面:

package com.mycompany.fw.app.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclarePrecedence;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.security.core.parameters.DefaultSecurityParameterNameDiscoverer;

import com.mycompany.fw.security.Integration;

@Aspect
@DeclarePrecedence("IntegrationAspects*,*")
public class IntegrationAspects {

    ParameterNameDiscoverer parameterNameDiscoverer = new DefaultSecurityParameterNameDiscoverer();

    @Pointcut("(execution(* com.mycompany..*(..))) && @annotation(integrate) ")
    public void integratePointCut(Integration integrate) {

    }

    /**
     * TODO: cache
     * 
     * @param jp
     * @param integrate
     * @throws Throwable
     */
    @Around("integratePointCut(integrate)")
    public Object integrate(final ProceedingJoinPoint pjp, Integration integrate) throws Throwable {
        Object res = pjp.proceed();
        return res;
    }

}

我需要将上述(集成方面)放在任何其他方面(包括 Spring 的安全方面)之前的第一个方面,如您所见,我尝试过@DeclarePrecedence(我也在declare precedence : IntegrationAspects*,*.aj 文件中尝试过),不幸的是, 没有成功。

有人可以指导我如何定义方面调用顺序吗?

4

1 回答 1

4

问题是您没有使用简单的方面名称IntegrationAspects@DeclarePrecedence而是一个小丑字符*。在这种情况下,您需要使用完全限定的类名或创建相同的小丑。

不工作:

@DeclarePrecedence("IntegrationAspects*, *")

作品:

@DeclarePrecedence("IntegrationAspects, *")
@DeclarePrecedence("com.mycompany.fw.security.Integration.IntegrationAspects, *")
@DeclarePrecedence("com.mycompany.fw.security.Integration.IntegrationAspects*, *")
@DeclarePrecedence("*..IntegrationAspects*, *")

等等。顺便说一句,在类名中使用大写的包名和复数看起来真的很难看。

我是 AspectJ 专家,不是 Spring 用户,所以我不能告诉你声明优先级是否也会影响 Spring 提供的方面。它还可能取决于它们是使用本机 AspectJ 还是 Spring-AOP(基于代理的“AOP lite”)实现的。

于 2014-09-08T09:00:47.873 回答