在 Spring ApplicationContext 中相对于 applicationContext.xml 设置 PropertyPlaceholderConfigurer 的位置
set location of PropertyPlaceholderConfigurer in Spring ApplicationContext relative to applicationContext.xml
我想知道:是否可以在 spring applicationContext.xml 中为 PropertyPlaceholderConfigurer 设置一个 属性 文件相对于 xml- 的位置文件本身?
所以当 applicationContext.xml 和 dataSource.properties 文件在同一目录中时:
像这样的东西会工作吗,或者 Spring 找不到文件,因为我必须修改位置-属性 值?
<bean id="dataSourceProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="dataSource.properties" />
</bean>
提前致谢!
不,这行不通。
该位置的 "value" 不会以 applicationContext.xml 的位置作为 "root-directory" 开始。它将使用启动 spring 应用程序的目录。
也许其他人也会有这个问题:P
将你的dataSourceProperties.properties
文件放在resources
文件夹中,修改你的dataSourceProperties
bean如下:
<bean id="dataSourceProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:dataSource.properties</value>
</list>
</property>
</bean>
并在你的 class 定义中得到 属性 如下:
@WebService
@Component
public class MyService{
@Value("${databaseDriver}")
private String databaseDriver;
....
}
另外,得到完整的属性文件,如下:
@Component
public class MyService{
@Resource(name="dataSourceProperties")
private Properties dataSourceProperties;
....
}
dataSource.properties:
databaseDriver=oracle.jdbc.driver.OracleDriver
我想知道:是否可以在 spring applicationContext.xml 中为 PropertyPlaceholderConfigurer 设置一个 属性 文件相对于 xml- 的位置文件本身?
所以当 applicationContext.xml 和 dataSource.properties 文件在同一目录中时: 像这样的东西会工作吗,或者 Spring 找不到文件,因为我必须修改位置-属性 值?
<bean id="dataSourceProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="dataSource.properties" />
</bean>
提前致谢!
不,这行不通。
该位置的 "value" 不会以 applicationContext.xml 的位置作为 "root-directory" 开始。它将使用启动 spring 应用程序的目录。
也许其他人也会有这个问题:P
将你的dataSourceProperties.properties
文件放在resources
文件夹中,修改你的dataSourceProperties
bean如下:
<bean id="dataSourceProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:dataSource.properties</value>
</list>
</property>
</bean>
并在你的 class 定义中得到 属性 如下:
@WebService
@Component
public class MyService{
@Value("${databaseDriver}")
private String databaseDriver;
....
}
另外,得到完整的属性文件,如下:
@Component
public class MyService{
@Resource(name="dataSourceProperties")
private Properties dataSourceProperties;
....
}
dataSource.properties:
databaseDriver=oracle.jdbc.driver.OracleDriver