Java 如何在guice模块中注入servlet上下文?
很抱歉问题标题不好Java 如何在guice模块中注入servlet上下文?,java,guice,resteasy,servletcontextlistener,Java,Guice,Resteasy,Servletcontextlistener,很抱歉问题标题不好 免责声明:对于主要使用dropwizard的web应用没有太多经验 因此,我一直在尝试在java web应用程序中使用guice 我的servlet/resource文件中有servletContext,但我需要将它注入到我的guice模块中,如何在guice模块中注入servlet上下文 参考代码如下,任何帮助都将不胜感激 缓存服务 public interface CacheService { public abstract String hello(); }
免责声明:对于主要使用dropwizard的web应用没有太多经验 因此,我一直在尝试在java web应用程序中使用guice
我的servlet/resource文件中有servletContext,但我需要将它注入到我的guice模块中,如何在guice模块中注入servlet上下文 参考代码如下,任何帮助都将不胜感激 缓存服务
public interface CacheService {
public abstract String hello();
}
public class CacheModule implements Module{
public void configure(final Binder binder) {
binder.bind(ResourceServlet.class);
binder.bind(CacheService.class).to(CacheServiceImpl.class);
}
}
CacheServiceImpl
public class CacheServiceImpl implements CacheService {
JedisPool jedisPool = null;
@Context
private ServletContext servletContext;
@Provides
public String hello() {
log.info("CacheServiceImpl servletContext {}", servletContext); // this is null
return "hello world";
}
@Provides
public Jedis getJedisConnection() {
HostAndPort redisServer = HostAndPortUtil.getRedisServers().get(0);
if (jedisPool == null)
jedisPool = new JedisPool(getJedisPoolConfig(), redisServer.getHost(),
redisServer.getPort(), Protocol.DEFAULT_TIMEOUT,
servletContext.getInitParameter("REDIS_PSWD")); // sample example of servletContext requirement, there are other functions in this module which need context
return jedisPool.getResource();
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>resteasy.guice.modules</param-name>
<param-value>com.org.app.guice.CacheModule</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
}
缓存模块
public interface CacheService {
public abstract String hello();
}
public class CacheModule implements Module{
public void configure(final Binder binder) {
binder.bind(ResourceServlet.class);
binder.bind(CacheService.class).to(CacheServiceImpl.class);
}
}
ResourceServlet
@Slf4j
@Path("/")
@Singleton
public class ResourceServlet {
@Context
private ServletContext servletContext;
@Inject
public ResourceServlet(CacheService storageModule) throws JAXBException {
log.info("base {}", storageModule.hello());
}
@GET
@Path("/test")
@Produces({MediaType.APPLICATION_JSON})
public Product abc() {
log.info("servletContext {}", servletContext); // this is not null but how do i pass it on to module?
}
}
web.xml
public class CacheServiceImpl implements CacheService {
JedisPool jedisPool = null;
@Context
private ServletContext servletContext;
@Provides
public String hello() {
log.info("CacheServiceImpl servletContext {}", servletContext); // this is null
return "hello world";
}
@Provides
public Jedis getJedisConnection() {
HostAndPort redisServer = HostAndPortUtil.getRedisServers().get(0);
if (jedisPool == null)
jedisPool = new JedisPool(getJedisPoolConfig(), redisServer.getHost(),
redisServer.getPort(), Protocol.DEFAULT_TIMEOUT,
servletContext.getInitParameter("REDIS_PSWD")); // sample example of servletContext requirement, there are other functions in this module which need context
return jedisPool.getResource();
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>resteasy.guice.modules</param-name>
<param-value>com.org.app.guice.CacheModule</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
resteasy.guice.modules
com.org.app.guice.CacheModule
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
放松
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
放松
/*
ServletContext
有点不同,您需要自己注入实现-您需要扩展guicerestasybootstrapservletcontextlistener
并将其添加到模块新的CacheModuleImpl(context)
中。请看最后一节ServletContext
有点不同,您需要自己注入实现-您需要扩展GuiceResteasyBootstrapServletContextListener
并将其添加到您的模块新的CacheModuleImpl(context)
中。请看最后一节