我正在使用 spring mvc 开发一个 Maven 项目。我在使用@value 访问properties () 时遇到问题。当我启动 tomcat 6 时,出现以下异常:
09:21:21.703 ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resultsDisplayController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int foo.ResultsDisplayController.PAGE_SIZE; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) ~[spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) ~[spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) ~[spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) ~[spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830) [catalina.jar:na]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337) [catalina.jar:na]
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) [catalina.jar:na]
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) [catalina.jar:na]
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) [catalina.jar:na]
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) [catalina.jar:na]
at org.apache.catalina.core.StandardService.start(StandardService.java:516) [catalina.jar:na]
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) [catalina.jar:na]
at org.apache.catalina.startup.Catalina.start(Catalina.java:566) [catalina.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_21]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_21]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_21]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_21]
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) [bootstrap.jar:na]
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) [bootstrap.jar:na]
这是我的 foo.resultsDisplayController
@Controller
@Transactional(readOnly = true)
public class ResultsDisplayController {
private static final Logger LOG = LoggerFactory.getLogger(ResultsDisplayController.class);
@Value("#{appProperties.page_default_size}")
private int PAGE_SIZE;
@RequestMapping(value = "/show-results", method = RequestMethod.GET)
public String displayVariousreults(Model model) {
...
}
}
servlet-context.xml 的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:spring-configured />
<context:component-scan base-package="foo" />
<annotation-driven />
<tx:annotation-driven />
<!-- properties -->
<util:properties id="appProperties" location="classpath:app.properties" />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<interceptors>
<beans:bean
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="language" />
</beans:bean>
</interceptors>
<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basenames">
<beans:list>
<beans:value>classpath:labels</beans:value>
</beans:list>
</beans:property>
<beans:property name="cacheSeconds" value="5" />
<beans:property name="defaultEncoding" value="UTF-8" />
<beans:property name="fallbackToSystemLocale" value="false" />
</beans:bean>
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<beans:property name="order" value="0" />
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="json" value="application/json" />
<beans:entry key="xml" value="text/xml" />
</beans:map>
</beans:property>
<beans:property name="defaultContentType" value="application/json" />
</beans:bean>
<beans:bean id="authenticationListener"
class="foo.AuthenticationListener" />
</beans:beans>
app.properties 的内容: page_default_size=10
编辑:
我发现问题与 eclipse wtp 有关。当我使用 eclipse 在 tomcat 上部署 web 应用程序时,出现问题。然而,当我独立于 eclipse 在另一台 tomcat 服务器上部署由 maven 发起的战争时,我没有遇到问题并且应用程序工作正常。问题是如何修复这个 eclipse wtp bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
我正在使用 spring mvc 开发一个 Maven 项目。我在使用@value 访问properties () 时遇到问题。当我启动 tomcat 6 时,出现以下异常:
09:21:21.703 ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resultsDisplayController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int foo.ResultsDisplayController.PAGE_SIZE; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) ~[spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) ~[spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) ~[spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) ~[spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830) [catalina.jar:na]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337) [catalina.jar:na]
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) [catalina.jar:na]
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) [catalina.jar:na]
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) [catalina.jar:na]
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) [catalina.jar:na]
at org.apache.catalina.core.StandardService.start(StandardService.java:516) [catalina.jar:na]
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) [catalina.jar:na]
at org.apache.catalina.startup.Catalina.start(Catalina.java:566) [catalina.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_21]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_21]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_21]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_21]
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) [bootstrap.jar:na]
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) [bootstrap.jar:na]
这是我的 foo.resultsDisplayController
@Controller
@Transactional(readOnly = true)
public class ResultsDisplayController {
private static final Logger LOG = LoggerFactory.getLogger(ResultsDisplayController.class);
@Value("#{appProperties.page_default_size}")
private int PAGE_SIZE;
@RequestMapping(value = "/show-results", method = RequestMethod.GET)
public String displayVariousreults(Model model) {
...
}
}
servlet-context.xml 的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:spring-configured />
<context:component-scan base-package="foo" />
<annotation-driven />
<tx:annotation-driven />
<!-- properties -->
<util:properties id="appProperties" location="classpath:app.properties" />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<interceptors>
<beans:bean
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="language" />
</beans:bean>
</interceptors>
<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basenames">
<beans:list>
<beans:value>classpath:labels</beans:value>
</beans:list>
</beans:property>
<beans:property name="cacheSeconds" value="5" />
<beans:property name="defaultEncoding" value="UTF-8" />
<beans:property name="fallbackToSystemLocale" value="false" />
</beans:bean>
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<beans:property name="order" value="0" />
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="json" value="application/json" />
<beans:entry key="xml" value="text/xml" />
</beans:map>
</beans:property>
<beans:property name="defaultContentType" value="application/json" />
</beans:bean>
<beans:bean id="authenticationListener"
class="foo.AuthenticationListener" />
</beans:beans>
app.properties 的内容: page_default_size=10
编辑:
我发现问题与 eclipse wtp 有关。当我使用 eclipse 在 tomcat 上部署 web 应用程序时,出现问题。然而,当我独立于 eclipse 在另一台 tomcat 服务器上部署由 maven 发起的战争时,我没有遇到问题并且应用程序工作正常。问题是如何修复这个 eclipse wtp bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用
然后使用@Value("${property}")
I'd use
<context:property-placeholder location=".." />
and then@Value("${property}")
“NumberFormatException”我认为它将值读取为字符串。也许您可以将 PAGE_SIZE 的类型更改为 String,然后将其转换为 int。
"NumberFormatException" i think it reads the value as string.Maybe you can change the type of PAGE_SIZE to String,and then convert it to int.