2012-06-29 111 views
3

我有以下情況
- 有一個MongoService類,它從文件中讀取主機,端口,數據庫Spring:如何使用@Value註釋從外部屬性文件讀取注入值?

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

當我想測試bean是很好,我做的在
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"

我想做

一)MongoService應@Autowired什麼和讀取配置PARAMS來自<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.我很新的春天和真的不知道如何使這項工作

感謝很多的幫助提前

回答

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

+0

我不覺得@Bean – daydreamer

+0

http://static.springsource.org/spring-javaconfig/docs/1.0 .0.M4/reference/html/ch02s02.html – nayakam

+0

我收到錯誤,提示'@Bean無法應用於構造函數 – daydreamer

0

默認情況下,春季實例使用默認的構造函數(不帶參數),然後使用對象和setter方法來設置所有的特性。如果你不想(或不能)使用setter或定義一個默認的construtor,你必須告訴spring將構造器參數傳入什麼。

這裏有一個博客文章,解釋它是如何做到:
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> 

索引屬性是可選的,但它需要你訂購的XML constructor-arg元素的順序與構造函數的params相同。

相關問題