Spring:如何通过读取外部属性文件来使用@Value注释注入值?

时间:2012-06-29 18:42:34

标签: java spring annotations

我有以下情况
- 有一个MongoService类,它从文件

中读取主机,端口和数据库

xml配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:///storage/local.properties</value>
            </list>
        </property>
    </bean>
</beans>  

local.properties 看起来像

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

MongoService类

@Service
public class MongoService {

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

    public MongoService(@Nonnull final @Value("#{ systemProperties['host']}") String host, @Nonnull final @Value("#{ systemProperties['port']}") int port, @Nonnull final @Value("#{ systemProperties['database']}") String db) throws UnknownHostException {
        LOGGER.info("host=" + host + ", port=" + port + ", database=" + db);
        mongo = new Mongo(host, port);
        database = db;
    }
}

当我想测试豆子没问题的时候,我在下面做了以下几点 MongoServiceTest.java

public class MongoServiceTest {

    @Autowired
    private MongoService mongoService;

}

抱怨说can not identify bean for MongoService

然后我将以下内容添加到上面的xml

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

然后抱怨说"No Matching Constructor found"

我想做什么

a。)MongoService应为@Autowired,并从<value>file:///storage/local.properties</value>

读取配置参数

问题

a。)访问构造函数中的值是否正确? (file name is local.properties and I am using @Value("#{ systemProperties['host']}") syntax)

b。)我需要做什么才能使@Autowired private MongoService mongoService正确加载并从local.properties文件中读取值。

P.S。我是Spring的新手,并不知道如何使这项工作

非常感谢您的帮助

2 个答案:

答案 0 :(得分:2)

我认为,您必须按如下方式将constructor-arg添加到config xml。

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

        <constructor-arg type="java.lang.String">
            <value>host</value>
        </constructor-arg>

        <constructor-arg type="int">
            <value>port</value>
        </constructor-arg>

        <constructor-arg type="java.lang.String">
            <value>database</value>
        </constructor-arg>

    </bean>

我不确定,Bst方式可能是添加基于java的bean配置。从xml中删除bean定义并添加基于java的congfig,如下所示

@Service
public class MongoService {

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

@bean
    public MongoService(@Nonnull final @Value("#{ systemProperties['host']}") String host, @Nonnull final @Value("#{ systemProperties['port']}") int port, @Nonnull final @Value("#{ systemProperties['database']}") String db) throws UnknownHostException {
        LOGGER.info("host=" + host + ", port=" + port + ", database=" + db);
        mongo = new Mongo(host, port);
        database = db;
    }
}

HTH

答案 1 :(得分:0)

默认情况下,Spring使用默认构造函数(没有args)实例化对象,然后使用setter方法设置所有属性。如果你不想(或不能)使用setter或定义默认的construtor,你必须告诉spring传递什么作为构造函数args。

这是一篇博客文章,解释了如何做到这一点:
http://www.javalobby.org/java/forums/t18396.html

基本语法如下:

<bean name="MyBean" class="com.example.BeanClass">
  <constructor-arg index="0"><ref bean="OtherBeanName"/></constructor-arg>
</bean>

index属性是可选的,但它要求您按照与构造函数的参数相同的顺序对XML constructor-arg元素进行排序。

相关问题