8

I would like to configure a HttpParams using spring setter injection but HttpParams has a two argument setter ( setParameter(String name, Object object) ). Is anyone aware of a way to configure this in spring?

The closest I can think of is like you would do a List, Set, or Property configuration:

http://www.mkyong.com/spring/spring-collections-list-set-map-and-properties-example/

Thanks!

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
craftsmanadam
  • 81
  • 1
  • 2

3 Answers3

11

Strictly speaking: A setter with two parameters is not a setter. It violates the JavaBeans convention, on which Spring builds. There is no simple way to solve that.


As an alternative, here's a Helper class you can use to configure your HttpParams object with Spring:

public class HttpParamSetter{

    private HttpParams httpParams;

    public void setHttpParams(HttpParams httpParams){
        this.httpParams = httpParams;
    }

    private Map<String, Object> parameters;

    public void setParameters(Map<String, Object> parameters){
        this.parameters = parameters;
    }

    @PostConstruct
    public void applyParameters(){
        for(Entry<String, Object> entry:parameters.entrySet()){
            httpParams.setParameter(entry.getKey(), entry.getValue());
        }

    }

}

Wire it like this:

<bean class="com.yourcompany.HttpParamSetter">
    <property name="httpParams" ref="httpParams" />
    <property name="parameters">
        <map>
            <entry key="foo" value="bar" />
            <entry key="baz" value="phleem" />
        </map>
    </property>
</bean>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    Thanks I thought there wasn't a way to do it purely using spring what we ended up doing is implementing a Spring FactoryBean that can supply the necessary connection factory then the config looks like: with httpParamFactory being an instance of our FactoryBean. – craftsmanadam Mar 15 '11 at 15:17
  • @craftsmanadam I was initially going to suggest that, but I didn't know if you created the Params object yourself, so I went for a solution that should always work (but yours is probably more spring-like) – Sean Patrick Floyd Mar 15 '11 at 17:04
3

I believe HttpConnectionParamBean and HttpProtocolParamBean were created for precisely this purpose. Documentation Example

tgoodhart
  • 3,111
  • 26
  • 37
1

Are you using Apache HttpClient? If so, the HttpClientParams implementation of HttpParams has real getters and setters that you can use.

Otherwise, I'd suggest writing a simple HttpParamsFactory that you could pass a map that contains the parameters you need and construct an appropriate instance of HttpParams.

GaryF
  • 23,950
  • 10
  • 60
  • 73