Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用Gradle配置Spring AOP_Java_Spring_Spring Boot - Fatal编程技术网

Java 无法使用Gradle配置Spring AOP

Java 无法使用Gradle配置Spring AOP,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试使用SpringBoot和AOP记录异常。使用gradlew和Java1.8 Main.java @SpringBootApplication @EnableAutoConfiguration @EnableAspectJAutoProxy public class Main implements CommandLineRunner { @Override public void run(String... args) { try{ ThrowingExample()

我正在尝试使用SpringBoot和AOP记录异常。使用gradlew和Java1.8

Main.java

@SpringBootApplication
@EnableAutoConfiguration
@EnableAspectJAutoProxy
public class Main implements CommandLineRunner {

@Override
public void run(String... args) {
    try{
    ThrowingExample();
    }catch(Exception e){
       System.out.println("This message is printed");
    }
}

public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
}

static void ThrowingMethod() throws FileNotFoundException {
  throw new FileNotFoundException();
}
}
AfterhrowAspect.java

@Component("AfterThrowAspect")
@Aspect
public class AfterThrowAspect {

  @AfterThrowing(pointcut = "execution(* *.*.*(..))", throwing = "exception")
  public void logAfterThrowing(Exception exception) {
    System.out.println("Not Printed @AfterReturning:"+new Date());
    System.out.println("Exception caught:"+ exception.getMessage());**
  }

}
我的Gradle文件是

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'

mainClassName = 'learn.Main'

repositories {
   maven  {
    url "http://repo1.maven.org/maven2"
   }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations {
  aspectjweaver
}

dependencies {
   compile "joda-time:joda-time:2.2"
   testCompile "junit:junit:4.12"
   compile("org.springframework.boot:spring-boot-starter-web") {
   exclude module: "spring-boot-starter-tomcat"
}
compile "org.springframework:spring-webmvc:4.+"
compile "org.springframework:spring-aop:4.+"
compile "org.springframework.boot:spring-boot-starter-web:1.+"
compile "org.aspectj:aspectjrt:1.+"
compile "org.slf4j:slf4j-api:1.+"

aspectjweaver "org.aspectj:aspectjweaver:1.+"
runtime configurations.aspectjweaver.dependencies
}
异常后从不调用logAfterThrowing方法。我用的是Intellj,ide说投掷法是推荐的方法

我是Java新手。环顾网络,让我觉得这应该是可行的,但不会发生。它编译并运行,但从未调用PosthrowAspect.java中的LogPosthrowing。所有文件都位于相同的层次结构和相同的包中

编辑

我想我已经发现你的代码有问题了。Spring面向方面的编程将只适用于由Spring容器维护的bean由于引发异常的方法是静态方法,因此spring无法拦截该异常

解决方案

在服务或组件中定义方法并自动连接。

工作示例回购

完整答案

首先,在服务bean中定义您的方法

@Service
public class ExceptionalService {
    public void thorowException() throws Exception {
        throw new Exception();
    }
}
第二个,而不是
@组件
注释,您应该在AspectJ类中使用
@配置
。另外,在切入点中使用适当的包名

@Aspect
@Configuration
public class ErrorInterceptor {
    @AfterThrowing(pointcut = "execution(* net.asifhossain.*.*(..))", throwing = "ex")
    public void errorInterceptor(Exception ex) {

        ex.printStackTrace();
    }
}
Third自动连接服务并使用它

@SpringBootApplication
@EnableAspectJAutoProxy
public class AopExampleApp implements CommandLineRunner {

    @Autowired
    ExceptionalService service;

    @Override
    public void run(String... args) throws Exception {
        try {
            service.thorowException();
            thorowException();
        } catch (Exception ex) {
            // Do nothing Since aop will log the answer
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(AopExampleApp.class);
    }

    public static void thorowException() throws Exception {
        throw new Exception();
    }
}
我已经创建了一篇博客文章,其中包含了如何使用Spring的面向方面编程处理异常的完整步骤。 您可以通过以下链接访问它


我认为您的代码看起来不错,可能是Gradle配置的问题。我只是玩了一下Spring+Aspects+Gradle,并在GitHub上得到了结果。您可以查看一下,也许可以调整您的
构建。gradle
:您是否检查了初始化后的
组件?@Zico我没有看到启动日志。所以我想没有初始化。如果可以,将进行更新。@Zico虽然我在日志中没有看到加载的组件,但appContext.containsBeanDefinition(“AfterHrowAspect”)返回true。我编辑了我的文章以使组件具有名称。尝试在切入点中使用正确的包名称,但更改的注释没有帮助。我的IDE能够获得应用于哪个方面的方法,所以我想切入点是好的minute@VishalKumar我已经更新了我的答案,并在github中添加了一个示例,看看这是否有效。感谢您的回复。当我做春季特价服务时,它起作用了。但是“@Component”注释对于ErrorInterceptor就足够了。不需要“@Configuration”。