发布于 2024-12-07 10:03:57 字数 8667 浏览 0 评论 0 原文

我正在使用 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛装女皇 2024-12-14 10:03:57

我会使用 然后使用 @Value("${property}")

I'd use <context:property-placeholder location=".." /> and then @Value("${property}")

亽野灬性zι浪 2024-12-14 10:03:57

“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文