You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "Ziemer, Tom" <to...@wirecard.com> on 2014/10/10 14:38:21 UTC
Camel-Spring, JavaConfig and Constructor-Based Injection
Hi,
currently I am setting up a new project using Spring and Camel. For a change, I wanted to use JavaConfig instead of the XML and had little problem injecting a ProducerTemplate. I am aiming for ConstructorBasedInjection, and @EndpointInject did not work for me.
What I ended up doing is the following:
@Configuration
@PropertySource("classpath:foo.properties")
@EnableJpaRepositories(basePackages = "x.y.foo.repository")
@EnableTransactionManagement
@ComponentScan(basePackages = { "x.y.foo.service", "x.y.foo.camel" })
public class ApplicationContext extends CamelConfiguration {
....
@Bean(destroyMethod = "stop")
public ProducerTemplate producerTemplate() throws Exception{
return camelContext().createProducerTemplate();
}
...
}
This way I can use @Autowired to inject a producerTemplate:
@Service
public class FooServiceImpl implements FooService {
private final ProducerTemplate producerTemplate;
@Autowired
public QuoteServiceImpl(ProducerTemplate producerTemplate) {
Assert.notNull(producerTemplate, "producerTemplate must not be null.");
this.producerTemplate = producerTemplate;
}
...
}
Is this approach acceptable or is there a better way?
Regards,
Tom
Re: Camel-Spring, JavaConfig and Constructor-Based Injection
Posted by Willem Jiang <wi...@gmail.com>.
You just leverage the feature which is provided by spring.
I think it’s OK to do it that way.
--
Willem Jiang
Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem
On October 10, 2014 at 8:39:03 PM, Ziemer, Tom (tom.ziemer@wirecard.com) wrote:
> Hi,
>
> currently I am setting up a new project using Spring and Camel. For a change, I wanted to
> use JavaConfig instead of the XML and had little problem injecting a ProducerTemplate.
> I am aiming for ConstructorBasedInjection, and @EndpointInject did not work for me.
> What I ended up doing is the following:
>
> @Configuration
> @PropertySource("classpath:foo.properties")
> @EnableJpaRepositories(basePackages = "x.y.foo.repository")
> @EnableTransactionManagement
> @ComponentScan(basePackages = { "x.y.foo.service", "x.y.foo.camel" })
> public class ApplicationContext extends CamelConfiguration {
>
> ....
> @Bean(destroyMethod = "stop")
> public ProducerTemplate producerTemplate() throws Exception{
> return camelContext().createProducerTemplate();
> }
> ...
> }
>
> This way I can use @Autowired to inject a producerTemplate:
>
> @Service
> public class FooServiceImpl implements FooService {
> private final ProducerTemplate producerTemplate;
>
> @Autowired
> public QuoteServiceImpl(ProducerTemplate producerTemplate) {
> Assert.notNull(producerTemplate, "producerTemplate must not be null.");
> this.producerTemplate = producerTemplate;
> }
> ...
> }
>
> Is this approach acceptable or is there a better way?
>
> Regards,
> Tom
>