您的位置:首页 > 产品设计 > UI/UE

通过@Value注解读取.properties配置内容

2016-09-14 23:23 543 查看
首先得有一个.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 "";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: