package com.my.proper; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("configInfo") public class ConfigInfo { @Value("${pagesize}") private String pagesize; @Value("${aaa}") private String aaa; public String getAaa() { return aaa; } public void setAaa(String aaa) { this.aaa = aaa; } public String getPagesize() { return pagesize; } public void setPagesize(String pagesize) { this.pagesize = pagesize; } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <context:component-scan base-package="com.my.proper" /> <context:component-scan base-package="com.my.action" /> <!-- spring的属性加载器,加载properties文件中的属性 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:messages.properties</value> <value>classpath:messages2.properties</value> </list> </property> <property name="fileEncoding" value="utf-8" /> </bean> </beans>
public void propertyTest() { ConfigInfo config = (ConfigInfo) cx.getBean("configInfo"); System.out.println(config.getPagesize()); System.out.println(config.getAaa()); }