Spring AOP,从外部 jar 导入的自定义注释方面作为依赖项未执行

问题描述 投票:0回答:1

我一直在开发一个项目 A,其中我在 AdminController 周围添加方面,以便在调用时验证这些 url,但我在单独的存储库 B 中拥有所有自定义注释,我已将其作为依赖项形式添加为 A 中的外部 jar。

以下是项目 B 的文件,我在其中定义了自定义注释。

B项目

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAuth {

}
@Component
@Aspect
public class AuthAspect {

    @Before(value = "@annotation(com.company.services.annotations.CustomAuth)")
    public void doBefore(final JoinPoint joinPoint) {
          System.out.println("Validating the admin url");
    }

    @Around(value = "@annotation(com.company.services.annotations.CustomAuth)")
    public Object doAuthorize(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("During this");
        long start = System.currentTimeMillis();
        Object proceed = proceedingJoinPoint.proceed(); // Continue with the method execution
        long executionTime = System.currentTimeMillis() - start;
        System.out.println(proceedingJoinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }
}

B 的 pom.xml

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
  </dependencies>
  <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

下面是我在项目 A 中的一组文件,其中有 AdminController。

AdminController.java

@RestController
@RequestMapping("/{version}/admin")
public interface AdminController {

    @CustomAuth
    @GetMapping(value = "/node/status",
            produces = {MediaType.APPLICATION_JSON})
    void getStatus();

  }

A的pom.xml(继承B的依赖)

   <dependencies> 
     <dependency>
            <groupId>com.company.service</groupId>
            <artifactId>filter</artifactId>
            <version>0.0.18</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.7</version>
        </dependency>
    </dependencies>
    <plugins>
      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                       <groupId>com.company.service</groupId>
                       <artifactId>filter</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
             </configuration>
         </plugin>
     <plugins>

SpringBoot主文件

@Configuration
@SpringBootApplication
@EnableAspectJAutoProxy
@ComponentScan(value = {"com.company"})
@EntityScan({"com.company"})
public class SpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class, args);
    }
}

使用此配置,我无法对自定义注释的方面进行加载时间编织。知道为什么这不起作用或者我在这里缺少什么吗?

java spring annotations aspectj spring-aop
1个回答
0
投票

自定义注释不是继承的,不能应用于接口,而是应用于类。

© www.soinside.com 2019 - 2024. All rights reserved.