0

In order to provide POJOs in my legacy web app (Tomcat 8.0.26) with the ability to send ActiveMQ messages I've taken the recommendation to introduce Camel (2.15.2) / Spring (4.2.1) into the app to purely for the purpose of managing pooled MQ connections. I'm hoping there isn't an easier way.

Doing things the Spring way I'm thinking everything would need to be based around an MVC architecture with HTTP servlet aware controllers having access to the servlet context and therefore the Spring context in order to inject beanFactory beans into classes annotated with @Controller and @Service (and in fact there must be a Spring @Controller class that enables Spring to inject the @Service class.

However, as I've stated this is legacy code that will not be using the spring web framework.

After much pain it seems that the only way I can get beanFactory beans injected into my POJOs is to go the AspectJ and Weaving route. Before I go down this road can someone tell me that this is currently the best approach (what I've read describing this solution is from 2011 Can't get Spring to inject my dependencies - Spring Newbie) ? Can you point me to documentation and a working example?

Many thanks.

Jack
  • 29
  • 5
  • This is close enough (and I'm on mobile, and don't have dupehammer on the Spring tag) that I'm going to mark as a duplicate to my writeup. I suggest using the AspectJ approach; it requires minimal implementation effort for your use case. – chrylis -cautiouslyoptimistic- Sep 19 '15 at 21:10

1 Answers1

0

1) aspectj with @Configurable

In your @Configuration annotated class/es

you can add some more annotations @Configuration @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED) @EnableSpringConfigured @EnableAspectJAutoProxy

to enable aspectj and the @Configurable annotation, you need to import the aspectj lib to your project and add the spring tomcat instrumentable java agent in your tomcat lib folder (give a look here, it exaplains how to configure tomcat) http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/instrument/classloading/tomcat/TomcatInstrumentableClassLoader.html

this is not going to help you if you are going to create your pojos using "new" MyPojo p = new MyPojo(); // no black magic for this, you will need to satisfies the dependencies yourself) but that would be helpful for example when you load some entities through a framework like hibernate and you want to inject something into them.. @Configurable it's an option that can be evaluated in those cases, for what you describe I would rather not use it.

2) You can have some static methods that uses some static set spring-beans and use them from your pojos, something like

class Util{

private static SprintBeanWithJmsSupport x;

public static setSpringBeanToHandleJmsMessages(SprintBeanWithJmsSupport x){ Util.x = x; }

public static sendJmsMessage(JmsMessage m){ x.sendMessage(m) } } and you can go with Util.sendJmsMessage(...)

this is a bit shitty but it does the work, I don't personally like this approach

3) set your spring beans in your pojo when they need to use them (maybe behind some nice interfaces that suit your domain)

if you go with spring mvc you will likely end up having some controllers that will use some services (generally they handle security / db access and are the entry point to start the "use cases"), as everything wthin these layers is handled by spring it will be very simple to pass the spring-bean to handle jms messaging to your pojos, this seems to me quite a nice way to handle the problem

I went mostly based on memory and something may not be completely accurate, plus my english is .. what it is, so hope this can be helpful anyway.

Emanuele Ivaldi
  • 1,332
  • 10
  • 15