title: Dependency Injection in Struts2 Accessing Session Scoped Beans
body: <p>Recently I needed to use DI in Struts2. I know it uses it's own DI implementation like Guice but not Guice, as far as I couldn't find some annotations suitable to set the scope for injected beans. To be short, I created a bean</p>
  
  <pre><code>//@Repository
  //@Scope("session")
  public class Session {
  
      private Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
  
      public Map&lt;String, Object&gt; getMap() {
          return map;
      }
  
      public void setMap(Map&lt;String, Object&gt; map) {
          this.map = map;
      }
  }
  </code></pre>
  
  <p>I have commented the annotations used with Spring beans. I was successfully created the same bean via spring DI and set the scope in which my objects were injected. Now, I want to do the same with Struts2 and DI. For this purpose I created the bean definition in <code>struts.xml</code></p>
  
  <pre><code>&lt;bean name="session" class="jspbean.struts.Session" scope="session"/&gt;
  </code></pre>
  
  <p>and simple action to get that bean created and injected into my action</p>
  
  <pre><code>public class DefaultAction extends ActionSupport {
  
      private Session session;
  
      //  @Autowired
      @Inject("session")
      public void setSession(Session session) {
          this.session = session;
      }
  
      public Session getSession() {
          return session;
      }
  
      private Map&lt;String, String&gt; myMap = new HashMap&lt;String, String&gt;();
  
      public Map&lt;String, String&gt; getMyMap() {
          return myMap;
      }
  
      public void setMyMap(Map&lt;String, String&gt; myMap) {
          this.myMap = myMap;
      }
  
      @Override
      public String execute() throws Exception {
          //populate my bean with sample data
          myMap.put("q1", "Question1");
          myMap.put("q2", "Question2");
          session.getMap().put("myMap", myMap);
          return SUCCESS;
      }
  }
  </code></pre>
  
  <p>in the JSP I use simple iterator over session bean</p>
  
  <pre><code>&lt;s:iterator value="session.map['myMap']"&gt;
    &lt;s:textfield name="myMap['%{key}']" value="%{value}" theme="simple" size="10" /&gt;&lt;br&gt;
  &lt;/s:iterator&gt;
  </code></pre>
  
  <p>Now, when I'm running this smple application I've got the exception</p>
  
  <pre><code>Stacktraces
  Unable to instantiate Action, jspbean.struts.DefaultAction, defined for '' in namespace '/'java.lang.IllegalStateException: Scope strategy not set. Please call Container.setScopeStrategy().
  
      com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:316)
      com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
      com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
      org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
      org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
      com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
      org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
      org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
      org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
      org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
      java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      java.lang.Thread.run(Unknown Source)
  
  
  java.lang.RuntimeException: java.lang.IllegalStateException: Scope strategy not set. Please call Container.setScopeStrategy().
  
      com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:301)
      com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:492)
      com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:530)
      com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:528)
      com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
      com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:528)
      com.opensymphony.xwork2.ObjectFactory.injectInternalBeans(ObjectFactory.java:139)
      com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:208)
      com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)
      com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
      com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
      com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
      com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
      com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
      com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
      org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
      org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
      com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
      org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
      org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
      org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
      org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
      java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      java.lang.Thread.run(Unknown Source)
  
  
  java.lang.IllegalStateException: Scope strategy not set. Please call Container.setScopeStrategy().
  
      com.opensymphony.xwork2.inject.InternalContext.getScopeStrategy(InternalContext.java:53)
      com.opensymphony.xwork2.inject.Scope$5$1.create(Scope.java:130)
      com.opensymphony.xwork2.inject.ContainerImpl$ParameterInjector.inject(ContainerImpl.java:469)
      com.opensymphony.xwork2.inject.ContainerImpl.getParameters(ContainerImpl.java:484)
      com.opensymphony.xwork2.inject.ContainerImpl.access$000(ContainerImpl.java:34)
      com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:299)
      com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:492)
      com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:530)
      com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:528)
      com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
      com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:528)
      com.opensymphony.xwork2.ObjectFactory.injectInternalBeans(ObjectFactory.java:139)
      com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:208)
      com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)
      com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
      com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
      com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
      com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
      com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
      com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
      org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
      org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
      com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
      org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
      org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
      org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
      org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
      java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>The exception says that I need to set the scope strategy. So, my question is what is this scope strategy and how it could be implemented in my simple application. Also, there is annotation <code>@Scoped</code>, how this annotations to apply in my case?</p>
  
  <p>My example references:</p>
  
  <ol>
  <li><a href="https://cwiki.apache.org/WW/bean-configuration.html" rel="nofollow">bean configuration</a> </li>
  </ol>