uima-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex <fingrpi...@gmail.com>
Subject Re: Dependency Injection support?
Date Tue, 12 Apr 2011 14:53:11 GMT
Marshall Schor <msa@...> writes:

> On 3/18/2011 4:09 PM, Alex Chaphiv wrote:
> > Hi,
> >
> > I have several annotators that require objects that are rather complex
> > to build.
> >  I've been able to get around this using by using ExternalResources
> > with custom api interfaces, but it requires a lot of boilerplate and
> > doesn't allow for sharing of dependencies between ExternalResources.
> 
> Could you share what an "ideal" solution might look like, along with a small,
> made-up sample of code that would use this ideal solution (with enough 
> comments for wide understanding  )?
> 
> Something that showed how the boilerplate is shrunk, and how dependencies are
> specified?
> 
> -Marshall Schor
> > So what would be the best way to get around this?  Even better, 
> > is there any support for Spring or Guice?
> >


I was having similar trouble sharing resources (mainly caches) among UIMA
annotators using Spring injection several months ago.  The simplest solution I
came up with was to create my own UimaContext object that also holds a Spring
ApplicationContext and pull my shared resources (Spring beans) in the Annotator
initialize method.  This is roughly what I did:

1. Create a object which holds a Spring ApplicationContext object and extends
the RootUimaContext_impl (RootUimaContext_impl implements UimaContext).

2. Create your ApplicationContext, which holds all of your resources, then set
the ApplicationContext in the object from step 1.

3. When creating the AnalysisEngine
(UIMAFramework.produceAnalysisEngine(aeDescription, resourceManager, params)),
make sure to add the parameter "UIMA_CONTEXT" which holds the UimaContext 
object that also contains the Spring ApplicationContext.  This will basically
overwrite the UimaContext that is created by default. 

You will essentially need to do what is done in
"Resource_ImplBase.initialize(...)" when creating your UimaContext (see
http://svn.apache.org/repos/asf/uima/uimaj/tags/uimaj-2.2.2/uimaj-2.2.2-fp1-02/
uimaj-core/src/main/java/org/apache/uima/resource/Resource_ImplBase.java).
 I'd recommend just debugging this method with a simple example just to see what
all is going on.

4. Instead of step 2, you could create a factory type object
(AnalysisEngineProducer) that produces the AnalysisEngine (step 3) and also
extends ApplicationContextAware.  By extending ApplicationContextAware and
overriding "setApplicationContext(...)", step 2 is automatically done for you
when you create a AnalysisEngineProducer bean in Spring.

5. Finally, in the "initialize(UimaContext uimaContext)" method of your
Annotators, you can get access to the object with the Spring ApplicationContext
(from step 1) by:
uimaContextWithApplicationContext =
(YourObjectContainingSpringApplicationContext) ((UimaContextAdmin)
uimaContext).getRootContext().

I haven't tried this solution to see if it works in PEAR packages, as I have no
use currently for PEAR, but this solution seems to work very well with little
code as oppose to extending the AnalysisEngineImpl, using external resource
files, or using UIMAfit.

Alex




Mime
View raw message