在激活配置文件时,从application.yaml中获取的spring.application.name属性值为null。

huangapple go评论27阅读模式
英文:

spring.application.name property in application.yaml is fetched as null if profile is active

问题

我有一个application.yaml文件,用来指定服务的名称:

spring:
  application:
    name: "my-microservice"

现在,当我尝试在代码中使用@Value("spring.application.name")来获取它时,我能够成功地做到这一点。

但是我还使用了一个名为"dev"的配置文件,为此我创建了一个单独的application-dev.yaml文件,但是在这个yaml文件中我没有指定spring.application.name。
现在,当我尝试运行我的代码时,@Value注解给我返回一个空值。
我原以为application-dev.yaml没有指定的字段应该会使用application.yaml中的值来填充,但显然这并没有发生。
我是不是应该只是从默认文件中复制每个通用字段到dev的应用程序文件中?还是还有其他办法?任何帮助都将不胜感激,谢谢 在激活配置文件时,从application.yaml中获取的spring.application.name属性值为null。

英文:

I have an application.yaml file which specifies the service's name:

spring:
  application:
    name: "my-microservice"

Now, when I try to fetch it using @Value("spring.application.name") inside my code, I'm able to do that successfully.

But I'm also using a "dev" profile for which I created a separate application-dev.yaml, but I didn't specify the spring.application.name inside this yaml.
Now, when I try to run my code, the @Value annotation gives me a null.
I thought that the fields not specified by application-dev.yaml should be populated using application.yaml, but apparently this is not happening.
Am I supposed to just copy every common field from the default to the dev's application file? Or is there any other way? Any help is appreciated, Thanks 在激活配置文件时,从application.yaml中获取的spring.application.name属性值为null。

答案1

得分: 3

  1. 你需要使用Spring表达式语言,应该这样写:
@Value("${spring.application.name}")
private String appName;
  1. 如果在yaml/yml或属性文件中找不到键的默认值:
@Value("${spring.application.name: defaultValue}")
private String appName;
  1. 最后一种方式是使用环境对象获取值:
@Autowired
private Environment environment;

String appName = environment.get("spring.application.name");
英文:
  1. You need to use Spring Expression Language which says we should write it as
    @Value("${spring.application.name}")
    private String appName;
  1. For Default value if key is not present in yaml/yml or properties file
    @Value("${spring.application.name: defaultValue}") 
    private String appName;
  1. The last way you can fetch value is using environment object
   @Autowired  
   private Environment environment;
       
   String appName = environment.get("spring.application.name");

答案2

得分: 0

我找到了问题。
默认的application.yaml属性确实由Spring读取所有配置文件(除非它们被某个配置文件的配置覆盖了)。
但在我的情况下,我尝试在静态字段上使用@Value注解。

@Value("${spring.application.name}")
private static String applicationName;

这导致了我的代码中的空指针异常。将字段中的静态上下文移除对我有用。

英文:

Ok I figured out the problem.
It is true that the default application.yaml properties are read by spring for all profiles (unless they have been overwritten by a profile's application file)
But in my case, I was trying to use the @Value annotation on a static field.

  @Value("${spring.application.name}")
  private static String applicationName;

This caused the null exception in my code. Removing the static context from the field worked for me.

huangapple
  • 本文由 发表于 2020年7月30日 13:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63166945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定