Example usage for org.springframework.beans.factory.config BeanDefinition isLazyInit

List of usage examples for org.springframework.beans.factory.config BeanDefinition isLazyInit

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config BeanDefinition isLazyInit.

Prototype

boolean isLazyInit();

Source Link

Document

Return whether this bean should be lazily initialized, i.e.

Usage

From source file:me.springframework.di.spring.SpringConfigurationLoader.java

/**
 * Loads a {@link MutableInstance} from one of the {@link BeanDefinition}s
 * provided by the {@link BeanDefinitionRegistry} passed in.
 * /*from  ww  w .ja v a  2  s . c  o m*/
 * @param instance
 *            A {@link MutableInstance} to be populated.
 * @param definition
 *            A {@link BeanDefinition}, providing the meta data.
 */
private static void load(MutableInstance instance, BeanDefinition definition, MutableContext context) {
    instance.setReferencedType(definition.getBeanClassName());
    instance.setPrimitive(false);
    instance.setLazyInit(definition.isLazyInit());
    instance.setId("source" + counter++);
    instance.setFactoryMethod(definition.getFactoryMethodName());
    instance.setFactoryInstance(definition.getFactoryBeanName());
    if (ConfigurableBeanFactory.SCOPE_SINGLETON.equals(definition.getScope())) {
        instance.setScope(Scope.SINGLETON);
    }
    if (ConfigurableBeanFactory.SCOPE_PROTOTYPE.equals(definition.getScope())) {
        instance.setScope(Scope.PROTOTYPE);
    }
    if (definition instanceof AbstractBeanDefinition) {
        instance.setInitMethod(((AbstractBeanDefinition) definition).getInitMethodName());
        instance.setDestroyMethod(((AbstractBeanDefinition) definition).getDestroyMethodName());
    }
    if (!definition.getConstructorArgumentValues().isEmpty()) {
        List<MutableConstructorArgument> arguments = new ArrayList<MutableConstructorArgument>();
        for (Object object : definition.getConstructorArgumentValues().getGenericArgumentValues()) {
            MutableConstructorArgument argument = new MutableConstructorArgument(instance);
            argument.setInstance(instance);
            ValueHolder holder = (ValueHolder) object;
            argument.setSource(loadSource(context, argument, holder.getValue()));
            argument.setType(holder.getType());
            arguments.add(argument);
        }
        instance.setConstructorArguments(arguments);
    }
    Set<MutablePropertySetter> setters = new HashSet<MutablePropertySetter>();
    for (Object object : definition.getPropertyValues().getPropertyValueList()) {
        MutablePropertySetter setter = new MutablePropertySetter(instance);
        setter.setInstance(instance);
        PropertyValue value = (PropertyValue) object;
        setter.setName(value.getName());
        setter.setSource(loadSource(context, setter, value.getValue()));
        setters.add(setter);
    }
    instance.setSetters(setters);

    // added by woj
    instance.setAutowireCandidate(definition.isAutowireCandidate());
    if (definition instanceof AbstractBeanDefinition) {
        instance.setAutowireMode(((AbstractBeanDefinition) definition).getResolvedAutowireMode());
    } else {
        instance.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_NO);
    }
}

From source file:de.contentreich.instrumentation.SpringBeansHelper.java

public List<String[]> getSpringBeans(ApplicationContext appContext, String parentId) {
    logger.debug("Get spring beans for context " + parentId);
    ArrayList<String[]> beans = new ArrayList<String[]>();
    String[] names = appContext.getBeanDefinitionNames();
    for (int i = 0; i < names.length; i++) {
        String beanName = names[i];
        DefaultListableBeanFactory beanFactory = getBeanFactory(appContext);
        Class clazz = getBeanClass(beanName, beanFactory);
        // DefaultListableBeanFactory beanFactory = (appContext instanceof DefaultListableBeanFactory) ? (DefaultListableBeanFactory) appContext : null;
        if (clazz != null && beanFactory != null) { // Not abstract
            BeanDefinition def = beanFactory.getBeanDefinition(beanName);
            if (!(def.isPrototype() || def.isLazyInit())) {
                Object bean = appContext.getBean(beanName);
                if (holdsChildApplicationContext(bean)) {
                    // Semantically not perfect but ok ;)
                    clazz = getApplicationContext(bean).getClass();
                }/*from   w w w . j  av a2s .  com*/
            }
            String[] beanEntry = new String[] { beanName, clazz.getName(), parentId };
            beans.add(beanEntry);
        }
    }
    Collections.sort(beans, new Comparator() {
        @Override
        public int compare(Object o1, Object o2) {
            String s1 = ((String[]) o1)[0];
            String s2 = ((String[]) o2)[0];
            return s1.compareTo(s2);
        }

    });
    logger.debug("Got " + beans.size() + " spring beans");
    return beans;
}

