• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Dependency Injection in ServletContextListener

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using Spring - there's a post discussing Accessing a Spring Bean from ServletContextListener in the Spring forums.
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks

 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic