Reputation: 73
I have a Spring Boot service, and I'm using IntelliJ to run it. I have a file call "application.properties
" in my resources
folder. I want intelliJ to read that .properties
file, how do I do that? the only way I get it to use the properties in .properties file is to add them directly to Environment VM Option.
I tried doing things like
-Dspring.config.location:/src/main/resources/application.properties
but that doesnt work.
Folder Structure:
Services
-src
-main
-resources
-application.properties
-target
-pom.xml
@Component
@Configuration
@EnableAutoConfiguration
@PropertySource({ "classpath:application.properties"})
@EntityScan(basePackages = "com.org")
public class AppConfig {
}
Upvotes: 0
Views: 5247
Reputation: 221
In case of Spring Boot, you don't have to pass any option when starting boot application.
When Spring boot application loads, it automatically checks if properties file exists in certain locations including src/main/resources
, src/main/resources/config
.
If you keep your properties file in these folders, the app automatically picks up the files and register properties. So in your AppConfig
you don't need @Component
, @EnableAutoConfiguration
, @PropertySource
, and @EntityScan
because @Configuration
already includes @Component
which enables @Value
to work.
I think the problem may arise when you call the property in the constructor of AppConfig
because when the class is being constructed the @Value
is not injected yet.
If you want to check if the property value is injected by Spring you can make a small test in the application class such as following
@SpringBootApplication
public class ApppropApplication {
@Autowired
private AppConfig appConfig;
public static void main(String[] args) {
SpringApplication.run(ApppropApplication.class, args);
}
@PostConstruct
public void init(){
System.out.println(appConfig.getTestProperty());
}
}
If your problem still exists, it would be great to provide more info (error logs and entire class structure)
Hope this helps! Happy Coding :)
Upvotes: 3
Reputation: 92
The ideal way for your springboot project to include the properties file would be to import using the annotation "@PropertySource" in your starter class. Please re check your starter class. It should include something like this below
@PropertySource({ "classpath:application-example.properties"})
@EntityScan(basePackages = "com.org")
public class YourProjectLauncher extends SpringBootServletInitializer {
@Value("${your.db.url}")
private transient String dataSourceUrl;
@Value("${your.db.username}")
private transient String userName;
@Value("${your.db.password}")
private transient String password;
@Value("${your.db.driver-class-name}")
private transient String driverClass;
public static void main(String... args) {
SpringApplication.run(YourProjectLauncher.class, args);
}
Let me know if you have already done this and still facing the issue.
Also it would be best if you add the starter class in your question, that way it is easier to analyse the problem you are facing.
Note - If you have already done this, please add more information to the question, will be happy to help.
Upvotes: 0