Wetts's blog

Stay Hungry, Stay Foolish.

0%

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

1
2
3
4
5
6
7
8
9
10
11
12
13
@Controller
@RequestMapping("/value")
public class ValuePropertyController extends ApplicationController{

@Value("#{configProperties['jdbc.jdbcUrl']}")
private String jdbcUrl;

@RequestMapping
public String value(){
System.out.println(jdbcUrl);
return "";
}
}

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
<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>

database.properties

1
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/commentDemo?useUnicode=true&characterEncoding=UTF-8