继续失败的Spring bean初始化

时间:2020-07-19 15:11:11

标签: java spring spring-boot

我有一个需要加载OpenCV本机库的Spring Bean,但是我也想如果它加载失败,那么应用程序仍然可以成功启动。我已经捕获了异常,但是Spring仍然抛出异常并关闭了应用程序。为什么Spring在我的catch块中抛出异常?如何使Spring在初始化时忽略异常?我发现了1个相关问题here,但它已经很老了,还没有任何解决方案。

我的代码如下。

const registerBtn = () => {
  let firstName = document.getElementById("inputFirstName").value;
  localStorage.setItem("firstname", firstName);
  let lastName = document.getElementById("inputLastName").value;
  localStorage.setItem("lastname", lastName);
  let email = document.getElementById("inputEmail").value;
  localStorage.setItem("email", email);
  let password = document.getElementById("inputPassword").value;
  localStorage.setItem("pasword", password);
  window.location.replace("../indexs.html");
};

Stacktrace

@Service
public class MyService {
  private boolean isLoaded = false;

  public MyService() {
    try {
      OpencvLoader.loadShared(); // load native library here
      isLoaded = true;
    } catch (Exception e) {
      LOGGER.error("Load failed");
    }
  }
}

1 个答案:

答案 0 :(得分:2)

因为加载程序会抛出Error,而您捕获了所有Exception

您可以抓住Error,但是通常不建议这样做。我怀疑是在这种情况下,您可能应该考虑其他解决方案。

相关问题