Jun 4th, 2007, 06:46 AM
Getting the Context from a Statefull bean.
I have come across a problem concerning getting beans from the Application Context which is either a limitation of Spring or just bad architecture on part.
Here is the scenario:
I have an application which, like most, gets beans out of the db, using Hibernate, and does something with them. My stateless beans, DAOs, package controllers, etc.. are all defined in Spring. My statefull beans come from the database and have no relationship with the application container.
The problem is that one of my beans, called Message, has an implementation of an interface, called the StatusResponder. A StatusResponder may be implemented by other packages and the Message has no concern what that implementation is, it just calls respond(). When a Message is persisted the class name of the implementing StatusResponder is written in the MESSAGE table. When a Message is loaded, the StatusResponderImpl is built dynamically. The StatusResponderImpl may have resources, such as DAOs, which should be loaded through the Spring Application Context. If I build the StatusResponderImpl dynamically then I must hard code the DAO instantiation because I cannot access the ApplicationContext from the bean.
I have tried implementing the ApplicationContextAware interface but that doesn't solve my problem as in order to set the ApplicationContext the bean must come from the context in the first place.
I guess that I should have some other way of referencing the StatusResponder implementaiton from my Message without having to persist the class name, does anyone have any idea how I should do this?
Jun 4th, 2007, 06:51 AM
take a look on @Configurable annotation in Spring reference manual. Likely, you need to use it together with AspectJ LTW.