2012-09-25 110 views
0

情景:在應用我有作爲模板來生成電子郵件語言相關的屬性文件:如何在幾個ResourceBundles之間共享區域獨立屬性?

email-subscription_en.properties

email.subject=You are successfully subscribed to list {0} 
email.body=... 

email-cancellation_en.properties

email.subject=You are successfully unsubscribed from list {0} 
email.body=... 

等。現在,在Spring上下文我想有這些捆綁:

<util:properties id="commonMailProperties"> 
    <prop key="email.from">[email protected]</prop> 
    <prop key="email.to">{0}@company.org</prop> 
</util:properties> 

怎麼可能:

<bean id="subscriptionMailProperties" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="org.company.email-subscription" /> 
</bean> 

<bean id="cancellationMailProperties" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="org.company.email-cancellation" /> 
</bean> 

與我想在上下文中宣佈這些共同的語言無關性合併?

回答

1

據我所知,沒有這方面的支持。您正嘗試將配置與資源包混合使用。我感覺你現在擁有的是正確的。如果你沒有保持它的豪華,因爲它是,這裏是(多一個黑客攻擊)的方式

  1. 實現org.springframework.context.MessageSource與「commonMailProperties」(java.util.Properties)的依賴,並說這個bean ID作爲'commonMessageSource'。

  2. 'getMessage'實現從'commonMailProperties'中獲取值。

  3. 爲'parentMessageSource'屬性注入'commonMessageSource'爲'subscriptionMailProperties'和'cancellationMailProperties'。

0

如果有人有興趣完整的解決方案:

  • 創建PropertiesMessageSource類:

    /** 
    * {@link org.springframework.context.MessageSource} implementation that resolves messages via underlying 
    * {@link Properties}. 
    */ 
    public class PropertiesMessageSource extends AbstractMessageSource { 
    
        private Properties properties; 
    
        /** 
        * Set properties to use. 
        */ 
        public void setProperties(Properties properties) { 
         this.properties = properties; 
        } 
    
        @Override 
        protected MessageFormat resolveCode(String code, Locale locale) { 
         String property = properties.getProperty(code); 
    
         if (property == null) { 
          return null; 
         } 
    
         return createMessageFormat(property, locale); 
        } 
    } 
    
  • 使用它:

    <bean id="commonMailProperties" class="org.company.PropertiesMessageSource"> 
        <property name="properties"> 
         <props> 
          <prop key="email.from">[email protected]</prop> 
          <prop key="email.to">{0}@company.org</prop> 
         </props> 
        </property> 
    </bean> 
    <bean id="subscriptionMailProperties" class="org.springframework.context.support.ResourceBundleMessageSource"> 
        <property name="basename" value="org.company.email-subscription" /> 
        <property name="parentMessageSource"> 
         <ref bean="commonMailProperties"/> 
        </property> 
    </bean> 
    
0

ResourceBundleMessageSource(更確切地說:所有AbstractMessageSource的後代)現在有commonMessages屬性,它可以保存與區域無關的值。例如,當您想要郵件主題和正文區域設置相關時,某些屬性(郵件發送和郵件發送)在所有包中都是通用的(請檢查SPR-10291):

<bean id="mailProperties" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="org.mycompany.email" /> 
    <property name="commonMessages"> 
     <props> 
      <prop key="email.from">[email protected]</prop> 
      <prop key="email.to">%[email protected]</prop> 
     </props> 
    </property> 
</bean> 
相關問題