コード例 #1
0
 public void preInstantiateSingletons() throws BeansException {
   if (logger.isInfoEnabled()) {
     logger.info("Pre-instantiating singletons in factory [" + this + "]");
   }
   try {
     for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext(); ) {
       String beanName = (String) it.next();
       if (containsBeanDefinition(beanName)) {
         RootBeanDefinition bd = getMergedBeanDefinition(beanName, false);
         if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
           if (bd.hasBeanClass() && FactoryBean.class.isAssignableFrom(bd.getBeanClass())) {
             FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
             if (factory.isSingleton()) {
               getBean(beanName);
             }
           } else {
             getBean(beanName);
           }
         }
       }
     }
   } catch (BeansException ex) {
     // destroy already created singletons to avoid dangling resources
     try {
       destroySingletons();
     } catch (Throwable ex2) {
       logger.error(
           "Pre-instantiating singletons failed, "
               + "and couldn't destroy already created singletons",
           ex2);
     }
     throw ex;
   }
 }
コード例 #2
0
  public void preInstantiateSingletons() throws BeansException {
    if (logger.isDebugEnabled()) {
      logger.debug("Pre-instantiating singletons in factory [" + this + "]");
    }

    // The superclass's variable by this name is declared private.
    String[] beanDefinitionNames = getBeanDefinitionNames();
    String beanName = null; // Remember in case of an exception
    try {
      // for (Iterator it = this.beanDefinitionNames.iterator();
      // it.hasNext();) {
      for (int i = 0; i < beanDefinitionNames.length; i++) {
        beanName = beanDefinitionNames[i];
        if (!containsSingleton(beanName) && containsBeanDefinition(beanName)) {
          RootBeanDefinition bd = (RootBeanDefinition) getMergedBeanDefinition(beanName);
          if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            if (bd.hasBeanClass() && FactoryBean.class.isAssignableFrom(bd.getBeanClass())) {
              FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
              if (factory.isSingleton()) {
                getBean(beanName);
              }
            } else {
              getBean(beanName);
            }
          }
        }
      }
    } catch (BeansException ex) {
      // Destroy already created singletons to avoid dangling resources.
      logger.error(
          "Failed to preinstantiate the singleton named "
              + beanName
              + ". Destroying all Spring beans.",
          ex);
      try {
        destroySingletons();
      } catch (Exception ex2) {
        logger.error(
            "Pre-instantiating singletons failed, and couldn't destroy already created singletons: "
                + ex2,
            ex2);
      }
      throw ex;
    }
  }
コード例 #3
0
  @Test
  public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testInterceptor", TestInterceptor.class);

    RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
    proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
    proxyCreator
        .getPropertyValues()
        .add("beanNames", "singletonToBeProxied,&singletonFactoryToBeProxied");
    sac.getDefaultListableBeanFactory()
        .registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);

    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);

    sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

    sac.refresh();

    ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
    assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass()));

    TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
    int initialNr = ti.nrOfInvocations;
    singletonToBeProxied.getName();
    assertEquals(initialNr + 1, ti.nrOfInvocations);

    FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
    assertTrue(Proxy.isProxyClass(factory.getClass()));
    TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
    assertFalse(AopUtils.isAopProxy(tb));
    assertEquals(initialNr + 3, ti.nrOfInvocations);
    tb.getAge();
    assertEquals(initialNr + 3, ti.nrOfInvocations);
  }