简体   繁体   English

Spring - 捕获bean创建异常

[英]Spring - catch bean creation exception

I want to catch bean instantiation exceptions in my code. 我想在我的代码中捕获bean实例化异常。 What options do I have? 我有什么选择? One way to do this is to use Java-based container configuration: 一种方法是使用基于Java的容器配置:

@Configuration
public class AppConfig {
  @Bean
  public SomeBean someBean() {
    try {
      return new SomeBean(); // throws SomeException
    } catch(SomeException se) {
      return new SomeBeanStub();
    }
  }
}

Is that possible to define exception handlers for bean instantiation using Spring using XML-based or annotation-based configuration? 是否可以使用基于XML或基于注释的配置使用Spring为bean实例化定义异常处理程序?

Method someBean should catch SomeException and then throw BeanCreationException with SomeException as the cause: 方法someBean应该抓住SomeException再扔BeanCreationExceptionSomeException的原因:

@Configuration
public class AppConfig {
  @Bean
  public SomeBean someBean() {
    try {
      return new SomeBean(); // throws SomeException
    } catch (SomeException se) {
      throw new BeanCreationException("someBean", "Failed to create a SomeBean", se);
    }
  }
}

You are not suppose to do that. 你不应该这样做。 That is the whole point of having Spring create a bean for you. 这就是让Spring为你创建一个bean的重点。 If you are to create your own beans using new (like above), why use Spring to create beans for you? 如果你要使用new创建自己的bean(如上所述),为什么要使用Spring为你创建bean?

You can indeed allocate object for your self and work along instead of dependency injection and all. 您确实可以为自己分配对象并继续工作,而不是依赖注入和所有。

Though I understand the essence behind the question. 虽然我理解这个问题背后的本质。 I think it is best if it fails during the server load time. 我认为最好是在服务器加载期间失败。 Reason: The application wont be in an inconsistent state. 原因:应用程序不会处于不一致状态。 Say suppose you catch the exception and do some cleanliness, but the other classes would be expecting for that bean to exist which it doesn't. 假设您捕获异常并执行一些清洁,但其他类将期望该bean存在而不存在。

Hence best it fails at initialization so that the application is consistent. 因此最好在初始化时失败,以便应用程序保持一致。 Though I do not know of any other legitimate way of doing. 虽然我不知道其他任何合法的做法。

Just for completeness. 只是为了完整。

You can also lazy init the bean and catch the exception the first time you use the bean. 你也可以懒惰初始化bean并在第一次使用bean时捕获异常。

spring config: 弹簧配置:

<bean id="a" class="A" lazy-init="true" />

In java: 在java中:

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

public class B {

    @Autowired
    @Lazy
    A a;

    public void foo(){
         try{
               a.foo();
         } catch (BeanCreationException e){
               // ignore if you want
         }
    }
}

In case if you expect failures during bean creation and the bean is not mandatory for the rest of the application (pretty legitimate situation) you can do as you have suggested by catching all failure exceptions in the @Bean method body and returning null to indicate that the bean creation has failed. 如果您期望在bean创建期间出现故障并且bean对于应用程序的其余部分(非常合理的情况)不是必需的,您可以按照建议通过捕获@Bean方法体中的所有失败异常并返回null来指示bean创建失败了。 The bean will not be added to the Spring application context in this case and the context construction will succeed in case if there are no mandatory dependencies on the given bean. 在这种情况下,bean不会被添加到Spring应用程序上下文中,如果给定bean没有强制依赖,则上下文构造将成功。

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

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