简体   繁体   English

带有 spring boot @component 注释类的构建器模式

[英]Builder pattern with spring boot @component annotated class

I am a newbie with spring.我是春天的新手。 I am trying to build a URL using the properties from application.properties file in a spring boot project using the org.springframework.beans.factory.annotation.Value annotation.我正在尝试使用org.springframework.beans.factory.annotation.Value注释在 Spring Boot 项目中使用 application.properties 文件中的属性构建一个 URL。

The class ElasticConfiguration picks the properties from the property file. ElasticConfiguration 类从属性文件中选取属性。 But, there can be scenarios where the port and protocol are optional.但是,可能存在端口和协议是可选的情况。

@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;
    }

}

To solve it i am using a builder class which builds the URL object basing on the available properties为了解决这个问题,我使用了一个构建器类,它根据可用属性构建 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 + "]";
    }

}

I would like to use a builder approach in the spring boot @component annotated class.我想在 spring boot @component注释类中使用构建器方法。

  1. Is this the right way to do in spring boot?这是春季靴子的正确做法吗?
  2. Is spring boot providing any such API's already to simulate the builder pattern? spring boot 是否已经提供任何此类 API 来模拟构建器模式?
  3. How to integrate above two classes to achieve what i want?如何集成以上两个类来实现我想要的?

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

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

相关问题 Spring 引导 - 如何避免在 Spring 引导中使用新的 class 注释为组件 - Spring boot - How to avoid using new in Spring boot class annotated as component Spring Boot - 如何通过名称获取用@Service 注释的类 - Spring Boot - how to get a class annotated with @Service by it's name 使用 Spring Boot 在侦听器中调用用 @RequestScope 注释的类 - Calling a class annotated with @RequestScope inside a Listener using Spring boot Spring Boot应用程序无法实例化一个类,除非使用Autowired对其进行了注释 - Spring Boot application cannot instantiate a class unless it is annotated with Autowired Spring-在Main中使用@Component注释类的正确方法 - Spring - proper way to use @Component annotated class in Main 将带有@Component 注释的类更改为带有@Bean 注释的方法 - Changing a class annotated @Component to @Bean annotated method 以编程方式注册@Component 注释类 - Registering @Component annotated class programmatically 春天找不到存储库注释的类 - Spring not finding Repository annotated class 在Spring Boot应用程序中,如何为每个自定义注释的类调用我的代码? - In Spring Boot application, how can my code be called for each custom-annotated class? 使用ConfigurationProperties注释的Spring Boot数据源,但未加载属性 - Spring Boot Datasource annotated with ConfigurationProperties but the properties are not loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM