Using Guice with embedded Tomcat?

I have a jersey 2 project with Guice used for DI (via hk2 bridge). Spring JDBC is used for DB calls and is configured through Guice. I am running it locally with tomcat embedded. This setup works great for the application, meaning I can access the database in my jersey resources.

Now I want to write test cases for an application where I need database access for initial setup, but I am getting NullPointerException on nested objects.

Main file (input gives null here)

public class StartApp {
    @Inject
    private JdbcTemplate jdbcTemplate;

    public static void main(String[] args) throws Exception {
        startTomcat();
    }

    private static void startTomcat() throws Exception {
        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();
        String webPort = System.getProperty("app.port", "8080");
        tomcat.setPort(Integer.valueOf(webPort));
        tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        tomcat.start();
        new StartApp().initDatabase();
        tomcat.getServer().await();
    }

    public void initDatabase() throws Exception {
        String sql = new String(Files.readAllBytes(Paths.get(StartApp.class.getClassLoader().getResource("db_base.sql").toURI())), "UTF-8");
        jdbcTemplate.execute(sql);
    }
}

      

JdbcTemplate error only here. In real Jersey resources, it works great.

web.xml (only showing part only)

<filter>
    <filter-name>Guice Filter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>Guice Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>MyGuiceServletContextListener</listener-class>
</listener>

      

MyGuiceServletContextListener

public class MyGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(JdbcTemplate.class).toProvider(JdbcTemplateProvider.class).in(Scopes.SINGLETON);
            }
        });
    }
}

      

JerseyConfig

public class JerseyConfig extends ResourceConfig {
    @Inject
    public JerseyConfig(ServiceLocator serviceLocator, ServletContext servletContext) {
        packages("resources");

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector((Injector) servletContext.getAttribute(Injector.class.getName()));
    }
}

      

+3


source to share


1 answer


Since tomcat is started in a different process, the guice created in Jersey is not available in StartApp. Guice injector must be instantiated in StartApp to get JdbcTemplate instamnce



JdbcTemplate jdbcTemplate = Guice.createInjector(new PaymentGatewayModule()).getInstance(JdbcTemplate.class);

      

0


source







All Articles