spring注入properties屬性配置


第一步:bean配置

<!-- 這個bean由spring提供,用來加載properties文件 -->
	<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="fileEncoding" value="utf-8" />
		<property name="locations">
		    <list>
				<value>classpath:config/param/jdbc.properties</value>
				<value>classpath:config/param/sysparams.properties</value>
			</list>
		</property>
	</bean>
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="properties" ref="configProperties" />
	</bean>

第二步:測試注入屬性

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/config/spring/applicationContext.xml")
public class TestProperties {

	@Value("#{configProperties['sys.postHRPF.appId']}")
	private String aa;

	@Value("#{configProperties['sys.postHRPF.appKey']}")
	private String bb;

	@Value("#{configProperties['gm.smol.url']}")
	private String cc;

	@Value("${sys.postHRPF.appId}")
	private String dd;

	@Value("${sys.postHRPF.appKey}")
	private String ee;

	@Value("${sys.postHRPF.appId}")
	private int ff;
	
	@Value("#{configProperties['sys.postHRPF.appId']}")
	private int gg;
	
	@Test
	public void testProperties(){
		System.out.println(aa);
		System.out.println(bb);
		System.out.println(cc);
		System.out.println(dd);
		System.out.println(ee);
		System.out.println(ff);
		System.out.println(gg);
		System.out.println("*************");
	}
	
	
	
}

代碼中使用的多種注入的寫法,實驗證明都是可以用滴!








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章