struts.xml
<struts>
<constant name="struts.objectFactory" value="guice" />
<constant name="guice.module" value="com.arsenalist.MyModule"/>
<package name="defaultPackage" extends="struts-default">
<action name="viewLogin" class="com.arsenalist.LoginAction">
<result>/WEB-INF/jsp/login.jsp</result>
</action>
</package>
</struts>
MyModule.java
public class MyModule extends AbstractModule {
protected void configure() {
log.debug("inside configure"); // never gets printed
bind(Service.class)
.to(ServiceImpl.class)
.in(Scopes.SINGLETON);
}
}
LoginAction.java
public class LoginAction extend ActionSupport {
private Service service;
@Inject
public void setService(Service service) {
this.service = service;
}
public String execute() {
return SUCCESS;
}
}
However, when I run this I get the following error:
javax.servlet.ServletException: Unable to instantiate Action,
com.arsenalist.LoginAction, defined for 'viewLogin' in namespace
'/'com.opensymphony.xwork2.inject.ContainerImpl
$MissingDependencyException: No mapping found for dependency
[type=com.arsenalist.Service, name='default'] in public void
com.arsenalist.LoginAction.setService(com.arsenalist.Service).
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
518)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:
421)
com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:
118
com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:
52)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:
99)
Now, it can't find a mapping for com.arsenalist.Service even though
one is defined in MyModule and MyModule is referenced using the
guice.module constant inside the struts.xml file. I never see the
debug statement in the logs so the MyModule.configure() never gets
called. Any ideas how to have Guice/Struts2 recognize the mappings in
MyModule? I'm following the setup instructions here:
http://docs.google.com/Doc?id=dd2fhx4z_5df5hw8
Thanks.
Zarar
[type=com.arsenalist.Service , name='default'] in public void
INFO: Creating injector...
com.google.inject.CreationException: Guice configuration errors:
1) Error at com.arsenalist.LoginAction.setService(LoginAction.java:
14):
Binding to com.arsenalist.Service not found. No bindings to that type
were found.
1 error[s]
at com.google.inject.BinderImpl.createInjector(BinderImpl.java:
277)
at com.google.inject.Guice.createInjector(Guice.java:79)
at com.google.inject.Guice.createInjector(Guice.java:53)
at com.google.inject.Guice.createInjector(Guice.java:43)
at
com.google.inject.struts2.GuiceObjectFactory.buildBean(GuiceObjectFactory.java:
101)
at
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:
152)
at
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:
141)
at
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:
111)
at
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:
270)
at
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:
360)
at com.opensymphony.xwork2.DefaultActionInvocation.access
$000(DefaultActionInvocation.java:38)
at com.opensymphony.xwork2.DefaultActionInvocation
$1.doProfiling(DefaultActionInvocation.java:78)
at
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
at
com.opensymphony.xwork2.DefaultActionInvocation.<init>(DefaultActionInvocation.java:
70)
at
com.opensymphony.xwork2.DefaultActionInvocation.<init>(DefaultActionInvocation.java:
66)
at
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:
189)
at
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:
41)
at
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
497)
at
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:
421)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
202)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
173)
> > [type=com.arsenalist.Service, name='default'] in public void
@ImplementedBy(ServiceImpl.class)
public interface Service {...}
@Singleton
public class ServiceImpl {...}
public class LoginAction extend ActionSupport {
...
@Inject
public void setService(Service service) {
this.service = service;
}
...
}
Zarar
> > com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:
> > > > org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java
> > :
> > > > 518)
>
> > > > org.apache.struts2.dispatcher.FilterDispatcher.doFilter(
> > > > FilterDispatcher.java:
> > > > 421)
>
> > > > com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(
http://code.google.com/p/google-guice/issues/detail?id=63
Thanks,
Zarar
On Mar 14, 12:19 am, "Bob Lee" <crazy...@crazybob.org> wrote:
> > [type=com.arsenalist.Service, name='default'] in public void
> > com.arsenalist.LoginAction.setService(com.arsenalist.Service).
>
> > org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
> > 518)
>
> > org.apache.struts2.dispatcher.FilterDispatcher.doFilter(
> > FilterDispatcher.java:
> > 421)
>
> > com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(
> > PageFilter.java:
> > 118
>
> > com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(
> > PageFilter.java:
> > 52)
>
> > org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(
> > ActionContextCleanUp.java:
> > org.apache.struts2.dispatcher.Dispatcher.serviceAction (Dispatcher.java:
> > 518)
>
> > org.apache.struts2.dispatcher.FilterDispatcher.doFilter(
> > FilterDispatcher.java:
> > 421)
>
> > com.opensymphony.module.sitemesh.filter.PageFilter.parsePage (
> > org.apache.struts2.dispatcher.Dispatcher.serviceAction (Dispatcher.java:
> > 518)
>
> > org.apache.struts2.dispatcher.FilterDispatcher.doFilter(
> > FilterDispatcher.java:
> > 421)
>
> > com.opensymphony.module.sitemesh.filter.PageFilter.parsePage (