c12 c12 - 7 months ago 115
Java Question

Spring PropertyPlaceholderConfigurer Issue to Load A Properties File

I'm trying to use an external properties file to populate some of the beans in my Spring Application Context configuration file. When I don't use the ${} properties and hardcode the jpaVendorAdapter class property below, I can see the file is loaded in the log file. If u rely on the properties file and use the class="${jpa.vendor.adapter}" notation, I get the Cannot find class [${jpa.vendor.adapter}] for bean with name 'jpaVendorAdapter' defined in class path resource [spring/applicationContext.xml]; exception. The file is definetly on the classpath at runtime, it just seems like its loaded after its needed. I've also tried using the

<contextroperty-placeholder/>
approach, but got the same exception. I'm using Spring version 3.2.2.RELEASE, any ideas?

ApplicationContext.xml



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:runtime/project.properties</value>
</property>
</bean>
<bean id="jpaVendorAdapter" class="${jpa.vendor.adapter}">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
</bean>
</beans>


project.properties



jpa.vendor.adapter=org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter


Exception



org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${jpa.vendor.adapter}] for bean with name 'jpaVendorAdapter' defined in class path resource [spring/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: ${jpa.vendor.adapter}
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1269) ~[spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:578) ~[spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1338) ~[spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:356) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:327) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:435) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624) [spring-context-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461) [spring-context-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.jboss.resteasy.plugins.spring.SpringContextLoaderListener.contextInitialized(SpringContextLoaderListener.java:44) [resteasy-spring-2.3.5.Final.jar:na]
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:740) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:238) [jetty-servlet-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1238) [jetty-webapp-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:683) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:480) [jetty-webapp-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256) [jetty-maven-plugin-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.server.Server.doStart(Server.java:275) [jetty-server-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65) [jetty-maven-plugin-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:511) [jetty-maven-plugin-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:364) [jetty-maven-plugin-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:521) [jetty-maven-plugin-7.6.8.v20121106.jar:7.6.8.v20121106]
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) [maven-core-3.0.3.jar:3.0.3]
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) [maven-embedder-3.0.3.jar:3.0.3]
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) [maven-embedder-3.0.3.jar:3.0.3]
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) [maven-embedder-3.0.3.jar:3.0.3]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_51]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_51]
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) [plexus-classworlds-2.4.jar:na]
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) [plexus-classworlds-2.4.jar:na]
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) [plexus-classworlds-2.4.jar:na]
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) [plexus-classworlds-2.4.jar:na]
Caused by: java.lang.ClassNotFoundException: ${jpa.vendor.adapter}
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50) ~[na:na]
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244) ~[na:na]
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230) ~[na:na]
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:430) ~[na:na]
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:383) ~[na:na]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:260) ~[spring-core-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416) ~[spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1290) ~[spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1261) ~[spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
... 51 common frames omitted

Answer

Try replacing the PropertyPlaceholderConfigurer bean declaration with

<context:property-placeholder location="classpath:/runtime/project.properties"/>

The above must not work with class attribute.

What you can do instead is implement a FactoryBean that produces your class type

<bean id="example" class="com.mycom.factory.JpaVendorAdapterFactory">
   <property name="class" value="${jpa.vendor.adapter}"/>
   <property name="showSql" value="true" />
   <property name="generateDdl" value="true" />
   <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
</bean>

I believe Spring will automatically call getObject() on this bean and add the returned object to the context. I do believe example will be the name for the generated bean (not the factory).

Comments