Example usage for org.springframework.beans.factory.support GenericBeanDefinition setAutowireCandidate

List of usage examples for org.springframework.beans.factory.support GenericBeanDefinition setAutowireCandidate

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support GenericBeanDefinition setAutowireCandidate.

Prototype

@Override
public void setAutowireCandidate(boolean autowireCandidate) 

Source Link

Document

Set whether this bean is a candidate for getting autowired into some other bean.

Usage

From source file:co.paralleluniverse.common.spring.SpringContainerHelper.java

public static BeanDefinition defineBean(Class<?> clazz, ConstructorArgumentValues constructorArgs,
        MutablePropertyValues properties) {
    GenericBeanDefinition bean = new GenericBeanDefinition();
    bean.setBeanClass(clazz);/*from  w w w.  j  av  a  2s .  c  om*/
    bean.setAutowireCandidate(true);
    bean.setConstructorArgumentValues(constructorArgs);
    bean.setPropertyValues(properties);

    return bean;
}

From source file:org.jacp.project.launcher.SpringLauncher.java

@Override
public synchronized <T> T registerAndGetBean(Class<? extends T> type, final String id, final Scope scope) {
    if (this.factory.containsBean(id))
        return getBean(type);
    final AutowireCapableBeanFactory factory = getContext().getAutowireCapableBeanFactory();
    final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(type);//  www  . j  av  a  2  s  .c o  m
    beanDefinition.setScope(scope.getType());
    beanDefinition.setAutowireCandidate(true);
    registry.registerBeanDefinition(id, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    return getBean(type);
}

From source file:org.jacpfx.spring.launcher.SpringXmlConfigLauncher.java

@Override
public synchronized <T> T registerAndGetBean(Class<? extends T> type, final String id, final Scope scope) {
    if (contains(id))
        return getBean(id);
    final AutowireCapableBeanFactory factory = getContext().getAutowireCapableBeanFactory();
    final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(type);//from  w  w  w .  j  a  v a  2 s  .c  om
    if (scope != null)
        beanDefinition.setScope(scope.getType());
    beanDefinition.setAutowireCandidate(true);
    registry.registerBeanDefinition(id, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    return getBean(id);
}

From source file:org.jacpfx.spring.launcher.SpringJavaConfigLauncher.java

@Override
public <P> P registerAndGetBean(Class<? extends P> type, String id, Scope scope) {
    if (contains(id))
        return getBean(id);
    final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(type);//w w  w  .  j  a v  a 2s.co  m
    if (scope != null)
        beanDefinition.setScope(scope.getType());
    beanDefinition.setAutowireCandidate(true);
    lock.writeLock().lock();
    try {
        final AutowireCapableBeanFactory factory = getContext().getAutowireCapableBeanFactory();
        final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
        registry.registerBeanDefinition(id, beanDefinition);
        factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    } finally {
        lock.writeLock().unlock();
    }
    return getBean(id);
}

From source file:com.techtrip.spring.beans.factory.ContextAwareBeanFactory.java

/**
 * Register bean.//from w  w w .j a v  a  2 s.com
 *
 * @param beanToRegister the bean to register
 * @param beanName the bean name
 * @param scope the scope
 * @param setLazyInit the set lazy init
 * @param setAutowireCandidate the set autowire candidate
 */
public void registerBean(Class<?> beanToRegister, String beanName, String scope /* "session" */,
        boolean setLazyInit, boolean setAutowireCandidate) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(beanToRegister);
    beanDefinition.setLazyInit(setLazyInit);
    beanDefinition.setAbstract(false);
    beanDefinition.setAutowireCandidate(setAutowireCandidate);
    beanDefinition.setScope(scope);

    registry.registerBeanDefinition(beanName, beanDefinition);
}

From source file:com.fitbur.jestify.junit.spring.IntegrationTestReifier.java

@Override
public Object reifyCut(CutDescriptor cutDescriptor, Object[] arguments) {
    return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        try {//from   w  ww  .  ja v  a2  s .com
            Cut cut = cutDescriptor.getCut();
            Field field = cutDescriptor.getField();
            Type fieldType = field.getGenericType();
            String fieldName = field.getName();

            field.setAccessible(true);

            Constructor<?> constructor = cutDescriptor.getConstructor();
            constructor.setAccessible(true);

            ResolvableType resolver = ResolvableType.forType(fieldType);

            Class rawType;

            if (resolver.hasGenerics()) {
                if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) {
                    rawType = resolver.getRawClass();
                } else {
                    rawType = resolver.resolve();
                }
            } else {
                rawType = resolver.resolve();
            }

            Module module = testInstance.getClass().getDeclaredAnnotation(Module.class);

            GenericBeanDefinition bean = new GenericBeanDefinition();
            bean.setBeanClass(rawType);
            bean.setAutowireCandidate(false);
            bean.setScope(SCOPE_PROTOTYPE);
            bean.setPrimary(true);
            bean.setLazyInit(true);
            bean.setRole(RootBeanDefinition.ROLE_APPLICATION);
            bean.setAutowireMode(RootBeanDefinition.AUTOWIRE_NO);
            appContext.registerBeanDefinition(fieldName, bean);
            if (module != null) {
                appContext.register(module.value());
            }
            appContext.refresh();

            Object instance = appContext.getBean(fieldName, arguments);

            if (cut.value()) {
                instance = spy(instance);
            }

            field.set(testInstance, instance);

            return instance;
        } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) {
            throw new RuntimeException(e);
        }
    });
}

