im trying to get a value from the application.properties in a Spring boot app.
The class is defined with the tag @Component, I already tried @Service too, and with @PropertySource("classpath:application.properties") and without the @PropertySource but in any case they get the value.
@Component
@PropertySource("application.properties")
public class TerraformOutput implements IPatternOutput {
@Value(value = "${terraformPath}")
private String pathTerraform;
}
The interface is defined like this
@Component
public interface IPatternOutput extends IOutput {
String createFile(TerraformTemplate t);
}
And the superior interface
@Component
public interface IOutput {
void deleteFile(String path);
}
In any case I tried without implementing the interface but it didn´t get it in any case
The application.properties is defined in this way:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/dbcloudbatch?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
#spring.jpa.show-sql: true
terraformPath=C:/terraform-files/
Thanks in advance.
CodePudding user response:
Usually @PropertySource
annotation goes together with @Configuration
annotation for your configuration class. It works for entire project. So, place it with your configuration and then in any class that is annotated as @Component
, @Service
@Controller
etc you can use @Value
annotation exactly the way you showed in your code. Here is a good article on the issue: Properties with Spring and Spring Boot
CodePudding user response:
I do not see any issue in your shared code. However, it is possible you override the terraformPath in somewhere. Spring Boot considers specific orders of externalized config files. It is well explained in the documentation already.