autowired spring(2)
在beans中增加
xmlns:context="http://www.springframework.org/schema/context"
在xsi中增加
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
则使用<context:annotation-config/>就可以使用autowired自动装配
加载资源配置
1 <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 2 <property name="location"> 3 <value>config/SqlMapConfig.properties</value> 4 </property> 5 </bean>
再在下面的配置中,需要使用SqlMapConfig.properties中的配置时,eg。 配置中有一个driver=com.mysql.jdbc.Driver,获取这个配置就是使用${driver}
导入其他的spring配置
1 <import resource="config/applicationDataSource.xml"/>
或者通过<context:property-placeholder>元素简单的注册
在beans中的配置和<context:annotation-config/>相同,
<context:property-placeholder location="config.properties" />
解析文本消息
废话少说,贴配置
1 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 2 <property name="basename"> 3 <value>messages</value> 4 </property> 5 </bean>
1 public static void main(String[] args) { 2 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); 3 String alert = context.getMessage("alert.checkout", new Object[]{4,new Date()}, Locale.US); 4 System.out.println(alert); 5 }
messages.properties
alert.checkout=A shopping cut costing {0} dollars has been checked at {1}.