简体   繁体   中英

Spring: how to dynamically create multiple beans from single template definition

I have the following Spring bean for a remote web service defined in xml:

    <bean id="authWSTemplate" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean" abstract="true">
       <property name="serviceInterface" value="com.example.webservices.Authentication" />
       <property name="wsdlDocumentUrl" value="${ws.root}/authentication?wsdl" />
       <property name="namespaceUri" value="http://security.webservices.example.com/" />
       <property name="serviceName" value="AuthenticationWebService" />
       <property name="portName" value="AuthenticationPort" />
       <property name="maintainSession" value="true" />
    </bean>

How do I obtain this bean template and create a concrete bean (ie supply the root property)? Can I then put the concrete bean into the Spring container?

I need numerous concrete beans pointing to different systems, so I have different root values. For this example, say there are 2 systems with roots: http://domain1.com:8001/ws and http://domain2.com:8002/ws .

Therefore I'd want 2 beans called "authWSdom1" and "authWSdom2".

I'm expecting to do this programmatically in an application initialisation block, where I'd retrieve a list of all known system implementations (this info is only known at runtime), and create a bean for each impl, cache the bean name, then my application will retrieve the appropriate bean from the Spring container when required.

Or, is there a better pattern for this? Perhaps by providing the root value in a constructor for the bean?

I'm thinking I cannot have a single bean in Spring as I need to support concurrent access across multiple end points (ie multiple users hitting domain1 and domain2 at the same time).

Create custom bean that implements BeanFactoryPostProcessor and InitializingBean. Use postProcessBeanFactory method to create bean:

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    String wsdlDocumentUrl = ....;
    // .......
    registry.registerBeanDefinition(YOUR_BEAN_NAME, BeanDefinitionBuilder.childBeanDefinition(
                getParentNoDomainServicBeanName(authWSTemplate)).addPropertyReference(
                "wsdlDocumentUrl", wsdlDocumentUrl).getBeanDefinition());

}

While I believe that Ragnor's answer is suitable if you want to dynamically create the bean in the spring container, I decided to use spring to define my own WSTemplate DTO then use a factory class to use this DTO and programmatically build (root url provided at runtime and DTO suffix value added to it) and cache the resulting JaxWS ProxyBean:

<bean id="authWSTemplate" class="com.example.WSProxyTemplate">
   <property name="serviceInterface" value="com.example.webservices.Authentication" />
   <property name="wsdlDocumentUrlSuffix" value="/authentication?wsdl" />
   <property name="namespaceUri" value="http://security.webservices.example.com/" />
   <property name="serviceName" value="AuthenticationWebService" />
   <property name="portName" value="AuthenticationPort" />
   <property name="maintainSession" value="true" />
</bean>

I like this approach as my spring config is abstracted away from the actual WS bean used. Ie if I wanted to use something other that JaxWS, then I'd simply write a different factory which used the same DTO beans. Again, this would help if I have to choose the WS implementation at runtime depending upon some system/env criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM