java Spring - 捕获 bean 创建异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13508342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring - catch bean creation exception
提问by khachik
I want to catch bean instantiation exceptions in my code. What options do I have? One way to do this is to use Java-based container configuration:
我想在我的代码中捕获 bean 实例化异常。我有哪些选择?一种方法是使用基于 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 实例化定义异常处理程序?
回答by Derek Mahar
Method someBean
should catch SomeException
and then throw BeanCreationException
with SomeException
as the cause:
方法someBean
应该抓住SomeException
然后扔BeanCreationException
用SomeException
的原因:
@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);
}
}
}
回答by Jatin
You are not suppose to do that. That is the whole point of having Spring create a bean for you. If you are to create your own beans using new
(like above), why use Spring to create beans for you?
你不应该这样做。这就是让 Spring 为您创建 bean 的全部意义所在。如果您要使用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.
因此最好在初始化时失败,以便应用程序保持一致。虽然我不知道任何其他合法的做法。
回答by mjspier
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
}
}
}
回答by Sergey Shcherbakov
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.
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 创建期间失败并且 bean 对于应用程序的其余部分不是必需的(非常合法的情况),您可以按照您的建议通过在 @Bean 方法主体中捕获所有失败异常并返回null
以指示bean 创建失败。在这种情况下,bean 不会被添加到 Spring 应用程序上下文中,如果给定 bean 没有强制依赖关系,则上下文构造将成功。