簡體   English   中英

使用@Value 注解注入單個屬性

[英]Using @Value annotation to Inject single property

我有一個類,我只想從屬性文件中注入一個屬性。 類是這樣的:

@Getter
public class BatchContext {

    private final String city;
    private final String channel;

    @Value("${table.url: https://www.someurl.com}")
    private final String url;

    @Builder
    public BatchContext(String city, String channel, @Value("${table.url:https://www.someurl.com}") String url) {
        this.city = city;
        this.channel = channel;
        this.url = url;
    }
}

我只想將國家/地區和段傳遞給 BatchContext 並想從屬性文件加載 url 但結果 url 為空,這是實現這一目標的最佳方法。

麻杜,看來你的問題只和你的班級有關。 它不是 Spring 上下文的 Bean,那么你的 @Value 不能被 Spring 注入。

嘗試將您的類設置為 Spring 管理的對象,例如 @Component:

@Component
public class BatchContext {...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM