@Test
  public void testGetAnonymousInnerBeanFromScope() throws Exception {
    TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
    assertFalse(AopUtils.isAopProxy(bean));
    assertTrue(AopUtils.isCglibProxy(bean.getSpouse()));

    BeanDefinition beanDef = this.beanFactory.getBeanDefinition("outerBean");
    BeanDefinitionHolder innerBeanDef =
        (BeanDefinitionHolder) beanDef.getPropertyValues().getPropertyValue("spouse").getValue();
    String name = innerBeanDef.getBeanName();

    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    try {
      assertNull(request.getAttribute("scopedTarget." + name));
      assertEquals("scoped", bean.getSpouse().getName());
      assertNotNull(request.getAttribute("scopedTarget." + name));
      assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass());
      assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName());
    } finally {
      RequestContextHolder.setRequestAttributes(null);
    }
  }
 @Test
 public void testSessionScope() {
   WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
   MockHttpServletRequest request = new MockHttpServletRequest();
   ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
   RequestContextHolder.setRequestAttributes(requestAttributes);
   try {
     assertNull(request.getSession().getAttribute(NAME));
     DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
     assertSame(bean, request.getSession().getAttribute(NAME));
     assertSame(bean, ac.getBean(NAME));
     request.getSession().invalidate();
     assertTrue(bean.wasDestroyed());
   } finally {
     RequestContextHolder.setRequestAttributes(null);
   }
 }
  @Test
  public void testGetFromFactoryBeanInScope() throws Exception {
    String name = "requestScopedFactoryBean";
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    assertTrue(AopUtils.isCglibProxy(bean));

    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    try {
      assertNull(request.getAttribute("scopedTarget." + name));
      assertEquals(DummyFactory.SINGLETON_NAME, bean.getName());
      assertNotNull(request.getAttribute("scopedTarget." + name));
      assertEquals(DummyFactory.class, request.getAttribute("scopedTarget." + name).getClass());
      assertSame(bean, this.beanFactory.getBean(name));
    } finally {
      RequestContextHolder.setRequestAttributes(null);
    }
  }
  @Test
  public void testGetFromScopeThroughDynamicProxy() throws Exception {
    String name = "requestScopedProxy";
    ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
    assertTrue(AopUtils.isJdkDynamicProxy(bean));

    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    try {
      assertNull(request.getAttribute("scopedTarget." + name));
      assertEquals("scoped", bean.getName());
      assertNotNull(request.getAttribute("scopedTarget." + name));
      TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
      assertEquals(TestBean.class, target.getClass());
      assertEquals("scoped", target.getName());
      assertSame(bean, this.beanFactory.getBean(name));
      assertEquals(bean.toString(), target.toString());
    } finally {
      RequestContextHolder.setRequestAttributes(null);
    }
  }
  @Test
  public void testDestructionAtRequestCompletion() throws Exception {
    String name = "requestScopedDisposableObject";
    DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertTrue(AopUtils.isCglibProxy(bean));

    MockHttpServletRequest request = new MockHttpServletRequest();
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    try {
      assertNull(request.getAttribute("scopedTarget." + name));
      assertEquals("scoped", bean.getName());
      assertNotNull(request.getAttribute("scopedTarget." + name));
      assertEquals(DerivedTestBean.class, request.getAttribute("scopedTarget." + name).getClass());
      assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName());
      assertSame(bean, this.beanFactory.getBean(name));

      requestAttributes.requestCompleted();
      assertTrue(((TestBean) request.getAttribute("scopedTarget." + name)).wasDestroyed());
    } finally {
      RequestContextHolder.setRequestAttributes(null);
    }
  }