Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I fail to use properties from application.properties file in Spring Boot?

Why I fail to use data from application.properties in the PostDao class?
Properties just not injected and DATABASE_URL and other variables are null.
PostDao.java is the class (src.main.java.site.model.PostDao) where I try to use properties.

@Component
public class PostDao
{
    @Value("${url}") public static String DATABASE_URL;
    @Value("${user}") public static String USER;
    @Value("${password}") public static String PASSWORD;
}

Here is my application.peoprerties file which stays in the resources directory

url=jdbc:postgresql://localhost:5432/news
user=postgres
password=123

Here is my config file (src.main.java.site.NewsSiteApplication)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@SpringBootApplication
public class NewsSiteApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(NewsSiteApplication.class, args);
    }
}

>Solution :

Alternatively, you can do the following, the setter method will handle for the injection:

@Component
public class PostDao
{
    public static String DATABASE_URL;


    @Value("${url}")
    public void setUrlStatic(String name){
        PostDao.DATABASE_URL = name;
    }

    // And others..
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading