Spring PropertyPlaceholderConfigurer加载属性文件的问题

时间:2013-08-19 22:10:37

标签: java spring

我正在尝试使用外部属性文件来填充Spring Application Context配置文件中的一些bean。当我不使用$ {}属性并硬编码下面的jpaVendorAdapter类属性时,我可以看到该文件已加载到日志文件中。如果你依赖属性文件并使用class =“$ {jpa.vendor.adapter}”表示法,我得到了无法在类中定义名为'jpaVendorAdapter'的bean的类[$ {jpa.vendor.adapter}] path resource [spring / applicationContext.xml];例外。该文件在运行时肯定在类路径上,它看起来就像它在需要之后加载。我也尝试使用 <contextroperty-placeholder/> 方法,但得到了相同的异常。我正在使用Spring版本3.2.2.RELEASE,任何想法?

的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

异常

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

2 个答案:

答案 0 :(得分:1)

尝试用

替换PropertyPlaceholderConfigurer bean声明
<context:property-placeholder location="classpath:/runtime/project.properties"/>

以上内容不得与class属性一起使用。

你可以做的是实现一个产生你的类类型的FactoryBean

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

我相信Spring会自动在这个bean上调用getObject()并将返回的对象添加到上下文中。我相信example将是生成的bean(不是工厂)的名称。

答案 1 :(得分:1)

尝试使用Spring配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:project.properties</value>
        </list>
    </property>
</bean>