@Value没有从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);
}

在任何情况下,我尝试不实现接口,但它没有得到它在任何情况下

应用程序。属性是这样定义的:

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注释。这里有一篇关于这个问题的好文章:Spring和Spring Boot的属性

我在你的共享代码中没有看到任何问题。然而,您有可能在某处重写terraformPath。Spring Boot考虑外部化配置文件的特定顺序。在文档中已经有很好的解释了。

相关内容

最新更新