繁体   English   中英

Spring:如何从单个模板定义动态创建多个bean

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

我有以下用于在xml中定义的远程Web服务的Spring bean:

    <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>

如何获得该bean模板并创建一个具体的bean(即提供root属性)? 然后可以将混凝土豆放入Spring容器中吗?

我需要大量指向不同系统的具体bean,因此我具有不同的根值。 对于此示例,假设有两个具有根目录的系统: http : //domain1.com : 8001/wshttp://domain2.com:8002/ws

因此,我想要2个名为“ authWSdom1”和“ authWSdom2”的bean。

我希望在应用程序初始化块中以编程方式进行此操作,在该程序块中,我将检索所有已知系统实现的列表(此信息仅在运行时才知道),并为每个impl创建一个bean,缓存bean名称,然后我的应用程序将在需要时从Spring容器中检索适当的bean。

还是为此有更好的模式? 也许通过在bean的构造函数中提供根值?

我想我在Spring中不能有一个bean,因为我需要支持跨多个端点的并发访问(即,多个用户同时访问domain1和domain2)。

创建实现BeanFactoryPostProcessor和InitializingBean的自定义bean。 使用postProcessBeanFactory方法创建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());

}

虽然我相信Ragnor的答案很适合在弹簧容器中动态创建bean的情况,但我还是决定使用spring来定义自己的WSTemplate DTO,然后使用工厂类来使用此DTO并以编程方式进行构建(运行时提供的根URL)并添加DTO后缀值)并缓存生成的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>

我喜欢这种方法,因为我的spring配置是从实际使用的WS bean中抽象出来的。 即,如果我想使用JaxWS之外的其他东西,那么我只是写了一个使用相同DTO bean的不同工厂。 同样,如果我必须根据某些系统/环境标准在运行时选择WS实现,这将有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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