2011-09-24 50 views
26

我有以下Bean聲明:春天PropertyPlaceholderConfigurer和內容:財產佔位

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>WEB-INF/classes/config/properties/database.properties</value> 
       <value>classpath:config/properties/database.properties</value> 
      </list> 
     </property> 
     <property name="ignoreResourceNotFound" value="true"/> 
    </bean> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
</bean> 

現在我想上面PropertyPlaceholderConfigurer更改爲以下格式:

<context:component-scan base-package="org.example.config"/> 
<util:properties id="jdbcProperties" 
      location="classpath:config/properties/database.properties"/> 
  1. ignoreResourceNotFound會忽略的財產,而運行。例如: 當測試應用程序WEB-INF/..路徑將忽略(因爲maven 項目和屬性文件在src/main/resources/..下),而 啓動web應用程序,其他屬性將忽略路徑,我需要 以上述格式實現。
  2. 應該可以添加多個屬性文件一樣 database.properties,test.properties等
  3. 在春季3,我可以使用註釋,而不是爲DB 加載這些XML文件,我該怎麼辦呢?因爲我只使用一個xml文件(上面給出的 )加載數據庫的東西。

我正在使用Spring 3框架。

回答

42

<context:property-placeholder ... />是與PropertyPlaceholderConfigurer等效的XML。所以,更喜歡那個。 <util:properties/>只是簡單地生成一個可以注入的java.util.Properties實例。

在Spring 3.1(不3.0 ...),你可以做這樣的事情:

@Configuration 
@PropertySource("/foo/bar/services.properties") 
public class ServiceConfiguration { 

    @Autowired Environment environment; 

    @Bean public javax.sql.DataSource dataSource(){ 
     String user = this.environment.getProperty("ds.user"); 
     ... 
    } 
} 

在Spring 3.0中,你可以 「訪問」 屬性使用使用SPEL註解PropertyPlaceHolderConfigurer機制定義:

@Value("${ds.user}") private String user; 

如果您想要一起刪除XML,只需使用Java配置手動註冊PropertyPlaceholderConfigurer即可。我更喜歡3.1方法。但是,如果您選擇使用Spring 3.0的方法(因爲3.1的GA不還......),你現在可以定義上面的XML是這樣的:

@Configuration 
public class MySpring3Configuration {  
     @Bean 
     public static PropertyPlaceholderConfigurer configurer() { 
      PropertyPlaceholderConfigurer ppc = ... 
      ppc.setLocations(...); 
      return ppc; 
     } 

     @Bean 
     public class DataSource dataSource(
       @Value("${ds.user}") String user, 
       @Value("${ds.pw}") String pw, 
       ...) { 
      DataSource ds = ... 
      ds.setUser(user); 
      ds.setPassword(pw);       
      ... 
      return ds; 
     } 
} 

注意,PPC定義使用static bean定義方法。這是確保bean被提前註冊所必需的,因爲PPC是一個BeanFactoryPostProcessor - 它可以影響bean自身在上下文中的註冊,因此它必須在其他所有之前註冊。

+1

但同樣你需要 @Bean 公共靜態PropertySourcePlaceholderConfigurer PSPC()在Spring 3.1例如 –

+1

這是最好的答案。一個簡單的說明:如果您使用Spring> = 3.1,請使用PropertySourcesPlaceholderConfigurer而不是舊的PropertyPlaceholderConfigurer。它支持Environment和Spring 3.1中引入的所有新東西。 –

18

首先,您不需要定義這兩個位置。只需使用classpath:config/properties/database.properties。在WAR中,WEB-INF/classes是一個類路徑條目,所以它可以正常工作。

之後,我認爲你的意思是你想要use Spring's schema-based configuration to create a configurer。這將是這樣的:

<context:property-placeholder location="classpath:config/properties/database.properties"/> 

請注意,您不需要「ignoreResourceNotFound」了。如果您需要定義分別使用util:properties屬性:

<context:property-placeholder properties-ref="jdbcProperties" ignore-resource-not-found="true"/> 

有通常不是任何理由單獨定義它們,雖然。

+0

1)如果我定義 TechFind

+1

'util:properties'只創建一個類型爲「java.util.Properties」的bean。它不會佔位符替換。最簡單的替換方法是在XML中使用'context:property-placeholder'。如果您願意,您可以創建並配置您自己的PropertyPlaceholderConfigurer實例(例如使用Java配置),但我不確定任何人都會認爲這是更好的方法。 –