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

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

Introduction

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

Prototype

@Override
public void setPrimary(boolean primary) 

Source Link

Document

Set whether this bean is a primary autowire candidate.

Usage

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   ww  w. j a  va2 s.c  o m
            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.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);/* w  w  w  . j a v  a  2  s. c o  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);
}