Created
March 15, 2013 18:09
-
-
Save jnmronquillo/5171827 to your computer and use it in GitHub Desktop.
Guice and Shiro Aop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.servlet.ServletContext; | |
import javax.servlet.ServletContextEvent; | |
import org.apache.shiro.guice.aop.ShiroAopModule; | |
import org.apache.shiro.guice.web.GuiceShiroFilter; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import com.google.inject.Provides; | |
import com.google.inject.Singleton; | |
import com.google.inject.persist.PersistFilter; | |
import com.google.inject.persist.jpa.JpaPersistModule; | |
import com.google.inject.servlet.GuiceServletContextListener; | |
import com.google.inject.servlet.ServletModule; | |
import com.rhem.server.requestfactory.InjectedRequestFactoryModule; | |
import com.rhem.server.requestfactory.InjectedRequestFactoryServlet; | |
//http://turbomanage.wordpress.com/2009/12/11/how-to-inject-guice-objects-in-a-jsp/ | |
public class MyGuiceServletConfig extends GuiceServletContextListener { | |
private ServletContext servletContext; | |
@Override | |
public void contextInitialized(ServletContextEvent servletContextEvent) { | |
// No call to super as it also calls getInjector() | |
servletContext = servletContextEvent.getServletContext(); | |
servletContext.setAttribute(Injector.class.getName(), getInjector()); | |
} | |
@Override | |
public void contextDestroyed(ServletContextEvent servletContextEvent) { | |
ServletContext sc = servletContextEvent.getServletContext(); | |
sc.removeAttribute(Injector.class.getName()); | |
super.contextDestroyed(servletContextEvent); | |
} | |
@Override | |
protected Injector getInjector() { | |
return Guice.createInjector( | |
new MyShiroWebModule(servletContext), | |
new ShiroAopModule(), | |
new ServletModule(){ | |
@Override | |
protected void configureServlets() { | |
install(new JpaPersistModule("principalJpaUnit")); // like we saw earlier. | |
filter("/*").through(PersistFilter.class); | |
install(new InjectedRequestFactoryModule()); | |
serve("/gwtRequest").with(InjectedRequestFactoryServlet.class); | |
//shiro filter | |
filter("/*").through(GuiceShiroFilter.class); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment