问题在Java application.properties文件
问题描述:
我想从性能,读取数据库中值,所以出于同样的原因,我在春天用@PropertySource,但它抛出FileNotFoundException异常问题在Java application.properties文件
@Configuration
@EnableJpaRepositories(basePackages = {
"com.manju.springdata.repository"
})
@EnableTransactionManagement
@EnableWebMvc
@ComponentScan(basePackages = "com.manju.springdata.*")
@PropertySource("classpath:/application.properties")
public class PersistenceContext {
@Value("${db.driver}")
private String dbDriver;
@Value("${db.url}")
private String dbURL;
@Value("${db.username}")
private String dbUserName;
@Value("${db.password}")
private String dbPassword;
@Bean(destroyMethod = "close")
DataSource dataSource(Environment env){
BoneCPDataSource dataSource = new BoneCPDataSource();
//dataSource.setDriverClass(env.getRequiredProperty("db.driver"));
dataSource.setDriverClass(dbDriver);
dataSource.setJdbcUrl(dbURL);
dataSource.setUsername(dbUserName);
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
我的项目结构如下,
我收到以下错误,
Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:153) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:84) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:360) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:254) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:231) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
... 57 common frames omitted
如何访问我的属性文件值,我的代码出了什么问题?任何建议
答
问题是与您的文件夹结构。 resource
文件夹应该在main
之下,而不是java
。有关maven项目的默认结构,请参阅this。 请移动资源文件夹或将值更改为classpath:/resources/application.properties
。
从您的'@PropertySource(“classpath:/application.properties”)中移除'/',尝试它。 – Blank
最初我只是试过。它不工作@Reno – Vinod
进入发生异常的代码,并逐步调试它为文件路径的实际值。 – Blank