package server.config;
import javax.servlet.annotation.WebListener;
import server.model.Employee;
import server.repo.JpaEmployeeRepository;
import server.repo.Repository;
import server.repo.StubEmployeeRepository;
import server.rest.EmployeeResource;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
// JPA: https://code.google.com/p/google-guice/wiki/JPA
@WebListener
public class GuiceDependencyInjector extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServlet(), new JpaPersistModule("myapp-db"));
}
private class JerseyServlet extends JerseyServletModule {
@Override
protected void configureServlets() {
// install(new JpaPersistModule("myapp-db"));
bind(EmployeeResource.class);
// for non generic interface/class
// bind(Repository.class).annotatedWith(Names.named("stub")).to(StubEmployeeRepository.class);
bind(new TypeLiteral<Repository<Employee>>() {
}).annotatedWith(Names.named("stub")).to(StubEmployeeRepository.class);
bind(new TypeLiteral<Repository<Employee>>() {
}).annotatedWith(Names.named("jpa")).to(JpaEmployeeRepository.class);
filter("/*").through(PersistFilter.class);
serve("/*").with(GuiceContainer.class);
}
}
}