繁体   English   中英

@Value 没有从 application.properties 获取值

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

我试图从 Spring Boot 应用程序中的 application.properties 获取值。

该类是使用标记@Component 定义的,我也已经尝试过@Service,并且使用@PropertySource("classpath:application.properties") 并且没有@PropertySource,但无论如何它们都会获得值。

   @Component
   @PropertySource("application.properties")
    public class TerraformOutput implements IPatternOutput {

    @Value(value = "${terraformPath}")
    private String pathTerraform;
}

接口是这样定义的

@Component
public interface IPatternOutput extends IOutput {
    
    String createFile(TerraformTemplate t);
}

和优越的界面

@Component
public interface IOutput {

    void deleteFile(String path);

}

无论如何,我尝试不实现接口,但无论如何都没有得到它

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/

提前致谢。

通常@PropertySource注释与配置类的@Configuration注释一起使用。 它适用于整个项目。 因此,将它与您的配置一起放置,然后在任何注释为@Component@Service @Controller等的类中,您可以完全按照您在代码中显示的方式使用@Value注释。 这是一篇关于这个问题的好文章: Properties with Spring and Spring Boot

我在您的共享代码中没有看到任何问题。 但是,您可以在某处覆盖terraformPath Spring Boot 考虑外部化配置文件的特定顺序。 它已经在文档中得到了很好的解释。

暂无
暂无

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

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