当前位置: 首页 > 知识库问答 >
问题:

Spring PropertyPlaceholderConfigurer加载属性文件时出现问题

丁成弘
2023-03-14
 <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>
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

共有1个答案

史俊德
2023-03-14

尝试将PropertyPlaceHolderConfigurerbean声明替换为

<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>
 类似资料:
  • 问题内容: 在上面指出的行中发现异常。无论语言是否是有效的属性文件,我都会在同一行上捕获相同的异常。 问题答案: 改成:

  • 我在尝试添加

  • 我有两个项目,CarpoolDB和拼车。 CarpoolDB:包含后端的东西 拼车应用程序上下文。xml server.properties 我做了一罐carpoolDB和地方拼车应用程序 拼车:包含UI东西和后端联系人carpoolDB jar,并具有 carpool-application-context1.xml spring-servlet.xml 拼车。性质 现在,我有一个类com.on

  • 我无法从main方法访问属性文件。 我在src/main/下添加了属性文件,如果我在Eclipse中运行main类,它可以工作,但是如果我使用这个命令,它在命令提示符下就不工作了 静态ResourceBundle rb=ResourceBundle.getBundle(“utils”);

  • 问题内容: 我在程序中需要一个配置文件来存储一些信息,我看到了一些属性文件的示例,并试图使用它们,但是每当在NetBeans上尝试第二行时,我都会得到“ Package sortConfig不存在”,“ >预期”和“类型非法开始”。 问题是我已经看到大约10个示例都以相同的方式执行此操作,而我不知道发生了什么。 任何帮助,将不胜感激 我的.java类和我的属性文件位于src的同一包文件夹中 问题答

  • 问题内容: 我在加载时遇到问题 在同一个文件夹中。 我在dist文件夹中有一个myProject.jar,在conf文件夹中有test.xml和test.properties。 要加载xml,我正在使用 但是我在加载属性文件时遇到问题 属性文件没有任何作用。 任何帮助表示赞赏。 问题答案: 为什么不取出文件并使用 上面的代码将获取属性文件并将其加载到属性对象中。