Spring Boot AOP不适用于Maven

时间:2018-07-25 06:42:59

标签: java maven aop spring-aop

我正在尝试通过自定义注释和“周围”建议在Spring-boot中实现AOP。我在GET api之前添加了注释,但无法在Aspect中捕获该功能。

pom.xml

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.0.7.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <scope>compile</scope>
        <version>1.8.12</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.11</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

HiveService.java

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface HiveService{
//public String name() default "";
}

Followup.java

@HiveService
@RequestMapping(value="rest/services/{pr 
oduct}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
public String getFollowupCategoryList(@PathVariable String product) {
    LOGGER.info("[getFollowupCategoryList]: started again ");
}

HiveConsumer.java

@Aspect
@Component
public class HiveConsumer {


@Around("@annotation(com.abc.xyz.HiveService)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Logger LOGGER = LoggerFactory.getLogger(com.abc.xyz.FollowupController.class);
    LOGGER.info("Before");

    Object proceed = joinPoint.proceed();

    long executionTime = System.currentTimeMillis() - start;

    LOGGER.info(joinPoint.getSignature() + " executed in " + executionTime + "ms");
    return proceed;
}
}

Spring AOP的配置文件

@SpringBootApplication
@RestController
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ActivitiRestApplication extends 
SpringBootServletInitializer  {


public static void main(String[] args) throws Exception {
    SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder app) {
    return app.sources(applicationClass);
}

private static Class<ActivitiRestApplication> applicationClass = ActivitiRestApplication.class;

1 个答案:

答案 0 :(得分:0)

如果您运行的应用程序运行良好

  • 向您的POM添加依赖项org.springframework.boot:spring-boot-starter-web:2.0.3.RELEASE
  • 删除冗余org.aspectj:aspectjrt,因为它是org.aspectj:aspectjweaver的子集(并且您始终指定了不匹配的版本号),
  • @RestController添加到包含带注释方法的类中,
  • make方法getFollowupCategoryList(..)实际上返回一个字符串(在您的示例代码中,它不返回任何内容,因此甚至不进行编译)。

这是完整的MCVE的代码固定版本,即包括程序包和类名。为了进行此演示,我将日志记录代码更改为使用System.out,以最大程度地减少依赖性。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.scrum-master.stackoverflow</groupId>
  <artifactId>spring-boot-51512380</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.7.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <scope>compile</scope>
      <version>1.8.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
      <version>2.0.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.0.3.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

更新:实际上,您甚至可以将POM减小为:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.scrum-master.stackoverflow</groupId>
  <artifactId>spring-boot-51512380</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
      <version>2.0.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.0.3.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>
package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface HiveService {
  //public String name() default "";
}
package de.scrum_master.app;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SomeClass {
  @HiveService
  @RequestMapping(value = "rest/services/{product}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
  public String getFollowupCategoryList(@PathVariable String product) {
    System.out.println("[getFollowupCategoryList]: started again");
    return "foo";
  }
}
package de.scrum_master.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ActivitiRestApplication extends SpringBootServletInitializer {
  private static Class<ActivitiRestApplication> applicationClass = ActivitiRestApplication.class;

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder app) {
    return app.sources(applicationClass);
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(applicationClass, args);
  }
}
package de.scrum_master.app;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class HiveConsumer {
  @Around("@annotation(de.scrum_master.app.HiveService)")
  public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    System.out.println("Before");
    Object proceed = joinPoint.proceed();
    long executionTime = System.currentTimeMillis() - start;
    System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
    return proceed;
  }
}

现在,如果您运行该应用程序并在Web浏览器中打开URL http://localhost:8080/rest/services/something,它将在浏览器中显示foo,并在控制台上显示:

Before
[getFollowupCategoryList]: started again
String de.scrum_master.app.SomeClass.getFollowupCategoryList(String) executed in 2ms