方案一:
applicationContext-dao.xml中添加
<!-- 加载配置文件 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:properties/*.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
Service、Controller层调用代码:
@Value("#{configProperties['jdbc.url']}")
private String url;
方案二:
applicationContext-dao.xml、springmvc.xml中添加
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
Service、Controller层调用代码:
@Value("${jdbc.url}")
private String url;
本文介绍了两种在Spring框架中加载配置文件的方法。一种是通过`PropertiesFactoryBean`和`PreferencesPlaceholderConfigurer`进行配置,另一种是使用`context:property-placeholder`标签来实现。这两种方法都能实现在Service和Controller层通过`@Value`注解读取配置文件中的属性。
8257

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



