Richard
Richard

Reputation: 6126

Converting Spring XML to JavaConfig

I want to convert the following spring beans from xml to a javaconfig class:

<bean id="restTemplate" class="org.springframework.security.oauth2.client.OAuth2RestTemplate">
    <constructor-arg ref="resource"/>
    <property name="messageConverters">
        <list>
            <ref bean="jaxbMessageConverter" />
            <ref bean="stringHttpMessageConverter" />
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

<bean id="resource" class="org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails">
    <property name="username" value="asdfasdf" />
    <property name="password" value="asdfasdfa" />
    <property name="clientId" value="asdfasdf-asdfas-asdfasf" />
    <property name="clientSecret" value="asdfasdf-asdfasdf-adfasdfd" /> 
    <property name="accessTokenUri" value="asdfsadfasd" />
    <property name="grantType" value="password"></property>
    <property name="clientAuthenticationScheme" value="form" />
</bean>

How exactly would you convert this? I came across this example but it's kind of confusing because it doesn't really make sense to me how the <constructor-arg...> and <property....> tags are represented in java. In the example they both translate to:

return new JButton(...);

Upvotes: 1

Views: 2656

Answers (1)

Aviad
Aviad

Reputation: 1549

In spring configuration file you need to do this:

@Bean
public ResourceOwnerPasswordResourceDetails resource() {
       ResourceOwnerPasswordResourceDetails  r = new ResourceOwnerPasswordResourceDetails ();
       r.setUsername("asdfasdf");
    ....

}
@Bean
public OAuth2RestTemplate restTemplate() {
      OAuth2RestTemplate rest = new OAuth2RestTemplate(resource());
      rest.setMessageConverters(...);
      List<HttpMessageConverter<?>> messageConv = new ArrayList<HttpMessageConverter<?>>();
      messageConv.add(new MappingJackson2HttpMessageConverter());
      rest.setMessageConverters(messageConv);
}

From the above example you can understand the rules:

  1. property can be done with setProperty..

  2. Constructor arg is really param in constructor

  3. A list is arraylist

  4. Depencdecies between beans are like in the example

    Suggestion: If you are trying to achieve something and you don't understand just google it and serach for the specific example.. This will be the easiest way and working by the rules will be easy for you

Upvotes: 2

Related Questions