Access application context from code without servlet context
Hi
I'm designing a pretty straight-forward web application. As we'll have approx. 40-50 business classes and around 50 DAO implementation classes, I'd like to provide factory classes for these (DAOFactory). However, as soon as I move the bean creation into the code and away from the Spring context, I loose the references.
For the DAOFactory, I'd need a reference to the Hibernate sessionFactory. How do I get such a reference? I'm aware of WebApplicationContextUtils.getWebApplicationContex t, but I'd need a servlet context for that method. I don't have a servlet context in my DAOFactory, and I'd rather not couple the DAOFactory with some presentation layer class.
Currently, I've added an ApplicationContext class which implements BeanFactoryAware, stores the BeanFactory and and has a getter for the BeanFactory. But that's ugly.
An obvious but - imho - short-sighted solution would be to create a DAOFactory bean in the Spring context XML and pass a reference of the sessionFactory bean. With this, my problem would simply be shifted from
How does the DAOFactory get the sessionFactory instance
to
How does the business layer get the DAOFactory instance
Nothing solved, really.
Appreciate any help, links (or even a RTFM if a chapter is provided).
Simon
Access application context from code without servlet context
I feel like a moron, but here goes...
I have a problem similar to the question asked by sneiderb. I feel I've painted myself into a corner 'over-using' dependency injection.
I have two web applications. One is a Tapestry user interface program that creates XML requests and sends them to a remote servlet via Hessian. The remote back-end servlet uses Spring to inject dependencies. Being a Spring novice, I naively used singleton mode for all the spring injected beans. Needless to say, this can wreak havoc in the multi-threaded environment that is a web application/servlet.
Now I find that I need several of the back-end beans to be prototypes rather than singletons. The problem is, even if I change some of the beans to prototypes, they are injected into singleton's which are in-turn injected into singletons, etc. So my prototype beans will only ever be created once themselves!
My first thought was to extend the DispatcherServelet, grab the WebApplicationContext in the servlet, and create a wrapper around the context that other beans 'down stream' from the servlet can create new instances of beans using the 'getBean("beanId")' interface. This doesn't fee right, either.
The general architecture is this: The servlet is hooked to a bean whose interface is exported by a HessianExporter. All the spring beans are wired together in the configuration for this front-end exported bean.
The exported class contains a reference to a command factory. Commands are retrieved from the command factory and a service manager is used to obtain references to services that are called by the commands. Unfortunately, the command factory, commands, and services are all configured to be singletons.
Hopefully, I'm not a unique moron and you folks have seen this type of quandry before. Any advice on how to 'un-paint' myself from this corner would be appreciated.