Spring version 4.2.2
: What would be the Java Config replacement for the following:
Spring版本4.2.2:Java Config替代以下内容:
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/conf/mars.properties" />
</bean>
I have the following in one of the Config. files:
我在其中一个Config中有以下内容。文件:
@Configuration
@EnableTransactionManagement
@ComponentScan
@PropertySource("file:WEB-INF/conf/mars.properties")
public class JpaConfig {
private static final String DB_DRIVER = "a";
private static final String DB_URL = "b";
private static final String DB_USERNAME = "c";
private static final String DB_PASSWORD = "d";
private static final String DB_PLATFORM = "e";
@Resource
private Environment env;
@Bean(destroyMethod="close")
public DataSource dataSource() {
org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties p = new PoolProperties();
p.setUrl(env.getProperty(DB_URL));
p.setDriverClassName(env.getProperty(DB_DRIVER));
p.setUsername(env.getProperty(DB_USERNAME));
p.setPassword(env.getProperty(DB_PASSWORD));
....
}
This results in the following runtime error:
这会导致以下运行时错误:
Caused by: java.io.FileNotFoundException: \WEB-INF\conf\mars.properties (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
I tried the following variations but to no avail:
我尝试了以下变化,但无济于事:
@PropertySource("file://WEB-INF/conf/mars.properties")
This is an XML configured based application I'm converting to Java Config.
这是一个基于XML配置的应用程序,我正在转换为Java Config。
1 个解决方案
#1
3
All you need is
所有你需要的是
@PropertySource("/WEB-INF/conf/mars.properties")
Spring will use DefaultResourceLoader#getResource(String)
. For paths starting with /
, this delegates to getResourceByPath
which is overriden by AbstractRefreshableWebApplicationContext
(the superclass of AnnotationConfigWebApplicationContext
which handles @Configuration
).
Spring将使用DefaultResourceLoader#getResource(String)。对于以/开头的路径,此委托给getResourceByPath,后者由AbstractRefreshableWebApplicationContext(处理@Configuration的AnnotationConfigWebApplicationContext的超类)覆盖。
This will then create a ServletContextResource
which can successfully locate the resource relative to the servlet context.
然后,这将创建一个ServletContextResource,它可以成功地找到相对于servlet上下文的资源。
#1
3
All you need is
所有你需要的是
@PropertySource("/WEB-INF/conf/mars.properties")
Spring will use DefaultResourceLoader#getResource(String)
. For paths starting with /
, this delegates to getResourceByPath
which is overriden by AbstractRefreshableWebApplicationContext
(the superclass of AnnotationConfigWebApplicationContext
which handles @Configuration
).
Spring将使用DefaultResourceLoader#getResource(String)。对于以/开头的路径,此委托给getResourceByPath,后者由AbstractRefreshableWebApplicationContext(处理@Configuration的AnnotationConfigWebApplicationContext的超类)覆盖。
This will then create a ServletContextResource
which can successfully locate the resource relative to the servlet context.
然后,这将创建一个ServletContextResource,它可以成功地找到相对于servlet上下文的资源。