Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Stephen Starkey <stephen-SEnoDGAnjO/a5EbDDlwbIw <at> public.gmane.org>
Subject: Stripes + Guice -- Implemented
Newsgroups: gmane.comp.java.stripes.user
Date: Wednesday 14th March 2007 00:28:05 UTC (over 9 years ago)
If anyone cares, I went and replaced our Spring integration with  
Guice today (my project is small -- 200+ classes), and it was very  
easy to get Guice injection into action beans.  'Twas ridiculously  
easy.. :-).  Now, granted I could have stored the injector into the  
context instead of making it static, but I was lazy..

-- Added a GuiceInterceptor
@Intercepts(LifecycleStage.ActionBeanResolution)
public class GuiceInterceptor implements Interceptor {
   private static Injector injector;

   public Resolution intercept(ExecutionContext executionContext)  
throws Exception {
     Resolution resolution = executionContext.proceed();
     injector.injectMembers(executionContext.getActionBean());
     return resolution;
   }

   public static void setup(Injector injector) {
     GuiceInterceptor.injector = injector;
   }
}


-- Added a context listener that set up my context (this is necessary  
anyway since binding is done in code instead of XML)
  public void contextInitialized(final ServletContextEvent  
servletContextEvent) {
     injector = Guice.createInjector(new AppModule());
     GuiceInterceptor.setup(injector);
}

-- Changes to web.xml (Note I also use StripesSecurity -- just  
replace that with the standard filter and you're set)
   
     Stripes Filter
     StripesFilter
     net.sourceforge.stripes.security.controller.StripesSecurityFilter< 
/filter-class>
     
       SecurityManager.Class
       org.xptracker.services.SecurityManager
     
     
       UnauthorizedResolutionURL
       /Main.action
     
     
       ActionResolver.UrlFilters
       WEB-INF/lib/xptracker
     
     
       Interceptor.Classes
       
          
net.sourceforge.stripes.security.controller.SecurityInterceptor,
         org.xptracker.inject.GuiceInterceptor,
         net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor
       
     
     
       TypeConverterFactory.Class
       org.xptracker.web.TypeConverterFactory
     
   

-- Sample action bean
@UrlBinding("/Users.action")
public class UsersActionBean extends StandardActionBean {
   private List users;
   private User loggedIn;
   private String role;

   @Inject
   private EmailService emailer;

   // ... and so forth...
} 

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share
your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
 
CD: 3ms