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)
@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..
}