java中的application.properties文件问题

时间:2016-04-28 03:04:51

标签: java spring spring-mvc properties

我正在尝试从属性中获取数据库值,所以出于同样的原因我在Spring中使用@PropertySource,但是它抛出了FileNotFoundException

@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

如何访问我的属性文件值,我的代码出了什么问题?任何建议

1 个答案:

答案 0 :(得分:2)

问题在于您的文件夹结构。 resource文件夹应位于main下,而不是java。查看this以获取maven项目的默认结构。 移动资源文件夹或将值更改为classpath:/resources/application.properties