7

我有一MongoService堂课

public class MongoService {

    private final Mongo mongo;
    private final String database;
    private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

    public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }

    public void putDocument(@Nonnull final DBObject document) {
        LOGGER.info("inserting document - " + document.toString());
        mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
    }

    public void putDocuments(@Nonnull final List<DBObject> documents) {
        for (final DBObject document : documents) {
            putDocument(document);
        }
    }

}

我想将 的值从外部属性文件/storage/local.propertieshost, port, db注入构造函数

### === MongoDB interaction === ###
host=127.0.0.1
port=27017
database=contract  

我的 Spring wireup 文件如下所示
wireup.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <util:properties id="mongoProperties" location="file:///storage//local.properties" />

    <bean id="mongoService" class="com.business.persist.MongoService">
        // TODO
    </bean>
</beans>

问题

如何传递host, port, db来自local.properties文件的值并将其传递给以下构造函数?

public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }
4

4 回答 4

15

您不想使用 util:properties 标记导入属性文件,而是希望使用 context:property-placeholder 导入它。util 版本只是将文件作为 Properties 对象导入,而不是将属性值暴露给您的配置。因此,您的设置将类似于:

<context:property-placeholder location="file:///storage//local.properties"/>

然后,当您连接 MongoService 时,您可以在构造函数配置中使用属性名称,例如

<bean id="mongoService" class="com.business.persist.MongoService">
    <constructor-arg value="${host}"/>
    <constructor-arg value="${port}"/>
    <constructor-arg value="${database}"/>
</bean>

有关更多详细信息,请参阅spring 文档。在旁注中,我会考虑为每个属性提供一个更具描述性的名称,以避免与您的应用程序中可能定义的其他属性发生冲突。

于 2012-07-03T13:37:52.913 回答
12

迈克肖恩给出了完全充分的答案。这里有一个补充:一旦你PropertyPlaceHolderConfigurer的设置正确,考虑一下现在广泛使用@Value的将这些属性注入构造函数的注解:

public class MongoService {

  ..

  @Autowired
  public MongoService(@Value("${host}") final String host, @Value("${port}") final int port, @Value("${db}") @Nonnull final String db) throws UnknownHostException {
      mongo = new Mongo(host, port);
      database = db;
  }

..
}
于 2015-10-28T17:22:23.797 回答
5

定义一个属性占位符:

<context:property-placeholder location="classpath:path/to/your.properties"/>

现在使用属性:

<bean id="mongoService" class="com.business.persist.MongoService">
    <constructor-arg value="${property.foo}" />
    <constructor-arg value="${property.bar}" />
</bean>

请参阅:4.8.2.1 示例:PropertyPlaceholderConfigurer

于 2012-07-03T13:37:30.720 回答
4

要在构造函数中读取 Spring application.properties,可以使用以下示例:

// File: sample/Message.groovy

package sample
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*

@Component
class Message {

    final String text

    // Use @Autowired to get @Value to work.
    @Autowired
    Message(
        // Refer to configuration property
        // app.text to set value for
        // constructor argument text.
        @Value('${app.text}') final String text) {
        this.text = text
    }
}

资料来源:DZone

于 2018-07-25T09:31:23.937 回答