如何在注入bean失败时使spring boot web app退出非零

时间:2017-04-27 10:45:05

标签: spring spring-boot spring-bean

我的应用程序中有一个bean(基于Spring Boot 1.5.3),由于加载了大量资源,需要时间进行初始化。虽然该bean的初始化已经开始在端口上侦听,但是即使初始化该bean失败然后抛出异常,Spring启动应用程序仍然在运行。

然而,虽然没有注入bean,但app仍在运行。

如果bean无法注入,可以使用错误代码退出spring web app。

以下是如何注射我的豆子的方式,

@Component
@Slf4j
@RefreshScope
@Profile("!integTest")
public class NoteSpellChecker {

private Directory spellIndexDirectory;
private SpellChecker spellChecker;

public NoteSpellChecker(@Value("${app.spellcheck.indexPath:build/suggest}") String spellIndexPath,
                        @Value("${app.spellcheck.accuracy:0.5f}") float spellAccuracy,
                        @Value("${app.spellcheck.suggestDirectory:${spring.cloud.config.uri}/${spring.application.name}" +
                                "/${spring.profiles.active}/${spring.cloud.config.label:master}/spellcheck/note.txt},")
                                String[] spellSuggestDirectories,
                        OkHttpClient httpClient) throws IOException {
**here run some initialization task that takes some time**
}

还有另一个bean取决于NoteSpellChecker。请参阅下面的代码段,

@Configuration
@Slf4j
public class NoteDBConfig {

@Bean
public List<NoteRecognizer> noteRecognizers(NoteSpellChecker noteSpellChecker,
                                            NoteDBRepository noteDBRepository) {
    return noteDBRepository.findByEnabled(true).sorted((n1, n2) -> n1.getOrder() - n2.getOrder())
            .map(note -> new NoteRecognizerImpl(note, noteSpellChecker))
            .collect(Collectors.toList());
}
}

由于bean的初始化错误,Spring启动应用程序无法启动,但JVM仍在运行而没有退出。所以守护进程应用程序(启动春季启动应用程序)并不知道我的应用程序的致命异常。

以下是控制台中的输出,

  

2017-04-28 17:36:54.483错误[magiceye-server ,,,] 32444 --- [
  main] o.s.boot.SpringApplication:应用程序启动   失败

     

org.springframework.beans.factory.BeanCreationException:错误   创建名为&scansTarget.noteSpellChecker&#39;的bean定义于   文件   [/.../service/impl/NoteSpellChecker.class]:   通过构造函数的Bean实例化失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[service.impl.NoteSpellChecker]:   构造函数抛出异常;嵌套异常是java.io.IOException     在   org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 2.getObject(AbstractBeanFactory.java:345)     在   org.springframework.cloud.context.scope.GenericScope $ BeanLifecycleWrapper.getBean(GenericScope.java:359)     在   org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:176)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     在   org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)     在   org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:121)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:253)     在   org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:174)     在   org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:137)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)     在   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)     在   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)     在   org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)     在   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)     在   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)     在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)     在   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:314)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)     在   BackendApplication.main(BackendApplication.java:21)   引起:org.springframework.beans.BeanInstantiationException:   无法实例化   [service.impl.NoteSpellChecker]:构造函数抛出   例外;嵌套异常是java.io.IOException at   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)     在   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)     在   org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271)     ...省略了32个常见帧引起的:java.io.IOException:null at   service.impl.NoteSpellChecker。(NoteSpellChecker.java:69)     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native   方法)at   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)     在   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)     ...省略了34个常见帧

1 个答案:

答案 0 :(得分:0)

我发现它是由第三方库启动的守护程序线程引起的。