aspose file tools*
The moose likes Other Application Frameworks and the fly likes Dependency Injection in ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Dependency Injection in ServletContextListener" Watch "Dependency Injection in ServletContextListener" New topic
Author

Dependency Injection in ServletContextListener

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi,

In my ServletContextListener I wanted to access the database. So I used a DAO reference and tried using DI. But it looks like DI isnt happening.
I get a NullPointerException on using the dao reference.

My doubt is "Is it possible to use DI in listeners? "

Is it something like only after the initialization of listeners that any DI can take place?

Thanks,
Vishwa


Blog
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you're using Spring - there's a post discussing Accessing a Spring Bean from ServletContextListener in the Spring forums.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
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
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
thanks

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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?
If it wouldn't get triggered before ServletContextListener, then maybe that explains the NullPointerException.

Thanks,
Vishwa
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Hi Vishwanath,
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:
Raul Pinheiro
Greenhorn

Joined: Sep 15, 2009
Posts: 2
Hi everyone!

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).

Any help is welcome!
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency Injection in ServletContextListener