简体   繁体   English

@Value 没有从 application.properties 获取值

[英]@Value not getting the value from application.properties

im trying to get a value from the application.properties in a Spring boot app.我试图从 Spring Boot 应用程序中的 application.properties 获取值。

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 定义的,我也已经尝试过@Service,并且使用@PropertySource("classpath:application.properties") 并且没有@PropertySource,但无论如何它们都会获得值。

   @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: application.properties 是这样定义的:

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.提前致谢。

Usually @PropertySource annotation goes together with @Configuration annotation for your configuration class.通常@PropertySource注释与配置类的@Configuration注释一起使用。 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.因此,将它与您的配置一起放置,然后在任何注释为@Component@Service @Controller等的类中,您可以完全按照您在代码中显示的方式使用@Value注释。 Here is a good article on the issue: Properties with Spring and Spring Boot这是一篇关于这个问题的好文章: Properties with Spring and Spring Boot

I do not see any issue in your shared code.我在您的共享代码中没有看到任何问题。 However, it is possible you override the terraformPath in somewhere.但是,您可以在某处覆盖terraformPath Spring Boot considers specific orders of externalized config files. Spring Boot 考虑外部化配置文件的特定顺序。 It is well explained in the documentation already.它已经在文档中得到了很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM