gpt4 book ai didi

java - 无法使用 Gradle 配置 Spring AOP

转载 作者:行者123 更新时间:2023-11-30 06:19:30 26 4
gpt4 key购买 nike

我正在尝试使用 SpringBoot 和 AOP 记录异常。使用 gradlew 和 Java 1.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();
}
}

AfterThrowAspect.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。我正在使用 Intelj,ide 说 ThrowingMethod 是 AdvisedMethod。

我是 Java 新手。在网上环顾四周让我觉得这应该可行但没有发生。它编译并运行,但 AfterThrowAspect.java 中的 logAfterThrowing 从未调用。所有文件都位于同一层次结构和同一包中。

最佳答案

EDIT

I think I have found the problem with your code. Spring aspect-oriented programming will only work with beans that are maintained by spring container. Since the method throwing the exception is a static method there is no way spring can intercept this.

How to intercept static methods in Spring?

Solution

在服务或组件中定义您的方法并 Autowiring 它。

工作示例存储库 https://github.com/mirmdasif/springmvc/tree/master/aopexceptionhandling

Complete Answer

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

@Service
public class ExceptionalService {
public void thorowException() throws Exception {
throw new Exception();
}
}

第二,您应该在 AspectJ 类中使用 @configuration,而不是 @component 注释。另外,在切入点中使用正确的包名称

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

ex.printStackTrace();
}
}

第三 Autowiring 服务并使用它。

@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 的面向方面编程处理异常的完整分步过程。您可以通过以下链接访问它

Handling Exception Using Spring AOP

关于java - 无法使用 Gradle 配置 Spring AOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48482748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com