Hi,
I'm having problems when I try to @Inject a service on a Servlet. Currently
I'm not receiving any error but my service is null. This problem only
happens on my servlet as the service works in another places of the app.
What I have is something like this:
mainFolder
+ server
+ serviceFolder
- MyService.java
- MyServletContextListener.java
- MyServletModule.java
- HelloWorldServlet.java
+ WEB-INF
- web.xml
The files looks like:
1) web.xml
<web-app>
………
<servlet>
<servlet-name>helloWorldServlet</servlet-name>
<servlet-class>
mainFolder.HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorldServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.google.crm.grm.refdata.server.RefDataServletContextListener</listener-class>
</listener>
</web-app>
2) MyService.java
@RequestScoped
public class GrmDataService {
……..
public List<String> getFoo() {
…..
}
……..
}
3) MyServletContextListener.java
public class MyServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(
// Probably something is wrong here as I'm adding a module where the
service live but on
// that service I don't have a direct binding for this specific
service
….
}
}
4) MyServletModule.java
public class MyServletModule extends ServletModule {
@Override
protected void configureServlets() {
Names.bindProperties(binder(), getProps());
……..
serve("/path").with(HelloWorldServlet.class);
……..
}
…….
}
5) HelloWorldServlet.java
@Singleton
public class HelloWorldServlet extends HttpServlet {
@Inject
private Provider<MyService> myServiceProvider;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException {
response.setContentType("text/plain");
if (myServiceProvider != null) {
MyService myService = myServiceProvider.get();
response.getWriter().println("The service is not null");
} else {
response.getWriter().println("The service is null");
}
}
……
}
Of course I always receive the message "The service is null".
Just as a test I create a filter class on the same packages as
HelloWorldServlet.java where I @Inject the provider on the same way:
@Singleton
public class MyServiceFilter implements Filter {
@Inject
private final Provider<MyService> myServiceProvider;
……
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse,
FilterChain filterChain) throws IOException, ServletException {
if (servletRequest instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) servletRequest;
MyService myService = myServiceProvider.get();
List<String> myFoo = myService.getFoo();
…..
}
}
….
}
And this is working!!!
Any thoughts?
Thanks,
Mario
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/BHRRaNYRCDMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.