0
votes

I hope to find some help in converting Java class initiation to spring beans. I have not worked much with spring beans and getting a variety of errors. Java version works no problem.

Java version:

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); 

Spring bean:

   <spring:bean id="ssl_strategy" class="org.apache.http.conn.ssl.TrustSelfSignedStrategy" init-method="init"/>       
   <spring:bean id="ssl_builder" class="org.apache.http.conn.ssl.SSLContextBuilder" init-method="loadTrustMaterial(null, ssl_strategy)">

I'm getting an error that TrustSelfSignedStrategy has no method "init". The error :

 Couldn't find an init method named 'init' on bean with name 'ssl_strategy'

But once I remove init-method="init" from the first bean, then I get an error for .loadTrustMaterial method:

Couldn't find an init method named 'loadTrustMaterial(null, ssl_strategy)' on bean with name 'ssl_builder'

EDIT #1: I also have tried using org.springframework.beans.factory.config.MethodInvokingFactoryBean as follows and this is so far the closest I think:

<spring:bean id="ssl_strategy" class="org.apache.http.conn.ssl.TrustSelfSignedStrategy" /> 
<spring:bean id="ssl_builder" class="org.apache.http.conn.ssl.SSLContextBuilder" />

<spring:bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
   <spring:property name="targetObject" ref="ssl_builder"/>
   <spring:property name="targetMethod" value="loadTrustMaterial"/>
   <spring:property name="arguments">
    <spring:list>
        <spring:value>null</spring:value>
        <spring:value>ssl_strategy</spring:value>
    </spring:list>
    </spring:property>
</spring:bean>

But the above results in the following error:

 java.lang.NoSuchMethodException: org.apache.http.conn.ssl.SSLContextBuilder.loadTrustMaterial(java.lang.String, java.lang.String)   

Could someone please post here what would be the right way to have the above Java as Spring beans?

... much appreciate your time.

2
Dude, nobody uses Spring XML anymore. Use Java @Configurationclasses and let the IDE and compiler help you. - Sean Patrick Floyd

2 Answers

1
votes

The arguments you are passing are strings. IE the string "null" and the string "ssl_strategy".
Try the following which should pass an actual null and the reference to the ssl_strategy bean:

<spring:bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
   <spring:property name="targetObject" ref="ssl_builder"/>
   <spring:property name="targetMethod" value="loadTrustMaterial"/>
   <spring:property name="arguments">
    <spring:list>
        <spring:null />
        <spring:ref bean="ssl_strategy"/>
    </spring:list>
    </spring:property>
</spring:bean>
0
votes

First of all, no one uses Spring XML anymore, mostly because it's 2017, and somewhat because XML is not type-safe, and the IDE can't help you (to some extent IntelliJ IDEA Ultimate can, but you have to pay for it).

Second of all, I don't think a global SSL connection factory is a very good idea; with SSL, you want to keep it localized as much as possible and not have a singleton bean hang out for everyone to muck with.

That said, your Java code can simply be made into as bean, like this:

@Bean
SSLConnectionSocketFactory sslConnectionSocketFactory() {
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());

    return new SSLConnectionSocketFactory(builder.build());
}

Put the above in a @Configuration class and that's it!