有没有办法通过Spring的applicationConfig XML中的javacode添加ActiveMQ组件?
我的主要目标是从外部属性文件中获取“brokerURL”。但属性文件不是标准的属性文件,它基于 XML,因此必须对其进行适当的解析并获取属性。
<!-- COMPONENT BEANS -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
</property>
</bean>
最佳答案
有几种方法。
一个是真正的程序化,如下所示:
org.apache.activemq.camel.component.ActiveMQComponent amq = new org.apache.activemq.camel.component.ActiveMQComponent();
amq.setConnectionFactory(new ActiveMQConnectionFactory(parseOddXml(brokerXMLConfigFile)));
camelContext.addComponent("activemq", amq);
假设您有一个 Camel 上下文感知 bean 来初始化您的组件。
否则,您也许可以从其他地方连接连接工厂并将其注入(inject) ActiveMQ 组件上的 XML 配置中。
也许是这样的
@Configuration
class MyAMQConfig{
public @Bean ActiveMQConnectionFactory createCF(){
String brokerURI = parseOddXml(brokerConfigFile); // or whatever logic here.
return new ActiveMQConnectionFactory(brokerURI);
}
}
然后 XML 中类似这样的内容:
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="connectionFactory" ref="activeMQConnectionFactory"/>
</bean>
或者任何其他方式,因为有多种方法可以连接 bean 并与 Camel 上下文交互。
关于java - 通过java代码向Spring 3添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215136/