Java 如何在没有XML的情况下使用Camel和Spring
我正在阅读Camel-in-Action第二版,它指示使用Spring-Camel-XML命名空间配置将Camel嵌入到Spring中,自动发现定义为springbean等的组件。下面是一个示例Java 如何在没有XML的情况下使用Camel和Spring,java,spring,apache-camel,Java,Spring,Apache Camel,我正在阅读Camel-in-Action第二版,它指示使用Spring-Camel-XML命名空间配置将Camel嵌入到Spring中,自动发现定义为springbean等的组件。下面是一个示例 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>org.apache.camel.example.spring</package>
</camelContext>
<!-- lets configure the default ActiveMQ broker URL -->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost?broker.persistent=false&broker.useJmx=true"/>
</bean>
</property>
</bean>
</beans>
org.apache.camel.example.spring
如果不使用XML配置,而是使用SpringJava配置,我将如何实现这一点?请参阅camel。
并参考Activemq配置
在此仅添加一个片段:
public class MyRouteConfiguration extends CamelConfiguration {
@Autowire
private MyRouteBuilder myRouteBuilder;
@Autowire
private MyAnotherRouteBuilder myAnotherRouteBuilder;
@Override
public List<RouteBuilder> routes() {
return Arrays.asList(myRouteBuilder, myAnotherRouteBuilder);
}
}
公共类MyRouteConfiguration扩展了配置{
@自动连线
私人MyRouteBuilder MyRouteBuilder;
@自动连线
私人MyAnotherRouteBuilder MyAnotherRouteBuilder;
@凌驾
公共列表路线(){
返回Arrays.asList(myRouteBuilder、myAnotherRouteBuilder);
}
}
中有一个
这是您所需要的最简单的示例。如果您想使用Java语法并让Camel发现您的Bean,那么您可以首先通过从方法返回Bean来定义Bean,并使用
@Bean
和@Configuration
注释。对于上面发布的XML示例,如下所示:
@Configuration
public class AppConfig {
@Bean
public JmsComponent jms() {
ActiveMQConnectionFactory amqcf = new ActiveMQConnectionFactory();
amqcf.setBrokerURL("vm://localhost?broker.persistent=false");
JmsComponent jms = new JmsComponent();
jms.setConnectionFactory(amqcf);
return jms;
}
}
您可以对路由使用类似的方法(使用@组件
注释):
然后将@ComponentScan
添加到您的主类中,该类可能如下所示(假设您使用的是纯Spring,而不是Spring引导):
该示例没有实现与XML配置相同的功能。我看到了camel-spring的全部要点,即让camel上下文自动发现bean,并根据spring应用程序上下文中存在的bean配置自己,即不需要通过camel-API注册组件等。在本例中,驼峰上下文仍在以手动方式进行配置。在servlet容器中运行SpringMVC时,我该如何进行配置?我应该在哪里创建Camel上下文,以便它能够发现springbean?我是否在一个Spring配置类中创建和配置Camel上下文,将其作为bean公开?
@Component
public class MyRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:foo?period=5000")
.setBody(simple("Customer"))
.to("jms:queue:customers");
}
}
@Configuration
@ComponentScan
public class Application extends CamelConfiguration {
public static void main(String[] args) throws Exception {
//org.apache.camel.spring.javaconfig.Main
Main main = new Main();
main.setConfigClass(Application.class);
main.run();
}
//...
}