欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

记录一个项目交接时遇到的问题:‘tomcatEmbeddedServletContainerFactory’的bean无法创建

程序员文章站 2024-03-21 17:07:40
...

这个问题是我在交接一个使用springboot框架开发的maven项目时遇到的,具体发生时间是一切一切外部资源(仓库、jdk)配置妥当后运行时出现的。具体报错信息如下:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2020-11-12 15:33:26,565 INFO StartupInfoLogger.java:48 -Starting Application on LAPTOP-GL8C8PNL with PID 17068 (D:\1xiaozhusouworkspase\yhd-manage\target\classes started by jyk in D:\1xiaozhusouworkspase\yhd-manage)
2020-11-12 15:33:26,567 INFO SpringApplication.java:593 -No active profile set, falling back to default profiles: default
2020-11-12 15:33:26,611 INFO AbstractApplicationContext.java:583 -Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@45b9a632: startup date [Thu Nov 12 15:33:26 CST 2020]; root of context hierarchy
2020-11-12 15:33:26,874 INFO Version.java:30 -HV000001: Hibernate Validator 5.3.6.Final
2020-11-12 15:33:27,944 INFO RepositoryConfigurationDelegate.java:165 -Multiple Spring Data modules found, entering strict repository configuration mode!
2020-11-12 15:33:28,959 WARN AbstractApplicationContext.java:551 -Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jedisPoolConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
2020-11-12 15:33:28,969 INFO AutoConfigurationReportLoggingInitializer.java:101 -

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2020-11-12 15:33:28,977 ERROR SpringApplication.java:771 -Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container;
 nested exception is org.springframework.beans.factory.BeanCreationException: 
	Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: 
	 BeanPostProcessor before instantiation of bean failed;
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
	Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
	 Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
 nested exception is org.springframework.beans.factory.BeanCreationException:
	Error creating bean with name 'jedisPoolConfiguration':
	 Injection of autowired dependencies failed;
 nested exception is java.lang.IllegalArgumentException: 
	Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
			at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
			at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
			at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
			at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
			at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
			at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
			at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
			at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
			at com.chinalife.manage.Application.main(Application.java:9)
Caused by: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]:
	BeanPostProcessor before instantiation of bean failed;
  nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
	Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
		Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
  nested exception is org.springframework.beans.factory.BeanCreationException:
	Error creating bean with name 'jedisPoolConfiguration':
		Injection of autowired dependencies failed;
  nested exception is java.lang.IllegalArgumentException:
	Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
			at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:479)
			at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
			at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
			at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
			at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
			at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:199)
			at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162)
			at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
			... 8 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
  Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
   nested exception is org.springframework.beans.factory.BeanCreationException: 
 Error creating bean with name 'jedisPoolConfiguration': 
  Injection of autowired dependencies failed;
  nested exception is java.lang.IllegalArgumentException:
	Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
				at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:667)
				at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
				at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
				at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
				at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
				at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
				at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
				at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
				at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
				at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
				at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
				at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
				at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
				at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:102)
				at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:88)
				at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
				at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:248)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1037)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1011)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:473)
				... 15 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'jedisPoolConfiguration':
	Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: 
	 Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
				at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
				at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
				at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
				at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
				at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
				at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
				at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1180)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1096)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
				at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659)
				... 41 common frames omitted
Caused by: java.lang.IllegalArgumentException:
 Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
				at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
				at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
				at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
				at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
				at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
				at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086)
				at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
				at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
				at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
				at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
				... 55 common frames omitted

直接看上面这些报错信息真是相当让人头秃的,杂乱无章且无从入手(主要是个人太菜,不懂的太多),于是我将其中的有用信息摘取了一下,发现在以上报错信息中出现了这么几个关键信息:

1、这块儿显示该项目启动不了主要有有四个问题分别是:

‘tomcatEmbeddedServletContainerFactory’、‘org.springframework.cache.annotation.ProxyCachingConfiguration’’、‘jedisPoolConfiguration’这个三个bean无法创建,以及‘Could not resolve placeholder ‘spring.redis.host’ in value “${spring.redis.host}”’也就是‘${spring.redis.host}’这个值找不到.
这部分内容呢是一个整体性的提示,就是告诉你主要有哪几个错误,后面的报错信息是对每个出错点的具体描述和定位。