From source file:org.eclipse.gemini.blueprint.service.exporter.support.OsgiServiceFactoryBean.java

public void afterPropertiesSet() throws Exception {
    Assert.notNull(bundleContext, "required property 'bundleContext' has not been set");

    hasNamedBean = StringUtils.hasText(targetBeanName);

    Assert.isTrue(hasNamedBean || target != null,
            "Either 'targetBeanName' or 'target' properties have to be set.");

    // if we have a name, we need a bean factory
    if (hasNamedBean) {
        Assert.notNull(beanFactory, "Required property 'beanFactory' has not been set.");
    }//  ww  w.  ja va2s.  c  om

    // initialize bean only when dealing with singletons and named beans
    if (hasNamedBean) {
        Assert.isTrue(beanFactory.containsBean(targetBeanName),
                "Cannot locate bean named '" + targetBeanName + "' inside the running bean factory.");

        if (beanFactory.isSingleton(targetBeanName)) {
            if (beanFactory instanceof ConfigurableListableBeanFactory) {
                ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) beanFactory;
                BeanDefinition definition = clbf.getBeanDefinition(targetBeanName);
                if (!definition.isLazyInit()) {
                    target = beanFactory.getBean(targetBeanName);
                    targetClass = target.getClass();
                }
            }
        }

        if (targetClass == null) {
            // lazily get the target class
            targetClass = beanFactory.getType(targetBeanName);
        }

        // when running inside a container, add the dependency between this bean and the target one
        addBeanFactoryDependency();
    } else {
        targetClass = target.getClass();
    }

    if (propertiesResolver == null) {
        propertiesResolver = new BeanNameServicePropertiesResolver();
        ((BeanNameServicePropertiesResolver) propertiesResolver).setBundleContext(bundleContext);
    }

    // sanity check
    if (interfaces == null) {
        if (DefaultInterfaceDetector.DISABLED.equals(interfaceDetector))
            throw new IllegalArgumentException(
                    "No service interface(s) specified and auto-export discovery disabled; change at least one of these properties.");
        interfaces = new Class[0];
    }
    // check visibility type
    else {
        if (!ServiceFactory.class.isAssignableFrom(targetClass)) {
            for (int interfaceIndex = 0; interfaceIndex < interfaces.length; interfaceIndex++) {
                Class<?> intf = interfaces[interfaceIndex];
                Assert.isAssignable(intf, targetClass,
                        "Exported service object does not implement the given interface: ");
            }
        }
    }
    // check service properties listener
    if (serviceProperties instanceof ServicePropertiesListenerManager) {
        propertiesListener = new PropertiesMonitor();
        ((ServicePropertiesListenerManager) serviceProperties).addListener(propertiesListener);
    }

    boolean shouldRegisterAtStartup;
    synchronized (lock) {
        shouldRegisterAtStartup = registerAtStartup;
    }

    resolver = new LazyTargetResolver(target, beanFactory, targetBeanName, cacheTarget, getNotifier(),
            getLazyListeners());

    if (shouldRegisterAtStartup) {
        registerService();
    }
}

From source file:org.impalaframework.spring.ProxyCreatingBeanFactory.java

