The Artima Developer Community
Sponsored Link

Java Buzz Forum
PicoContainer, AOP and containers you don't possess.

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Adam Kruszewski

Posts: 90
Nickname: phantomik
Registered: Jan, 2005

Adam Kruszewski is (mostly) Java developer and linux system administrator.
PicoContainer, AOP and containers you don't possess. Posted: Jan 1, 2005 7:34 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Adam Kruszewski.
Original Post: PicoContainer, AOP and containers you don't possess.
Feed Title: Adam Kruszewski :: WebLog();
Feed URL: http://adam.kruszewski.name/blojsom/blog/adam.kruszewski/?flavor=rss2
Feed Description: Thoughts about linux, open source, programming, ...
Latest Java Buzz Posts
Latest Java Buzz Posts by Adam Kruszewski
Latest Posts From Adam Kruszewski :: WebLog();

Advertisement

As far as I know there is no simple way to use PicoContainer (tiny IoC framework) in servlet container environment for filters, servlets and so on... (this is also true for any managed object in non-IoC aware container)

The problem is that we cannot control instantiation of such objects -- it is hard coded into servlet engine. But we can of course execute dependecy lookup code in contructor itself (loosing any IoC abilities :/), or we can use AspectJ (or any other AOP framework) to decouple dependency lookup code from constructor. going futher we can use setter injection on our already instantiated object.

It sounds simple and (probably) logical but PicoContainer desn't support injection in already instantiated object 'out of the box' (or at last I couldn't find it). I have achieved this by hacking around SetterInjectionComponentAdapter and creating my own InstanceSetterInjectionAdapter. Resolving dependencies is now as easy as creating one simple aspect:

  pointcut ReferenceServiceInitialization () : execution (public ReferenceService.new ());
 
  after
(): ReferenceServiceInitialization () {
   
MutablePicoContainer picoContainer = getPicoContainer ();
    PicoUtils.resolveDependencies
(picoContainer, thisJoinPoint.getThis ());
 
}

It will be more easier when AspectJ will have full support for J2SE5.0 features. Then it will be possible to write our classes as:

   @injectDeps (container="root") public class SomeFilter implements ServletFilter {
   
...
  
}

and leave rest to our little aspect ;)

An example is available as compressed eclipse project, just import it into your workspace and take a look. It doesn't depend on servlet/or-something container it is just uses this IoC hack to show this basic idea. (you must have AJDT installed because it is AspectJ project)

update:
  • 01-jan: colorized source code with java2html.

Read: PicoContainer, AOP and containers you don't possess.

Topic: New AppFuse Developer: Nathan Anderson Previous Topic   Next Topic Topic: [Dec 20, 2004 06:48 PST] 9 Links

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use