1

I am trying to fetch the database values from properties, so for the same reason I am using @PropertySource in spring, but it is throwing the 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();
    }
    }

My project structure is as follows,

enter image description here

I am getting the following error,

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

How do I access my properties file values, what's wrong with my code? Any suggestions

3
  • Remove / from your @PropertySource("classpath:/application.properties"), try it. Commented Apr 28, 2016 at 3:12
  • Initially I tried that only. It is not working @Reno Commented Apr 28, 2016 at 3:17
  • Go into code which exception happened and debug it for real value of file path step by step. Commented Apr 28, 2016 at 3:21

1 Answer 1

2

The problem is with your folder structure. The resource folder should be under main, not java. Look at this for default structure of maven project. Either move the resource folder or change value to classpath:/resources/application.properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.