There is a Spring class that you can inject attributes into, and then a ServletContextListener can get dependencies using context.getAttribute method, but I cannot remember the class name. When I have a look at it, I'll tell you.
However, it's no DI, but I think it's good enough.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
This class name is org.springframework.web.context.support.ServletContextAttributeExporter.
You can inject collaborators into "attributes" property, and then you can use ServletContext.getAttribute in ServletContextListener, in this way you'll not have any dependency to Spring Framework code in your ServletContextListeners.
Vishwanath Murthi wrote:Hi kengkaj,
Yes. This works, but I'm wondering if DI would ever work in ServletContextListener...
From my understanding, DI should get triggered sometime. But is it even before the ServletContextListener is initialized?
Normally, Spring can only DI to its managed beans. ServletContextListeners objects are not created by Spring, so normally we cannot use DI.
There is a workaround by using AutowireCapableBeanFactory.autowireBeanProperties method, but the consequence is your code will have dependency to Spring.
For example, we can use DI in Servlet by using the following example codes in init method:
My problem is similar, i have an application that uses some config properties, but this properties should be loaded before any request is sent. It also affect my logging capabilities. As averyone, i can't use DI through EJB annotation.
I read some posts about this subject but always referring to Spring framework. I don't use it, and i think that importing all those libs just to workaround this issue is not a good solution. I use EJB3, if i don't use spring, what will be my options to solve it?
I've already thought about abandon JPA mechanism at this point and do a JDBC connection directly, complete my config mapper and the close the connection. The disadvantage is that i would have to keep two user/password to database access, one within the application (directly JDBC connection) and other in glassfish connection pool configuration (JPA connection).