【问题标题】:Builder pattern with spring boot @component annotated class带有 Spring Boot @component 注释类的构建器模式
【发布时间】:2018-02-20 15:55:18
【问题描述】:

我是春天的新手。我正在尝试使用 org.springframework.beans.factory.annotation.Value 注释在 Spring Boot 项目中使用 application.properties 文件中的属性构建 URL。

ElasticConfiguration 类从属性文件中挑选属性。但是,在某些情况下端口和协议是可选的。

@Component
public class ElasticConfiguration {

    @Value("${elasticsearch.hostname}")
    String hostname;
    @Value("${elasticsearch.portnumber}")
    Integer portnumber;
    @Value("${elasticsearch.protocol}")
    String protocol;

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public Integer getPortnumber() {
        return portnumber;
    }

    public void setPortnumber(Integer portnumber) {
        this.portnumber = portnumber;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

}

为了解决这个问题,我使用了一个构建器类,它基于可用属性构建 URL 对象

public class URL {

    private final String _hostname;
    private final String _portnumber;
    private final String _protocol;

    private URL(URLBuilder builder){
        this._hostname = builder._hostname;
        this._portnumber = builder._portnumber;
        this._protocol = builder._protocol;
    }

    public String get_hostname() {
        return _hostname;
    }

    public String get_portnumber() {
        return _portnumber;
    }

    public String get_protocol() {
        return _protocol;
    }

    public static class URLBuilder {
        private final String _hostname;
        private String _portnumber;
        private String _protocol;

        public URLBuilder(String hostname){
            this._hostname = hostname;
        }

        public URLBuilder portNumber(String value) {
            this._portnumber = value;
            return this;
        }

        public URLBuilder protocol(String value) {
            this._protocol = value;
            return this;
        }

        public URL build() {
            return new URL(this);
        }
    }

    @Override
    public String toString() {
        return "URL [_hostname=" + _hostname + ", _portnumber=" + _portnumber + ", _protocol=" + _protocol + "]";
    }

}

我想在 Spring Boot @component 注释类中使用构建器方法。

  1. 这是在 Spring Boot 中正确的做法吗?
  2. spring boot 是否已经提供任何此类 API 来模拟构建器模式?
  3. 如何整合以上两个类来实现我想要的?

【问题讨论】:

  • 如果它是可选的,您可以提供默认值,甚至可以将 null 指定为默认值。 @Value("${elasticsearch.protocol:#{null}}")
  • 哇太棒了。谢谢。
  • 您可能希望查看 ConfigurationProperties,stackoverflow.com/questions/46055112/…,以将属性绑定到类。使用 @Value 是相当低级的,不是 Spring boot 的要求,它有更好的选择
  • 您不想使用构建器模式。更常见的是,你使用 spring baeldung.com/… 的依赖注入

标签: java spring spring-boot


【解决方案1】:

使用 lombok.jar 它会有 @builder 来解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 2021-02-09
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多