简体   繁体   English

Spring Boot外部属性未加载

[英]Spring Boot external properties not loaded

I'm starting with Spring Boot, so am first going through the Spring Boot reference Guide 我从Spring Boot开始,所以我首先要阅读Spring Boot参考指南

I'm currently having a look at the Externalized Configuration 我目前正在查看外部化配置

My goal is to have a simple Component to read the value out of application.properties , which, if I'm not mistaken, should be loaded automatically, without any further configuration. 我的目标是要有一个简单的Component来从application.properties读取值,如果我没有记错的话,应该自动加载它,而无需进行任何进一步配置。

The example in the reference is as follows: 参考中的示例如下:

import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*

@Component
public class MyBean {

    @Value("${name}")
    private String name;

    // ...

}

with the following explanation: 有以下说明:

On your application classpath (eg inside your jar) you can have an application.properties that provides a sensible default property value for name. 在您的应用程序类路径上(例如,在jar内),您可以具有application.properties,它为名称提供了合理的默认属性值。 When running in a new environment, an application.properties can be provided outside of your jar that overrides the name; 在新环境中运行时,可以在jar外部提供application.properties,以覆盖名称; and for one-off testing, you can launch with a specific command line switch (eg java -jar app.jar --name="Spring"). 对于一次性测试,您可以使用特定的命令行开关启动(例如java -jar app.jar --name =“ Spring”)。

Seems simple enough. 似乎很简单。 So, I have created following Component: 因此,我创建了以下组件:

@Component
public class Admin {

    @Value("${name}")
    private String name;

    public String getName(){
        return "ReadName_" + name;
    }
}

And, in my recources folder, the following application.properties file (which, I verified, is copied into my .jar) 并且,在我的recources文件夹中,以下application.properties文件(我已验证将其复制到我的.jar中)

name=myName 名字=我的名字

So, if I follow the logic in the reference (which is also mentioned in this post: Read application.properties , and run my application. 因此,如果我遵循参考中的逻辑(本文中也提到了该逻辑,请阅读application.properties并运行我的应用程序。

The code compiles and runs without error, but each time I print the value of getName() from an instance of Admin on my screen, it just gives a "ReadName_null" instead of "ReadName_myName", the value in the properties file. 该代码可以编译并运行,没有错误,但是每次我从屏幕上的Admin实例打印getName()的值时,它只会给出一个“ ReadName_null”而不是属性文件中的值“ ReadName_myName”。

As the reference states, in 24.2: 作为参考,在24.2中:

By default SpringApplication will convert any command line option arguments (starting with '--', eg --server.port=9000) to a property and add it to the Spring Environment. 默认情况下,SpringApplication会将所有命令行选项参数(以'-'开头,例如--server.port = 9000)转换为属性,并将其添加到Spring Environment中。 As mentioned above, command line properties always take precedence over other property sources. 如上所述,命令行属性始终优先于其他属性源。

So, just to check, I run my .jar file with both the application.properties with the key present, and add --name=passedName as a command line argument. 因此,仅检查一下,我同时运行了带有既有密钥的application.properties和我的.jar文件,并将--name=passedName添加为命令行参数。

Even though this value should be automatically added to the environment, and overrule anything (or in my case: nothing) that is currently there, and I expect the getName() to return "ReadName_passedName", it still returns "ReadName_null". 即使此值应自动添加到环境中,并且否决当前存在的任何内容(或者在我的情况下为:无),并且我希望getName()返回“ ReadName_passedName”,但它仍将返回“ ReadName_null”。

EDIT: I print the command line arguments I pass while booting, so I know that the argument is indeed read by the system as "--name=passedName" 编辑:我打印引导时传递的命令行参数,所以我知道该参数确实被系统读取为“ --name = passedName”

Is there anything obvious I'm missing here? 有什么明显的我想念的地方吗?

If you do new Admin () , you are creating a new instance of Admin and not a spring bean. 如果执行new Admin () ,则将创建Admin的新实例,而不是spring bean。 So you don't get any advantages which spring provides(for example Dependency Injection) And hence the value is null. 因此您没有得到spring提供的任何优势(例如Dependency Injection),因此该值为null。

Instead you should Autowire it in your class. 相反,您应该在课堂上对其进行自动布线。 This way spring will inject the instance of Admin that it has created (with injected values) 这样,spring将注入它创建的Admin实例(具有注入的值)

@Autowired
Admin admin;  

And before you ask, yes by default all beans are singleton(unless specified otherwise). 在您询问之前,默认情况下,是的,所有bean都是单例(除非另有说明)。 So no matter whereever you Autowire Admin, you will get the same instance. 因此,无论您在何处使用Autowire Admin,您都将获得相同的实例。

This is a broad topic, you should read about it. 这是一个广泛的主题,您应该阅读它。

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

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