Package org.springframework.beans.factory.config

Examples of org.springframework.beans.factory.config.ConfigurableBeanFactory


      }
    } catch (IOException e) {
      log.error("Error looking for config file", e);
    }
    // add the context to the parent, this will be red5.xml
    ConfigurableBeanFactory factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    if (factory.containsSingleton(name)) {
      log.warn("Singleton {} already exists, try unload first", name);
      return;
    }
    // if parent context was not set then lookup red5.common
    if (parentContext == null) {
      log.debug("Lookup common - bean:{} local:{} singleton:{}",
          new Object[] { factory.containsBean("red5.common"), factory.containsLocalBean("red5.common"), factory.containsSingleton("red5.common"), });
      parentContext = (ApplicationContext) factory.getBean("red5.common");
    }
    if (config.startsWith("/")) {
      // Spring always interprets files as relative, so will strip a leading slash unless we tell
      // it otherwise. It also appears to not need this for Windows
      // absolute paths (e.g. C:\Foo\Bar) so we don't catch that either
      String newConfig = "file://" + config;
      log.debug("Resetting {} to {}", config, newConfig);
      config = newConfig;
    }
    ApplicationContext context = new FileSystemXmlApplicationContext(new String[] { config }, parentContext);
    log.debug("Adding to context map - name: {} context: {}", name, context);
    if (contextMap == null) {
      contextMap = new ConcurrentHashMap<String, ApplicationContext>(3, 0.9f, 1);
    }
    contextMap.put(name, context);
    // Register context in parent bean factory
    log.debug("Registering - name: {}", name);
    factory.registerSingleton(name, context);
  }
View Full Code Here


    log.debug("Context from map: {}", context);
    String[] bnames = BeanFactoryUtils.beanNamesIncludingAncestors(context);
    for (String bname : bnames) {
      log.debug("Bean: {}", bname);
    }
    ConfigurableBeanFactory factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    if (factory.containsSingleton(name)) {
      log.debug("Context found in parent, destroying: {}", name);
      FileSystemXmlApplicationContext ctx = (FileSystemXmlApplicationContext) factory.getSingleton(name);
      if (ctx.isRunning()) {
        log.debug("Context was running, attempting to stop");
        ctx.stop();
      }
      if (ctx.isActive()) {
        log.debug("Context is active, attempting to close");
        ctx.close();
      } else {
        try {
          factory.destroyBean(name, ctx);
        } catch (Exception e) {
          log.warn("Context destroy failed for: {}", name, e);
          ctx.destroy();
        } finally {
          if (factory.containsSingleton(name)) {
            log.debug("Singleton still exists, trying another destroy method");
            ((DefaultListableBeanFactory) factory).destroySingleton(name);
          }
        }
      }
View Full Code Here

        if (!(beanFactory instanceof ConfigurableBeanFactory))
        {
            throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
        }
        ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;

        ScopedBeanTargetSource scopedTargetSource = new ScopedBeanTargetSource(
                scope, conversationName, targetBeanName, objectFactory, beanFactory);

        ProxyFactory pf = new ProxyFactory();
        pf.setProxyTargetClass(true);
        pf.setTargetSource(scopedTargetSource);

        Class beanType = beanFactory.getType(targetBeanName);
        if (beanType == null)
        {
            throw new IllegalStateException("Cannot create scoped proxy for bean '" + targetBeanName +
                    "': Target type could not be determined at the time of proxy creation.");
        }

        // Add an introduction that implements only the methods on ScopedObject.
        // Not sure if this is useful...
        ScopedObject scopedObject = new DefaultScopedObject(cbf, scopedTargetSource.getTargetBeanName());
        pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));

        // Add the AopInfrastructureBean marker to indicate that the scoped proxy
        // itself is not subject to auto-proxying! Only its target bean is.
        // Not sure if this is needed....
        pf.addInterface(AopInfrastructureBean.class);

        return pf.getProxy(cbf.getBeanClassLoader());
    }
View Full Code Here

        if (!(beanFactory instanceof ConfigurableBeanFactory))
        {
            throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
        }
        ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;

        ScopedBeanTargetSource scopedTargetSource = new ScopedBeanTargetSource(
                scope, conversationName, targetBeanName, objectFactory, beanFactory);

        ProxyFactory pf = new ProxyFactory();
        pf.setProxyTargetClass(true);
        pf.setTargetSource(scopedTargetSource);

        Class beanType = beanFactory.getType(targetBeanName);
        if (beanType == null)
        {
            throw new IllegalStateException("Cannot create scoped proxy for bean '" + targetBeanName +
                    "': Target type could not be determined at the time of proxy creation.");
        }

        // Add an introduction that implements only the methods on ScopedObject.
        // Not sure if this is useful...
        ScopedObject scopedObject = new DefaultScopedObject(cbf, scopedTargetSource.getTargetBeanName());
        pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));

        // Add the AopInfrastructureBean marker to indicate that the scoped proxy
        // itself is not subject to auto-proxying! Only its target bean is.
        // Not sure if this is needed....
        pf.addInterface(AopInfrastructureBean.class);

        return pf.getProxy(cbf.getBeanClassLoader());
    }
View Full Code Here

        }
       
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (defaultBus
                && applicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory) {
                ConfigurableBeanFactory bf = (ConfigurableBeanFactory)applicationContext
                    .getAutowireCapableBeanFactory();
                bf.registerSingleton("cxf", bus);
            }
        }
