0

场景:在应用程序中,我有与语言相关的属性文件,它们用作模板来生成电子邮件:

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 上下文中,我想要这些包:

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

与我希望在上下文中声明的这些与语言无关的公共属性合并:

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

这怎么可能?

4

3 回答 3

1

据我所知,没有对此的支持。您正在尝试将配置与资源包混合。我觉得你现在拥有的是对的。如果您没有保持原样的奢侈,这里有一种方法(更多的黑客)

  1. 使用“ commonMailProperties org.springframework.context.MessageSource”(java.util.Properties)作为依赖项来实现,并将 bean id 称为“commonMessageSource”。

  2. In 'getMessage' implementations get the value from 'commonMailProperties'.

  3. Inject 'commonMessageSource' to 'subscriptionMailProperties' and 'cancellationMailProperties', for 'parentMessageSource' property.

于 2012-09-25T20:14:59.160 回答
0

If somebody got interested in complete solution:

  • Create class 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);
        }
    }
    
  • Use it:

    <bean id="commonMailProperties" class="org.company.PropertiesMessageSource">
        <property name="properties">
            <props>
                <prop key="email.from">noreply@company.org</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>
    
于 2012-09-26T14:33:47.610 回答
0

ResourceBundleMessageSource (more exactly: all descendants of AbstractMessageSource) now has commonMessages property which can hold locale-independent values. For example while you want to have mail subject and body locale-dependant, some properties (mail from and mail to) are common across all bundles (check 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">empty@mydomain.org</prop>
            <prop key="email.to">%s@mydomain.org</prop>
        </props>
    </property>
</bean>
于 2013-04-15T14:56:01.160 回答