【问题标题】:Invalid bean definition with name 'mongo' defined in null: Could not resolve placeholder 'mongo.port' in string value "${mongo.port}"在 null 中定义的名称为“mongo”的无效 bean 定义:无法解析字符串值“${mongo.port}”中的占位符“mongo.port”
【发布时间】:2014-07-17 11:48:49
【问题描述】:

我得到了异常

 Invalid bean definition with name 'mongo' defined in null: Could not resolve placeholder 'mongo.port' in string value "${mongo.port}"

当我尝试部署我的应用程序时

重点是我尝试使用属性占位符来替换属性文件中的值。你能指出缺少什么吗?

我的应用程序上下文是

<beans xmlns="http://www.springframework.org/schema/beans"  ...>

<util:properties id="propertiesFile" location="classpath:properties/base.properties"/>

<import resource="mongodb-config.xml"/>

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
    <map>
        <entry key="session">
            <bean class="org.springframework.context.support.SimpleThreadScope"/>
        </entry>
    </map>
</property>
</bean>

</beans>

我的 base.properties 是

 mongo.host=${profils.mongo.host}
 mongo.port=${profils.mongo.port}
 mongo.dbname=${profils.mongo.dbname}

mongodb-config.xml 是

<mongo:mongo host="${mongo.host}" port="${mongo.port}" />
<mongo:db-factory dbname="${mongo.dbname}" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
            <property name="writeConcern">
                <util:constant static-field="com.mongodb.WriteConcern.SAFE" ></util:constant>
            </property>
</bean>

此外,在 pom.xml 中有

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
    </plugins>
     <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>

        </resource>
    </resources>

    <filters>
        <filter>
            src/main/filters/filter-default.properties
        </filter>
    </filters>
     <finalName>base</finalName>
</build>

filter-default.properties 是:

profils.mongo.host=127.0.0.1
profils.mongo.port=27017
profils.mongo.dbname=base

编辑:

在目标文件夹中,

值被正确替换

mongo.host=127.0.0.1
mongo.port=27017
mongo.dbname=base

但他们似乎没有考虑到

安装了location="classpath:properties/base.properties",我试过用

 <util:properties id="propertiesFile" location="file:///D:/base/target/base/WEB-INF/classes/properties/base.properties"/>

 <context:property-placeholder location="file:///D:/base/target/base/WEB-INF/classes/properties/base.properties"/>

但这没有用

【问题讨论】:

  • 我不知道util:properties,但我会像这样使用&lt;context:property-placeholder location="classpath:foo.properties" /&gt;baeldung.com/2012/02/06/properties-with-spring/#xml。作为奖励,您还可以在遇到“找不到资源”时启用/禁用错误报告。
  • 我考虑了您的评论并更新了答案

标签: spring


【解决方案1】:

当我切换到 Java 配置时,我不再使用 XML 配置;但在一个带有 XML 配置的旧项目中,我使用的是类似的东西:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="myBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesMode" value="2" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations"><list>
      <value>classpath:foobar.properties</value>
    </list></property>
    <property name="properties"><props>
      <prop key="aProperty">value</prop>
    </props></property>
  </bean>
</beans>

它似乎有效,但它在它自己的 XML 文件中,而该文件又被包含在内。我不认为找不到您的变量的原因。

【讨论】:

  • 谢谢!我有两个属性文件,问题源于此,所以我使用了您的模板,并将 classpath:foobar.propertiesclasspath:foobar2.properties 放在列表中,然后它工作
猜你喜欢
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 2017-07-23
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多