Spring: Using builder pattern to create a bean

后端 未结 5 616
[愿得一人]
[愿得一人] 2020-12-08 00:54

I use ektorp to connect to CouchDB.

The way to build an ektorp HttpClient instance is to use builder pattern:

HttpClient httpClient = n         


        
5条回答
  •  醉话见心
    2020-12-08 01:04

    You may try to implement FactoryBean interface:

    public class HttpFactoryBean implements FactoryBean{
    
    private String host;
    private int port;
    
    
    public HttpClient getObject() throws Exception {
        return new StdHttpClient.Builder()
                                .host(host)
                                .port(port)
                                .build();
    }
    
    public Class getObjectType() {
        return StdHttpClient.class;
    }
    
    public boolean isSingleton() {
        return true;
    }
    
    public void setHost(String host) {
        this.host = host;
    }
    
    public void setPort(int port) {
        this.port = port;
    }}
    

    And add to config following bean definition:

     
       
           
           
       
    
    

    Then you can inject this bean to another beans, it will be resolved as StdHttpClient instance.

提交回复
热议问题