@Override
public void preInstantiateSingletons() throws BeansException {
    if (logger.isInfoEnabled()) {
        logger.info("Pre-instantiating singletons in factory [" + this + "]");
    }/*from   w  w w .  j  ava2 s. co  m*/

    String[] beanNames = this.getBeanDefinitionNames();

    for (int i = 0; i < beanNames.length; i++) {
        String beanName = beanNames[i];
        if (!containsSingleton(beanName) && containsBeanDefinition(beanName)) {

            BeanDefinition bd = getMergedBeanDefinition(beanName);
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {

                if (bd instanceof RootBeanDefinition) {
                    RootBeanDefinition rootBeanDefinition = (RootBeanDefinition) bd;

                    Class<?> beanClass = resolveBeanClass(rootBeanDefinition, beanName);
                    if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
                        getBean(FACTORY_BEAN_PREFIX + beanName);
                    } else {
                        getBean(beanName);
                    }
                } else {
                    log.warn("Unable to instantiate bean definition " + bd + " as this is not an instance of "
                            + RootBeanDefinition.class.getName());
                }
            }
        }
    }
}

From source file:org.springframework.flex.config.RemotingAnnotationPostProcessor.java

/**
 * Helper that searches the BeanFactory for beans annotated with @RemotingDestination, being careful not to force
 * eager creation of the beans if it can be avoided.
 * //ww w  .  j av  a  2  s.  c  o  m
 * @param beanFactory the BeanFactory to search
 * @return a set of collected RemotingDestinationMetadata
 */
private Set<RemotingDestinationMetadata> findRemotingDestinations(ConfigurableListableBeanFactory beanFactory) {
    Set<RemotingDestinationMetadata> remotingDestinations = new HashSet<RemotingDestinationMetadata>();
    Set<String> beanNames = new HashSet<String>();
    beanNames.addAll(Arrays.asList(beanFactory.getBeanDefinitionNames()));
    if (beanFactory.getParentBeanFactory() instanceof ListableBeanFactory) {
        beanNames.addAll(Arrays
                .asList(((ListableBeanFactory) beanFactory.getParentBeanFactory()).getBeanDefinitionNames()));
    }
    for (String beanName : beanNames) {
        if (beanName.startsWith("scopedTarget.")) {
            continue;
        }
        RemotingDestination remotingDestination = null;
        BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
        if (bd.isAbstract() || bd.isLazyInit()) {
            continue;
        }
        if (bd instanceof AbstractBeanDefinition) {
            AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
            if (abd.hasBeanClass()) {
                Class<?> beanClass = abd.getBeanClass();
                remotingDestination = AnnotationUtils.findAnnotation(beanClass, RemotingDestination.class);
                if (remotingDestination != null) {
                    remotingDestinations
                            .add(new RemotingDestinationMetadata(remotingDestination, beanName, beanClass));
                    continue;
                }
            }
        }
        Class<?> handlerType = beanFactory.getType(beanName);
        if (handlerType != null) {
            remotingDestination = AnnotationUtils.findAnnotation(handlerType, RemotingDestination.class);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Could not get type of bean '" + beanName + "' from bean factory.");
            }
        }
        if (remotingDestination != null) {
            remotingDestinations
                    .add(new RemotingDestinationMetadata(remotingDestination, beanName, handlerType));
        }
    }
    return remotingDestinations;
}

From source file:org.springframework.jmx.export.MBeanExporter.java

/**
 * Return whether the specified bean definition should be considered as lazy-init.
 * @param beanFactory the bean factory that is supposed to contain the bean definition
 * @param beanName the name of the bean to check
 * @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory#getBeanDefinition
 * @see org.springframework.beans.factory.config.BeanDefinition#isLazyInit
 *///  www.java 2  s . c  om
protected boolean isBeanDefinitionLazyInit(ListableBeanFactory beanFactory, String beanName) {
    if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
        return false;
    }
    try {
        BeanDefinition bd = ((ConfigurableListableBeanFactory) beanFactory).getBeanDefinition(beanName);
        return bd.isLazyInit();
    } catch (NoSuchBeanDefinitionException ex) {
        // Probably a directly registered singleton.
        return false;
    }
}