1.module绑定
package lee.guice.module;
import lee.guice.service.IHelloService;
import lee.guice.service.impl.CheseHelloServiceImpl;
import com.google.inject.AbstractModule;
public class ConfigModule extends AbstractModule{
public void configure() {
//对应接口与实现类
bind(IHelloService.class).to(CheseHelloServiceImpl.class);
}
}
2.编写一个listener
package lee.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import lee.guice.module.ConfigModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.ServletModule;
public class GuiceListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.removeAttribute(Injector.class.getName());
System.out.println("...... servlet context remove guice ......");
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
Injector injector = Guice.createInjector(new Module[] { new ConfigModule(), new ServletModule() });
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute(Injector.class.getName(), injector);
System.out.println("...... servlet context init guice ......");
}
}
3.编写基类servlet,用来获取inject实例
package lee.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.google.inject.Injector;
public abstract class InjectServlet extends HttpServlet{
@Override
public void init(ServletConfig config) throws ServletException{
ServletContext context = config.getServletContext();
Injector injector = (Injector) context.getAttribute(Injector.class.getName());
if (injector == null) {
throw new ServletException("Guice Injector not found");
}
injector.injectMembers(this);
}
}
4.web.xml中配置监听
<listener>
<listener-class>lee.listener.GuiceListener</listener-class>
</listener>
5.测试,所写servlet需要继承InjectServlet
package lee.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lee.guice.service.IHelloService;
import com.google.inject.Inject;
import com.google.inject.servlet.SessionScoped;
public class Hello extends InjectServlet implements Servlet {
@Inject
private IHelloService service;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(service.hello());
out.flush();
out.close();
}
}
参考文章:http://java-x.blogspot.com/2007/03/my-first-guice-web-application.html