2016-04-28 33 views
0

我想從性能,讀取數據庫中值,所以出於同樣的原因,我在春天用@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(); 
    } 
    } 

我的項目結構如下,

enter image description here

我收到以下錯誤,

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 

如何訪問我的屬性文件值,我的代碼出了什麼問題?任何建議

+0

從您的'@PropertySource(「classpath:/application.properties」)中移除'/',嘗試它。 – Blank

+0

最初我只是試過。它不工作@Reno – Vinod

+0

進入發生異常的代碼,並逐步調試它爲文件路徑的實際值。 – Blank

回答

1

問題是與您的文件夾結構。 resource文件夾應該在main之下,而不是java。有關maven項目的默認結構,請參閱this。 請移動資源文件夾或將值更改爲classpath:/resources/application.properties