首先得有一个.properties文件,里面的内容如下
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/commentDemo?useUnicode=true&characterEncoding=UTF-8
第二,在spring配置文件中加载properties文件
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
第三,在java程序中,获取配置文件中的属性信息
@Controller
@RequestMapping("/value")
public class ValuePropertyController extends ApplicationController{
@Value("#{configProperties['jdbc.jdbcUrl']}")
private String jdbcUrl;
@RequestMapping
public String value(){
System.out.println(jdbcUrl);
return "";
}
}