简体   繁体   English

Spring-Boot:尝试从@PostConstruct 方法抛出自定义 RunTimeException 失败

[英]Spring-Boot: Trying to throw a custom RunTimeException from @PostConstruct method fails

Basically, the code I have is here below.基本上,我的代码在下面。 Take into account that this is a "test" state of the code.考虑到这是代码的“测试”状态。 The original problematic was a call on init() to a different class that threw a checked exception.最初的问题是调用 init() 到另一个抛出已检查异常的类。 This throw was catched by a try/catch block, then the application failed when trying to create the exception.此抛出被 try/catch 块捕获,然后应用程序在尝试创建异常时失败。 All that has been removed for clarity's sake, as the problem was in the "MyCustomRuntimeException" creation.为了清楚起见,所有这些都已删除,因为问题出在“MyCustomRuntimeException”创建中。

@Component
public class ClassName {

  public ClassName() {
    //minor, non problematic operations.
  }

  @PostConstruct
  public void init() {
    throw new MyCustomRuntimeException("AAAAAAAH");
  }

}

MyCustomRuntimeException is defined like this: MyCustomRuntimeException 定义如下:

public class MyCustomRuntimeException extends RuntimeException {

  public MyCustomRuntimeException (String message) {
    super(message);
  }
}

And, I'm getting an "UnsatisfiedDependencyException" when creating a class that uses this class.而且,在创建使用此类的类时,我收到了“UnsatisfiedDependencyException”。 The console points towards the line where the new MyCustomRuntimeException is being thrown, and I don't really get what's going on.控制台指向抛出新的 MyCustomRuntimeException 的那一行,我真的不明白发生了什么。

Also, "MyCustomRuntimeException" started as a regular exception, but I saw that I should throw a RunTimeException instead because the @PostConstruct forbids checked exceptions to be thrown .此外,“MyCustomRuntimeException”开始作为一个常规异常,但我看到我应该抛出一个 RunTimeException 因为@PostConstruct 禁止抛出已检查的异常 And I've also tried to throw a standard RunTimeException with no luck.而且我还尝试在没有运气的情况下抛出标准的 RunTimeException。

So, I'm clueless here.所以,我在这里一无所知。 Any ideas on why I can't throw this exception?关于为什么我不能抛出这个异常的任何想法?

Every bean in the context needs to be correctly created.上下文中的每个 bean 都需要正确创建。 When an error occurs the creation of beans will stop/fail and the context (or in other words your Application) will not start.当发生错误时,bean 的创建将停止/失败并且上下文(或换句话说您的应用程序)将不会启动。

You get an UnsatisfiedDependencyException due to the fact that the ClassName bean is created because it is needed by the other bean.您会收到UnsatisfiedDependencyException因为ClassName bean 是因为另一个 bean 需要它而创建的。 After construction of ClassName it will call the @PostConstruct of the ClassName bean, and that fails due to an exception.在构造ClassName ,它将调用ClassName bean 的@PostConstruct ,并且由于异常而失败。 Hence the instance doesn't get created, hence an UnsatisfiedDependencyException .因此不会创建实例,因此会出现UnsatisfiedDependencyException

The root cause of the UnsatisfiedDependencyException will be the exception thrown by your own initializer method. UnsatisfiedDependencyException的根本原因将是您自己的初始化方法抛出的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM