@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
<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
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/commentDemo?useUnicode=true&characterEncoding=UTF-8
本文展示了一个使用Spring框架进行属性注入的例子。通过配置文件读取数据库连接字符串,并将其注入到控制器类中。此示例使用了PropertiesFactoryBean来加载外部配置文件,并利用PreferencesPlaceholderConfigurer进行属性占位符的解析。
2961

被折叠的 条评论
为什么被折叠?