From source file:com.rvantwisk.cnctools.ScreensConfiguration.java

/**
 * Add's a new bean to Spring's context/*from   w ww  .  j  a v a  2  s . c  o  m*/
 *
 * @param name
 */
public void registerBean(final String name) {
    if (registeredbeans.containsKey(name)) {
        return;
    }
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClassName(name);
    beanDefinition.setAutowireCandidate(true);
    AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    registry.registerBeanDefinition(name, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    registeredbeans.put(name, Boolean.TRUE);
}

From source file:com.fitbur.testify.di.spring.SpringServiceLocator.java

@Override
public void addService(ServiceDescriptor descriptor) {
    checkState(!context.containsBeanDefinition(descriptor.getName()),
            "Service with the name '%s' already exists.", descriptor.getName());

    GenericBeanDefinition bean = new GenericBeanDefinition();
    bean.setBeanClass(descriptor.getType());
    bean.setAutowireCandidate(descriptor.getDiscoverable());
    bean.setPrimary(descriptor.getPrimary());
    bean.setLazyInit(descriptor.getLazy());
    bean.setRole(ROLE_APPLICATION);//from   w w w . j ava  2 s .  co m

    if (descriptor.getInjectable()) {
        bean.setAutowireMode(AUTOWIRE_CONSTRUCTOR);
    } else {
        bean.setAutowireMode(AUTOWIRE_NO);
        ConstructorArgumentValues values = new ConstructorArgumentValues();

        Object[] arguments = descriptor.getArguments();
        for (int i = 0; i < arguments.length; i++) {
            Object arg = arguments[i];

            if (arg == null) {
                //TODO: warn user that the argument was not specified and there
                //for the real instance will be injected.
                continue;
            }

            values.addIndexedArgumentValue(i, arg);
        }

        bean.setConstructorArgumentValues(values);
    }

    ServiceScope scope = descriptor.getScope();
    switch (scope) {
    case PROTOTYPE:
        bean.setScope("prototype");
        break;
    case SINGLETON:
        bean.setScope("singleton");
        break;
    case REQUEST:
        bean.setScope("request");
        break;
    case SESSION:
        bean.setScope("session");
        break;
    case APPLICATION:
        bean.setScope("application");
        break;
    default:
        checkState(false, "Scope '{}' is not supported by Spring IoC.", scope.name());

    }
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
    beanFactory.registerBeanDefinition(descriptor.getName(), bean);
}

From source file:com.dianping.avatar.cache.spring.CacheBeanDefinitionParser.java

/**
 * Register {@link CacheService} definition.
 * DefaultCacheServiceProxy delegates the DefaultCacheService on behalf of the cache hit-rate statistics.
 *//*from  w  w  w . j  av  a 2s . co m*/
protected GenericBeanDefinition initCacheServiceDefinition(Element element,
        BeanDefinitionRegistry beanDefinitionRegistry) {
    GenericBeanDefinition cacheDefinition = new GenericBeanDefinition();
    cacheDefinition.setBeanClass(DefaultCacheService.class);
    cacheDefinition.setAutowireCandidate(false);
    //JDK proxy not available, as the objects are managed by the Spring IOC
    /*        DefaultCacheService cacheServiceProxy = (DefaultCacheService)Proxy.newProxyInstance(DefaultCacheService.class.getClassLoader(),new Class<?>[]{DefaultCacheService.class}, new CacheServiceInvokerHandler());
            definition.setBeanClass(cacheServiceProxy.getClass());*/

    cacheServiceId = element.getAttribute(CACHE_SERVICE_ID_ATTR);

    if (!StringUtils.hasText(cacheServiceId)) {
        cacheServiceId = DEFAULT_CACHE_SERVICE_ID;
    }

    // Add reference to CacheFactory
    ConstructorArgumentValues constructorArgumentValues = cacheDefinition.getConstructorArgumentValues();

    String cacheClientFactoryId = element.getAttribute(CACHE_CLIENT_FACTORY_ID_ATTR);
    cacheItemConfigManager = element.getAttribute(CACHE_ITEM_MANAGER_ID_ATTR);
    if (!StringUtils.hasText(cacheClientFactoryId) || !StringUtils.hasText(cacheItemConfigManager)) {
        registerCacheRelatedWebService(beanDefinitionRegistry);
    }

    if (!StringUtils.hasText(cacheClientFactoryId)) {
        cacheClientFactoryId = DEFAULT_CACHE_CLIENT_FACTORY_ID;
        // Register default cache client factory
        registerDefaultCacheClientFactory(beanDefinitionRegistry);
    }
    constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference(cacheClientFactoryId));
    constructorArgumentValues
            .addGenericArgumentValue(new RuntimeBeanReference(ONEWAY_CACHE_MANAGE_WEB_SERVICE_ID));

    if (!StringUtils.hasText(cacheItemConfigManager)) {
        cacheItemConfigManager = DEFAULT_ITEM_CONFIG_MANAGER_ID;
        // Register default cache item config manager
        registerDefaultCacheItemConfigManager(beanDefinitionRegistry);
    }
    constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference(cacheItemConfigManager));

    return cacheDefinition;

    /*        BeanDefinitionHolder holder = new BeanDefinitionHolder(cacheDefinition, this.cacheServiceId);
            
            BeanDefinitionReaderUtils.registerBeanDefinition(holder, beanDefinitionRegistry);*/
}