ProblemSolver
ProblemSolver

Reputation: 161

How to use custom webclient in spring webflux integration using xml configuration

I am building webclient by adding different filters in my application. I want to use this custom webclient in spring webflux integration. I saw that WebFluxRequestExecutingMessageHandler accept custom webclient but i am not able to inject custom webclient bean through xml configuration.

https://github.com/spring-projects/spring-integration/blob/2217cb4f90a722fdfd5868cfc376809b7eb7da31/spring-integration-webflux/src/main/java/org/springframework/integration/webflux/outbound/WebFluxRequestExecutingMessageHandler.java#L103-L116

This is my gateway spring xml

<outbound-gateway id="reactiveFullConfig"
                  url="http://localhost:8080/hello"
                  http-method="GET"
                  request-channel="requests"
                  reply-timeout="1234"
                  extract-request-payload="false"
                  expected-response-type="java.lang.String"
                  mapped-request-headers="requestHeader1, requestHeader2"
                  mapped-response-headers="responseHeader"
                  reply-channel="replies"
                  charset="UTF-8"
                  order="77"
                  auto-startup="false"
                  transfer-cookies="true"
                  reply-payload-to-flux="true"
                  body-extractor="bodyExtractor"
                  publisher-element-type-expression="headers.elementType"
                  extract-response-body="false"
                  web-client="customWebClient">

    <uri-variable name="foo" expression="headers.bar"/>
</outbound-gateway>

<beans:bean id="customWebClient" class="com.abc.mypackage.CreateWebClient"
   init-method="createWebClient"/>

This is my CreateWebClient.java

public class CreateWebClient {

    @Autowired
    MyCustomBuilderFactory builderFactory;

    public WebClient createWebClient() {

        WebClient webClient =  builderFactory.getbuilder("someconfiguration").build();
        return webClient;
    }
}

Note: I know that CreateWebClient.java doesnt implement WebClient interface which i dont want to do as i just want to handover the already prepared webclient object spring integration for use.

Upvotes: 0

Views: 685

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

The pattern you are trying to use is called factory method. This has nothing to do with an init, which is typically something like afterPropertiesSet().

Since your factory method is not static - some dependency is present. Then you need to create a bean for the CreateWebClient, and then call that factory method for WebClient:

<beans:bean id="createWebClient" class="com.abc.mypackage.CreateWebClient"/>

<beans:bean id="customWebClient" factory-bean="createWebClient"
                                 factory-method="createWebClient"/>

See XSD doc for the factory-method:

The name of a factory method to use to create this object. Use
constructor-arg elements to specify arguments to the factory method,
if it takes arguments. Autowiring does not apply to factory methods.

If the "class" attribute is present, the factory method will be a static
method on the class specified by the "class" attribute on this bean
definition. Often this will be the same class as that of the constructed
object - for example, when the factory method is used as an alternative
to a constructor. However, it may be on a different class. In that case,
the created object will *not* be of the class specified in the "class"
attribute. This is analogous to FactoryBean behavior.

If the "factory-bean" attribute is present, the "class" attribute is not
used, and the factory method will be an instance method on the object
returned from a getBean call with the specified bean name. The factory
bean may be defined as a singleton or a prototype.

The factory method can have any number of arguments. Autowiring is not
supported. Use indexed constructor-arg elements in conjunction with the
factory-method attribute.

Setter Injection can be used in conjunction with a factory method.
Method Injection cannot, as the factory method returns an instance,
which will be used when the container creates the bean.

Upvotes: 1

Related Questions