开发者

propertyplaceholderconfigurer class name substitution problem

The following example works for "class name substitution using the PropertyPlaceHolderConfigurer":

http://forum.springsource.org/showpost.php?p=228136&postcount=2

However, when porting this code over (messages.properties, com.spring.ioc.TestClass, and spring-config.xml) to a web application, the class name substitution now fails.

I am running on the webapp on Tomcat through the Eclipse plugin. In the web.xml I have the following:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener&l开发者_运维百科t;/listener-class>
</listener>

The following is output in the log:

282  [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - 
Ignoring bean class loading failure for bean 'test'
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${test.class}] for bean with name 'test' defined in ServletContext resource [/WEB-INF/spring-config.xml]; nested exception is java.lang.ClassNotFoundException: ${test.class}
 at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1141)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:524)
 at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1177)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:222)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:505)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:362)
 at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3795)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
 at org.apache.catalina.core.StandardService.start(StandardService.java:448)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: java.lang.ClassNotFoundException: ${test.class}
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1438)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1284)
 at org.springframework.util.ClassUtils.forName(ClassUtils.java:211)
 at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:385)
 at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
 ... 23 more

I haven't included it, but the PropertyPlaceHolderConfigurer is successfully locating the messages.properties file, but this seems to happen after the above error is output.

Does anybody know why this is the case and how can I overcome this problem?


I have the same problem using Spring batch. The BeanFactory try to load the class before than the substitution occurs for the classname. I ended by not using substitution for classname because it is not a strong requirement for me. You may try to:

  • extend the Bean Factory in order to skip class with variable non substituted
  • extend the Bean Factory in order to load your place holder at the very first (since lazy-init and depends-on propertyPlaceOrder fail as well)


I haven't included it, but the PropertyPlaceHolderConfigurer is successfully locating the messages.properties file, but this seems to happen after the above error is output.

I strongly belive the PropertyPlaceHolderConfigurer is not working! (If it is not configured it can't.)

Add the configuer to your spring configuration.

<context:property-placeholder location="classpath:messages.properties"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