<util:properties not injected in java @controller on webapp deployed using eclipse WTP
I am developping a maven project using spring mvc. I have a problem with accessing properties () using @value. when i start tomcat 6, I get the following exception :
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]
Here is my 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) {
...
}
}
the content of 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>
the content of app.properties : page_default_size=10
EDIT:
I doscovered that the problem is related to eclipse wtp. When I deploy the webapp on tomcat using eclipse the problem occurs. However, when I deployed the war ganarated by maven on another tomcat server independently from eclipse, I don't get the problem and the application works fine. The question is how to fix this eclipse wtp bug?
I'd use <context:property-placeholder location=".." />
and then @Value("${property}")
"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.
精彩评论