title: AnnotationConfigApplicationContext.getBean returns a different bean, Spring
body: <p>I have a problem that I have a ClassA needs <code>RoomService</code> to be injected, and it works fine that I find in ClassA, the roomService's id is the same.</p>
  
  <p>While for some reason, I need roomservice to create room instance based on some input param for me, so I use below config to achieve this:</p>
  
  <pre><code>@Configuration
  @EnableAspectJAutoProxy
  public class Application {
  
  private static ApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);
  
      public static ApplicationContext getApplicationContext(){
      return ctx;
  }
  
      @Bean        
      public RoomService roomService(){
          return new RoomService();//Singleton
      }
  
      @Bean
      @Scope("prototype")
      public AbstractRoom room(AbstractRoom.Mode roomMode){
          RoomService roomService = (RoomService) ctx.getBean(RoomService.class);
          LogUtil.debug("--------from application:" +roomService.id1);//here, I find the id is different every time
          return roomService.newRoom(roomMode);
      }
  }
  </code></pre>
  
  <p>The problem is that I need <code>RoomService</code> to be singleton, but I find that in the Application.java , the ctx.getBean(roomService) always returns a different bean which has different id. Isn't Spring should reuse the same bean? Why is that?</p>
  
  <p>Here is how I create a room in RoomService.java</p>
  
  <pre><code>public AbstractRoom createRoom(String userID,int playerCount,Mode roomMode){
  
      ApplicationContext ctx =Application.getApplicationContext();
      AbstractRoom room = (AbstractRoom)ctx.getBean(AbstractRoom.class,roomMode);
  
  }
  </code></pre>
  
  <p>Update:
  I tried reusing the same ctx and it does not work. One hint is that I find my constructor of <code>RoomService()</code> is called several times(I put a break point in it.) when I run tomcat to start it </p>
  
  <p>Here is my web.xml</p>
  
  <pre><code> &lt;!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  
  &lt;web-app&gt;
      &lt;display-name&gt;wodinow&lt;/display-name&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Please help!</p>