配置文件注入(一)

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <list><!-- 配置文件名 -->
      <value>test</value>
      <value>test1</value>
    </list>
  </property>
</bean>


test.properties配置文件
message=Alligators rock\!

test1.properties配置文件
argument.required=The '{0}' argument is required.

类文件代码 (一)
MessageSource resources = new ClassPathXmlApplicationContext("applicationContext.xml");
String message = resources.getMessage("message", null, "Default", null);

输出: Alligators rock!

类文件代码 (二)
MessageSource resources = new ClassPathXmlApplicationContext("applicationContext.xml");
String message = resources.getMessage("argument", new Object [] {"userDao"}, "Required", Locale.UK);

输出: The userDao argument is required.

你可能感兴趣的:(配置文件)