Spring boot rabbitmq integration problems - spring

i am attempting to do this tutorial on springboot with rabbitmq integration.When i declare the receiver bean i cannot Autowire the AnnotationConfigApplicationContext bean making my whole receiver class unusable as i declare it as a bean in the main method class.
The whole tutorial is in the link below.
https://spring.io/guides/gs/messaging-rabbitmq/
Thanks..

Related

Spring Boot + Spring Integration Java DSL + AOP : Fails to proxy the Gateway interface

Hi I have a spring boot application, which starts a spring integration flow, through a gateway interface, using Java DSL. Everything works fine on its own. I added AOP to capture exceptions, with #EnableAspectJAutoProxy(proxyTargetClass = true)
At this stage, it gives the error:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'jobInitiator': Post-processing of
FactoryBean's singleton object failed; nested exception is
org.springframework.aop.framework.AopConfigException: Could not
generate CGLIB subclass of class [class com.sun.proxy.$Proxy54]:
Common causes of this problem include using a final class or a
non-visible class; nested exception is
java.lang.IllegalArgumentException: Cannot subclass final class class
com.sun.proxy.$Proxy54
When I remove the proxyTargetClass = true, it works but the advices are not triggered.
Any help? Is there a way to start the spring integration flow without a gateway?
There is no class associated with the gateway Proxy so you can't advise it.
Is there a way to start the spring integration flow without a gateway?
Instead of using the gateway, declare a bean of type MessagingTemplate and use template.sendAndReceive(someMessage) or template.convertSendAndReceive(somePojo) instead. See here.
(The gateway uses a MessagingTemplate internally; the gateway unwraps a MessagingException and throws the cause, the template does not).
It also does not support an error channel.
To get closer to the gateway functionality, you can subclass MessagingGatewaySupport and invoke its sendAndReceive() method(s).

How do I inject declarative service into my spring bean

I have a third party lib which is exposing declarative service. But I have a spring bean class.
How do I inject this declarative service into my spring bean class?
You must declare this service also as a bean so it can be managed by spring.

How to inject a Spring bean in to Camel DefultProducer

I have written a camel component by extending DefaultComponent and also have the associative classes implemetation for endpoint, consumer, producer. My producer is extending the camel DefaultProducer and I want to inject a spring bean inside this class, so that whenever a route will be executed like
<route id="myRoute"><from uri="file://inbox"/><to uri="myComp://outbox"/>
I will be able to get the file from the file system and store it into database. For storing the file into the DB I have a service class instantiated by the spring container, but whenever I inject that bean into MyProducer we are getting null.
I recongnized the problem was not about Camel, it is related to the spring and I was injecting the bean in a wrong way. I resolved the problem by implementing the ApplicationContextAware interface into my helper class and storing the spring context as static variable and with the help of this helper class I am able to get spring bean inside MyProducer class. Thanks for spring ApplicationContextAware interface.

Does the test class create implementations to inject in spring dependency injection

In this example on spring dependency injection here
Whne the final test class is run, after this line:
MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory
.getBean("mySpringBeanWithDependency");
Which implementation of writer class will get injected? The test class still is responsbile for creating an actual implementation and injecting it before calling business methods on the Writer. Is it true?
In both the annotation-based example and the XML-based example, Spring will inject the NiceWriter bean into the MySpringBeanWithDependency bean.
For the annotation example, this is because the NiceWriter class is annotated with #Service (while the Writer class is not) and Spring will discover this via classpath scanning and autowire it into MySpringBeanWithDependency.
For the XML example, this is because the NiceWriter class is used to define the bean with id writer, which is referenced as the "writer" property of the bean with id mySpringBeanWithDependency.
In both cases, the MySpringBeanWithDependency bean is instantiated with dependencies injected via Spring, and is thus ready to use. It is not responsible for managing its IWriter dependency. This is why dependency injection (DI for short) usually goes hand-in-hand with inversion-of-control (IoC for short). Spring provides an IoC container that uses DI.

Spring bean initialized on web app starts up

This is probably due to my ignorance of the Spring framewok but i am building a JSF+Facelets+Spring web app, i have a bean that whose init method i want to get called at the time the application is started. My problem is getting the bean initialized. I can reference the bean on a page, and when I go to the page, the bean is initialized, and works as directed; what I would like instead is for the bean to be initialized when the application is started
What is the way to get a Spring bean initialized on web app starts up
Your question is more Spring-targeted than JSF-targeted. I know the solution for JSF, but I don't think that this suits a Spring bean. I googled a second and found this topic at the Spring forum which may be of use for you. It describes/links several different approaches: http://forum.springsource.org/archive/index.php/t-21982.html
All the code that you want to handle immediately after an webapp is initialized can be done in a class that implements ServletContextListener as
#WebListener
public class ApplicationListener implements ServletContextListener {...}
and you can create spring application context like
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = context;
and get the bean you are interested in and go on.

Resources