View Full Code Here

     */
    @Override
    public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object[] beanMethodArgs,
          MethodProxy cglibMethodProxy) throws Throwable {

      ConfigurableBeanFactory beanFactory = getBeanFactory(enhancedConfigInstance);
      String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);

      // Determine whether this bean is a scoped-proxy
      Scope scope = AnnotationUtils.findAnnotation(beanMethod, Scope.class);
      if (scope != null && scope.proxyMode() != ScopedProxyMode.NO) {
        String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);
        if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {
          beanName = scopedBeanName;
        }
      }

      // To handle the case of an inter-bean method reference, we must explicitly check the
      // container for already cached instances.

      // First, check to see if the requested bean is a FactoryBean. If so, create a subclass
      // proxy that intercepts calls to getObject() and returns any cached bean instance.
      // This ensures that the semantics of calling a FactoryBean from within @Bean methods
      // is the same as that of referring to a FactoryBean within XML. See SPR-6602.
      if (factoryContainsBean(beanFactory, BeanFactory.FACTORY_BEAN_PREFIX + beanName) &&
          factoryContainsBean(beanFactory, beanName)) {
        Object factoryBean = beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
        if (factoryBean instanceof ScopedProxyFactoryBean) {
          // Pass through - scoped proxy factory beans are a special case and should not
          // be further proxied
        }
        else {
          // It is a candidate FactoryBean - go ahead with enhancement
          return enhanceFactoryBean(factoryBean.getClass(), beanFactory, beanName);
        }
      }

      if (isCurrentlyInvokedFactoryMethod(beanMethod)) {
        // The factory is calling the bean method in order to instantiate and register the bean
        // (i.e. via a getBean() call) -> invoke the super implementation of the method to actually
        // create the bean instance.
        if (BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
          logger.warn(String.format("@Bean method %s.%s is non-static and returns an object " +
              "assignable to Spring's BeanFactoryPostProcessor interface. This will " +
              "result in a failure to process annotations such as @Autowired, " +
              "@Resource and @PostConstruct within the method's declaring " +
              "@Configuration class. Add the 'static' modifier to this method to avoid " +
              "these container lifecycle issues; see @Bean javadoc for complete details",
              beanMethod.getDeclaringClass().getSimpleName(), beanMethod.getName()));
        }
        return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs);
      }
      else {
        // The user (i.e. not the factory) is requesting this bean through a
        // call to the bean method, direct or indirect. The bean may have already been
        // marked as 'in creation' in certain autowiring scenarios; if so, temporarily
        // set the in-creation status to false in order to avoid an exception.
        boolean alreadyInCreation = beanFactory.isCurrentlyInCreation(beanName);
        try {
          if (alreadyInCreation) {
            beanFactory.setCurrentlyInCreation(beanName, false);
          }
          return beanFactory.getBean(beanName);
        }
        finally {
          if (alreadyInCreation) {
            beanFactory.setCurrentlyInCreation(beanName, true);
          }
        }
      }
    }
View Full Code Here

  public void testAliasing() {
    BeanFactory bf = getBeanFactory();
    if (!(bf instanceof ConfigurableBeanFactory)) {
      return;
    }
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) bf;

    String alias = "rods alias";
    try {
      cbf.getBean(alias);
      fail("Shouldn't permit factory get on normal bean");
    }
    catch (NoSuchBeanDefinitionException ex) {
      // Ok
      assertTrue(alias.equals(ex.getBeanName()));
    }

    // Create alias
    cbf.registerAlias("rod", alias);
    Object rod = getBeanFactory().getBean("rod");
    Object aliasRod = getBeanFactory().getBean(alias);
    assertTrue(rod == aliasRod);
  }
View Full Code Here

      resource = factory.getBean(name, element.lookupType);
      autowiredBeanNames = Collections.singleton(name);
    }

    if (factory instanceof ConfigurableBeanFactory) {
      ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
      for (String autowiredBeanName : autowiredBeanNames) {
        if (beanFactory.containsBean(autowiredBeanName)) {
          beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
        }
      }
    }

    return resource;
View Full Code Here

  @Override
  public void setBeanFactory(BeanFactory beanFactory) {
    if (!(beanFactory instanceof ConfigurableBeanFactory)) {
      throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
    }
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;

    this.scopedTargetSource.setBeanFactory(beanFactory);

    ProxyFactory pf = new ProxyFactory();
    pf.copyFrom(this);
    pf.setTargetSource(this.scopedTargetSource);

    Class<?> beanType = beanFactory.getType(this.targetBeanName);
    if (beanType == null) {
      throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName +
          "': Target type could not be determined at the time of proxy creation.");
    }
    if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
      pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));
    }

    // Add an introduction that implements only the methods on ScopedObject.
    ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
    pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));

    // Add the AopInfrastructureBean marker to indicate that the scoped proxy
    // itself is not subject to auto-proxying! Only its target bean is.
    pf.addInterface(AopInfrastructureBean.class);

    this.proxy = pf.getProxy(cbf.getBeanClassLoader());
  }
View Full Code Here

  /**
   * Resolves the specified interceptor names to Advisor objects.
   * @see #setInterceptorNames
   */
  private Advisor[] resolveInterceptorNames() {
    ConfigurableBeanFactory cbf = (this.beanFactory instanceof ConfigurableBeanFactory) ?
        (ConfigurableBeanFactory) this.beanFactory : null;
    List<Advisor> advisors = new ArrayList<Advisor>();
    for (String beanName : this.interceptorNames) {
      if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) {
        Object next = this.beanFactory.getBean(beanName);
        advisors.add(this.advisorAdapterRegistry.wrap(next));
      }
    }
    return advisors.toArray(new Advisor[advisors.size()]);
View Full Code Here

TOP

Related Classes of org.springframework.beans.factory.config.ConfigurableBeanFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.