1.
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2020-11-12 15:33:28,977 ERROR SpringApplication.java:771 -Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container;
 nested exception is org.springframework.beans.factory.BeanCreationException: 
	Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: 
	 BeanPostProcessor before instantiation of bean failed;
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
	Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
	 Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
 nested exception is org.springframework.beans.factory.BeanCreationException:
	Error creating bean with name 'jedisPoolConfiguration':
	 Injection of autowired dependencies failed;
 nested exception is java.lang.IllegalArgumentException: 
	Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"

2、这里告诉我们还是和之前一样,三个bean无法被创建以及一个value找不到

2.Caused by: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]:
	BeanPostProcessor before instantiation of bean failed;
  nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
	Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
		Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
  nested exception is org.springframework.beans.factory.BeanCreationException:
	Error creating bean with name 'jedisPoolConfiguration':
		Injection of autowired dependencies failed;
  nested exception is java.lang.IllegalArgumentException:
	Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"
3、这里说两个bean无法被创建,一个参数无法赋值
3.Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
  Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
   nested exception is org.springframework.beans.factory.BeanCreationException: 
 Error creating bean with name 'jedisPoolConfiguration': 
  Injection of autowired dependencies failed;
  nested exception is java.lang.IllegalArgumentException:
	Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"

4、一个bean无法创建,一个参数无法赋值

4.Caused by: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'jedisPoolConfiguration':
	Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: 
	 Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"

5、参数无法赋值

5.Caused by: java.lang.IllegalArgumentException:
 Could not resolve placeholder 'spring.redis.host' in value "${spring.redis.host}"

有没有发现一个问题?
就是每个报错的详细描述中都会提及最开始的那条报错信息——‘${spring.redis.host}’的值找不到(怎么哪儿都有你捏?),我理解的是spring的报错提示应该是存放在一个类似于栈的存储结构中,最底部的那条报错信息大概率就是运行时出现问题的根本原因之一(如果项目中有多个问题,可能会后续也有提示,但这第一条记录肯定能够确认一个出错的原因)。所以大家以后在遇到这种不是很明朗的报错信息时,不妨先从最后的开始解决,因为很大可能后续的所有报错就是因为最初的某个东西出现了问题。

继续讨论我这个项目问题,我们看到上述好几个bean创建失败了,然后我就去搜啊,挨个去网上找,这个bean是干嘛的啊,什么情况下会创建失败啊,等等。这些东西都搜了一通,有用的信息没查到多少,源码倒是了解了一些,不过从这件事中我切实体会到了后端开发熟悉源码的重要性,希望小伙伴们引以为戒。
找了半天意识到最终还是只能看最后这个报错是什么原因造成的,肯定是这些bean在创建的时候需要用到这个value,而这个value一开始就没获取成功,后面的bean自然就都创建失败了,看了半天也没看出来配置文件有啥问题,事实证明确实不是配置文件的问题而是我这个项目配置文件不止一个,也就是我们指定了不止一个redis,
记录一个项目交接时遇到的问题:‘tomcatEmbeddedServletContainerFactory’的bean无法创建
而我在启动的时候又没选定使用哪套配置文件启动,系统自然找不到需要的host值,也就报错了。怎么解决呢?
如果你也遇到了同样的问题,先clean一下项目,找到入口程序右键,扎找到run as下的run configurations 选项记录一个项目交接时遇到的问题:‘tomcatEmbeddedServletContainerFactory’的bean无法创建
打开是这样的:
记录一个项目交接时遇到的问题:‘tomcatEmbeddedServletContainerFactory’的bean无法创建
我们需要在argument中进行配置,选定启动时使用哪个yml文件的配置
记录一个项目交接时遇到的问题:‘tomcatEmbeddedServletContainerFactory’的bean无法创建
也可以用这种方法进行配置,连接在这里有兴趣的可以看一下:
添加链接描述

这个问题呢是我问过一位这个项目原来在跟进的一位同事才明白的,有时候你绞尽脑汁的问题,可能旁人也就几秒钟的事儿,所以一定要多请教,但是也不能老是请教,先自己思考一下。。。就这样。

小结:在这个问题中收获到两点
1、报错信息一般最后一条才是关键,解决问题要先从最后一条入手
2、spring、springboot源码有其深入了解的重要性,日后抽时间一定要看