問題:
SpringBoot專案中整合了阿里開源Druid資料來源,本地開發環境直接啟動SpringBoot應用(SpringApplication.run方法),啟動成功,由於正式應用是部署在伺服器已有的Tomcat環境中,經過改造將SpringBoot專案打成ROOT.war包後,部署到伺服器上,啟動應用程式時,報錯了,錯誤資訊如下所示:
15:13:34.616 repair [localhost-startStop-1] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.level]
15:13:34.607 repair [localhost-startStop-1] DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [logging.group] threw NamingException with message: Name [logging.group] is not bound in this Context. Unable to find [logging.group].. Returning null.
15:13:34.606 repair [localhost-startStop-1] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.group]
15:13:34.606 repair [localhost-startStop-1] DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.group] not found - trying original name [logging.group]. javax.naming.NameNotFoundException: Name [logging.group] is not bound in this Context. Unable to find [logging.group].
15:13:34.580 repair [background-preinit] DEBUG o.h.v.m.ResourceBundleMessageInterpolator - Loaded expression factory via original TCCL
15:13:34.599 repair [localhost-startStop-1] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.group]
15:13:34.578 repair [localhost-startStop-1] DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [logging.group] threw NamingException with message: Name [logging.group] is not bound in this Context. Unable to find [logging.group].. Returning null.
15:13:34.577 repair [localhost-startStop-1] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.group]
15:13:34.577 repair [localhost-startStop-1] DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.group] not found - trying original name [logging.group]. javax.naming.NameNotFoundException: Name [logging.group] is not bound in this Context. Unable to find [logging.group].
15:13:34.548 repair [localhost-startStop-1] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.group]
... 74 common frames omitted
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at cn.demo.repair.service.impl.VpServiceImpl.init(VpServiceImpl.java:42)
at java.util.Properties.load(Properties.java:341)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties$LineReader.readLine(Properties.java:434)
Caused by: java.lang.NullPointerException: null
... 61 common frames omitted
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1239)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$120/684584821.getObject(Unknown Source)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1737)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:419)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VpServiceImpl': Invocation of init method failed; nested exception is java.lang.NullPointerException
... 47 common frames omitted
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1239)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$120/684584821.getObject(Unknown Source)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalServiceImpl': Unsatisfied dependency expressed through field 'vpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VpServiceImpl': Invocation of init method failed; nested exception is java.lang.NullPointerException
... 33 common frames omitted
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1239)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$120/684584821.getObject(Unknown Source)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairServiceImpl': Unsatisfied dependency expressed through field 'approvalService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalServiceImpl': Unsatisfied dependency expressed through field 'vpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VpServiceImpl': Invocation of init method failed; nested exception is java.lang.NullPointerException
at java.lang.Thread.run(Thread.java:745)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5178)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:171)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:137)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:157)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$120/684584821.getObject(Unknown Source)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairController': Unsatisfied dependency expressed through field 'repairService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairServiceImpl': Unsatisfied dependency expressed through field 'approvalService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalServiceImpl': Unsatisfied dependency expressed through field 'vpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VpServiceImpl': Invocation of init method failed; nested exception is java.lang.NullPointerException
15:07:53.610 repair [localhost-startStop-1] ERROR o.s.boot.SpringApplication - Application run failed
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
15:07:53.586 repair [localhost-startStop-1] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
15:07:53.536 repair [localhost-startStop-1] INFO c.alibaba.druid.pool.DruidDataSource - {dataSource-1} closed
15:07:53.258 repair [localhost-startStop-1] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairController': Unsatisfied dependency expressed through field 'repairService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairServiceImpl': Unsatisfied dependency expressed through field 'approvalService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalServiceImpl': Unsatisfied dependency expressed through field 'vpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VpServiceImpl': Invocation of init method failed; nested exception is java.lang.NullPointerException
15:07:37.645 repair [localhost-startStop-1] INFO c.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
15:07:28.073 repair [localhost-startStop-1] INFO c.a.d.s.b.a.DruidDataSourceAutoConfigure - Init DruidDataSource
15:07:25.620 repair [localhost-startStop-1] INFO o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'webStatFilter' to urls: [/*]
15:07:25.619 repair [localhost-startStop-1] INFO o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
15:07:25.619 repair [localhost-startStop-1] INFO o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'formContentFilter' to: [/*]
15:07:25.618 repair [localhost-startStop-1] INFO o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
15:07:25.618 repair [localhost-startStop-1] INFO o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'errorPageFilter' to: [/*]
15:07:25.617 repair [localhost-startStop-1] INFO o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
15:07:25.570 repair [localhost-startStop-1] INFO o.s.b.w.s.ServletRegistrationBean - Servlet statViewServlet mapped to [/druid/*]
15:07:25.568 repair [localhost-startStop-1] INFO o.s.b.w.s.ServletRegistrationBean - Servlet dispatcherServlet mapped to [/]
15:07:23.151 repair [localhost-startStop-1] INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 16648 ms
15:07:22.043 repair [localhost-startStop-1] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$e7ee3861] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
15:07:17.690 repair [localhost-startStop-1] INFO o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 421ms. Found 0 repository interfaces.
15:07:17.185 repair [localhost-startStop-1] INFO o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
15:07:17.154 repair [localhost-startStop-1] INFO o.s.d.r.c.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode!
15:07:06.257 repair [localhost-startStop-1] INFO cn.demo.repair.RepairApplication - The following profiles are active: dev
15:07:06.256 repair [localhost-startStop-1] DEBUG cn.demo.repair.RepairApplication - Running with Spring Boot v2.1.0.RELEASE, Spring v5.1.2.RELEASE
15:07:06.235 repair [localhost-startStop-1] INFO cn.demo.repair.RepairApplication - Starting RepairApplication v0.0.1-SNAPSHOT on 10.40.20.119 with PID 102172 (/home/work/data/www/demo-repair/ROOT/WEB-INF/classes started by work in /home/work)
:: Spring Boot :: (v2.1.0.RELEASE)
=========|_|==============|___/=/_/_/_/
' |____| .__|_| |_|_| |_\__, | / / / /
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
. ____ _ __ _ _
複製程式碼
分析:
分析報錯資訊,看到DruidDataSource初始化後,出現WARN資訊,依賴生成bin的時候(最終生成VpServiceImpl的bin時)出現了空指標異常,如下異常資訊:
15:07:53.536 repair [localhost-startStop-1] INFO c.alibaba.druid.pool.DruidDataSource - {dataSource-1} closed
15:07:53.258 repair [localhost-startStop-1] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairController': Unsatisfied dependency expressed through field 'repairService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repairServiceImpl': Unsatisfied dependency expressed through field 'approvalService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalServiceImpl': Unsatisfied dependency expressed through field 'vpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VpServiceImpl': Invocation of init method failed; nested exception is java.lang.NullPointerException
15:07:37.645 repair [localhost-startStop-1] INFO c.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
複製程式碼
剛開始以為是SpringBoot整合DruidDataSource出了問題,各種查各種找,沒有解決。
在網上搜能引起SpringBoot啟動出現以下報錯的原因,然也沒有解決。
15:07:53.610 repair [localhost-startStop-1] ERROR o.s.boot.SpringApplication - Application run failed Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
異常意思是:應用程式執行啟動ApplicationContext失敗,要顯示條件報告,請在“debug”模式下重新執行應用程式。
複製程式碼
此時覺得可能是出發的方向錯了,所以再次回過頭來,仔細分析異常堆疊資訊,看到兩行:
at cn.demo.repair.service.impl.VpServiceImpl.init(VpServiceImpl.java:42)
at java.util.Properties.load(Properties.java:341)
複製程式碼
覺得應該是自己程式中:在VpServiceImpl類初始化方法init中,Properties物件load檔案時丟擲了空指標異常。有了明確方向,開始研究出現問題的程式碼。
較詳細的異常資訊摘錄:
15:13:34.548 repair [localhost-startStop-1] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.group]
... 74 common frames omitted
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at cn.demo.repair.service.impl.VpServiceImpl.init(VpServiceImpl.java:42)
at java.util.Properties.load(Properties.java:341)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties$LineReader.readLine(Properties.java:434)
Caused by: java.lang.NullPointerException: null
... 61 common frames omitted
複製程式碼
結果發現直接用SpringBoot自帶的SpringApplication.run方法啟動應用程式,VpServiceImpl類的init方法不報錯,而且應用程式啟動成功,跑業務也正常,但是改造打成ROOT.war包後,部署到伺服器的Tomcat上,就報錯了,應用程式也啟動失敗。
哎呀,腦仁疼,怎麼辦,怎麼辦,怎麼辦......
腦子裡一閃現,可能是打ROOT.war包出的問題,不對,不能再跑偏(因為之前版本部署ROOT.war沒問題),立即弄死這個想法。
正確的招:分析程式碼,新增詳細的日誌列印。
然後問題的根源逐漸浮出水面,直接看程式碼:
@Service
@Slf4j
public class VpServiceImpl implements VpService{
@Value("${vp.path}")
private String vpPath;
/**
* VP列表
*/
private Set<String> vpSet;
/**
* Properties檔案
*/
private Properties vpProperties;
/**
* 初始化VP列表
*/
@PostConstruct
private void init() {
vpProperties = new Properties();
try {
vpProperties.load(ClassLoader.getSystemResourceAsStream(vpPath));
} catch (IOException e) {
log.error("Failed to load the vp.properties file. vpPath = " + vpPath + ", IOException : " + e);
}
vpSet = vpProperties.stringPropertyNames();
}
@Override
public Set<String> getVpSet() {
return vpSet;
}
@Override
public String getVpLeader(String username) {
if (!vpSet.contains(username)) {
return null;
}
String vpLeader = vpProperties.getProperty(username);
return StringUtils.isEmpty(vpLeader) ? vpProperties.getProperty("vpLeader", "xxx") : vpLeader;
}
@Override
public void setVpSet(Set<String> vpSet) {
this.vpSet = vpSet;
}
@Override
public boolean addVp(String... vp) {
if (vpSet == null) {
vpSet = new HashSet<>();
}
return vpSet.addAll(Arrays.asList(vp));
}
@Override
public boolean addVp(List<String> vpList) {
if (vpSet == null) {
vpSet = new HashSet<>();
}
return vpSet.addAll(vpList);
}
}
複製程式碼
vp.path在application.yml檔案中的配置如下:
vp:
path: vp.properties
複製程式碼
vp.properties在SpringBoot工程的resources目錄下:
程式碼中Properties物件vpProperties轉載InputStream輸入流,而InputStream輸入流是通過獲取vp.properties檔案資源轉換而來的,這裡是有問題的,ClassLoader.getSystemResourceAsStream(vpPath)
在實際線上伺服器環境中找不到vp.properties檔案,所以報了空指標異常,導致線上應用程式啟動失敗。
解決方法:
程式碼修改:
@PostConstruct
private void init() {
vpProperties = new Properties();
try {
vpProperties.load(VpServiceImpl.class.getClassLoader().getResourceAsStream(vpPath));
} catch (Exception e) {
log.error("Failed to load the vp.properties file. vpPath = " + vpPath + ", Exception : " + e);
}
vpSet = vpProperties.stringPropertyNames();
}
複製程式碼
修改點:
- 將
vpProperties.load(ClassLoader.getSystemResourceAsStream(vpPath));
改為vpProperties.load(VpServiceImpl.class.getClassLoader().getResourceAsStream(vpPath));
- catch所有異常,不會導致應用程式啟動失敗。
總結:
程式報錯了,不要瞎考慮,亂認為,一定要仔細研究異常棧資訊,仔細研究異常棧資訊,仔細研究異常棧資訊(重要的事說三遍)。
知識擴充套件:
寫累了,隨後再擴充套件吧。