【bug】Error creating bean with name 'indexController': Injection of autowired dependencies failed; ne

目的:

实现涛涛商城首页大广告位的轮播图

过程:

将固定的数值写到properties配置文件中,使用SpringMVC配置文件加载properties文件,从而在Controller表现层使用数值。

@Controller
public class IndexController {
	
//	从配置文件中读取大广告位ID的变量
	@Value("${AD1_CATEGORY_ID}")
	private Long AD1_CATEGORY_ID;
	
//	从配置文件中读取 大广告位的图片的值大小
	@Value("${AD1_WIDTH}")
	private Integer AD1_WIDTH;
	@Value("${AD1_WIDTH_B}")
	private Integer AD1_WIDTH_B;
	@Value("${AD1_HEIGHT}")
	private Integer AD1_HEIGHT;
	@Value("${AD1_HEIGHT_B}")
	private Integer AD1_HEIGHT_B;
	
	@Autowired
	private ContentService contentService;
}

错误显示

1、Error creating bean with name 'indexController': Injection of autowired dependencies failed

2、Could not autowire field: private java.lang.Long com.taotao.portal.controller.IndexController.AD1_CATEGORY_ID; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'AD1_CATEGORY_ID' in string value "${AD1_CATEGORY_ID}"

3、Could not autowire field: private java.lang.Long com.taotao.portal.controller.IndexController.AD1_CATEGORY_ID;

解决方式:

查阅了网上的信息没有找到答案。最后自己才发现是以下原因造成的

在上图中加载文件地址之前加载顺序是相反的,改成上图中的顺序后,能够正常访问。

根本的原因为:

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,意思就是Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer 或 其余的会被Spring忽略由于Spring容器只能有一个PropertyPlaceholderConfigurer,如果有多个属性文件,这时就看谁先谁后了,先的保留 ,后的忽略。所以造成了以上的结果。

所有的报错信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Long com.taotao.portal.controller.IndexController.AD1_CATEGORY_ID; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'AD1_CATEGORY_ID' in string value "${AD1_CATEGORY_ID}"atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)

   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)

   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)

   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.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)

   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)

   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)

   at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)

   at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)

   at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)

   at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)

   at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)

   at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

   at javax.servlet.GenericServlet.init(GenericServlet.java:160)

   at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)

   at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)

   at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)

   at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)

   at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)

   at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

   at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

   at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

   at java.util.concurrent.FutureTask.run(FutureTask.java:166)

   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

   at java.lang.Thread.run(Thread.java:722)

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Long com.taotao.portal.controller.IndexController.AD1_CATEGORY_ID; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'AD1_CATEGORY_ID' in string value "${AD1_CATEGORY_ID}"

   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)

   at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)

   ... 30 more

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'AD1_CATEGORY_ID' in string value "${AD1_CATEGORY_ID}"

   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:204)

   at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)

   at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)

   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808)

   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)

   at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)

   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)

   ... 32 more

 

九月 06, 2018 10:13:04 下午 org.apache.catalina.core.StandardContext loadOnStartup

SEVERE: Servlet  threw load() exception

java.lang.IllegalArgumentException: Could not resolve placeholder 'AD1_CATEGORY_ID' in string value "${AD1_CATEGORY_ID}"

   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:204)

   at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)

   at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)

   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808)

   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)

   at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)

   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)

   at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)

   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)

   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)

   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)

   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.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)

   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)

   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)

   at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)

   at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)

   at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)

   at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)

   at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)

   at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

   at javax.servlet.GenericServlet.init(GenericServlet.java:160)

   at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)

   at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)

   at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)

   at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)

   at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)

   at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

   at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

   at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

   at java.util.concurrent.FutureTask.run(FutureTask.java:166)

   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

   at java.lang.Thread.run(Thread.java:722)

你可能感兴趣的:(BUG)