Search in sources :

Example 41 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spock by spockframework.

the class SpringMockTestExecutionListener method beforeTestMethod.

@Override
public void beforeTestMethod(SpringTestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (!(testInstance instanceof Specification))
        return;
    Specification specification = (Specification) testInstance;
    ScanScopedBeans scanScopedBeans = ReflectionUtil.getAnnotationRecursive(specification.getClass(), ScanScopedBeans.class);
    Set<String> scopes = scanScopedBeans == null ? Collections.<String>emptySet() : new HashSet<>(Arrays.asList(scanScopedBeans.value()));
    ApplicationContext applicationContext = testContext.getApplicationContext();
    String[] mockBeanNames = applicationContext.getBeanDefinitionNames();
    List<Object> mockedBeans = new ArrayList<>();
    for (String beanName : mockBeanNames) {
        BeanDefinition beanDefinition = ((BeanDefinitionRegistry) applicationContext).getBeanDefinition(beanName);
        if (beanDefinition.isAbstract()) {
            continue;
        }
        if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)) {
            Object bean = applicationContext.getBean(beanName);
            if (mockUtil.isMock(bean)) {
                mockUtil.attachMock(bean, specification);
                mockedBeans.add(bean);
            }
        }
    }
    testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Specification(spock.lang.Specification) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 42 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project leopard by tanhaichao.

the class LeopardBeanFactoryAware method registerBean.

// @PreDestroy
// public void destroy() {
// beanFactory = null;
// }
/**
 * @desc 向spring容器注册bean
 *
 * @param beanName
 *
 * @param beanDefinition
 */
private static void registerBean(String beanName, BeanDefinition beanDefinition) {
    BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) beanFactory;
    beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 43 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project leopard by tanhaichao.

the class BeanDefinitionParserUtil method registerBeanDefinition.

// public static void registerBeanDefinition(String beanName, BeanDefinitionBuilder beanDefinitionBuilder) {
// registerBeanDefinition(LeopardBeanFactoryAware.getBeanFactory(), beanName, beanDefinitionBuilder);
// }
public static void registerBeanDefinition(BeanFactory beanFactory, String beanName, BeanDefinitionBuilder beanDefinitionBuilder) {
    BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) beanFactory;
    BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
    beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
    beanFactory.getBean(beanName);
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 44 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-cloud-config by spring-cloud.

the class CompositeEnvironmentBeanFactoryPostProcessor method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    List<String> typePropertyList = CompositeUtils.getCompositeTypeList(environment);
    for (int i = 0; i < typePropertyList.size(); i++) {
        String type = typePropertyList.get(i);
        String factoryName = CompositeUtils.getFactoryName(type, beanFactory);
        Type[] factoryTypes = CompositeUtils.getEnvironmentRepositoryFactoryTypeParams(beanFactory, factoryName);
        Class<? extends EnvironmentRepositoryProperties> propertiesClass = (Class<? extends EnvironmentRepositoryProperties>) factoryTypes[1];
        EnvironmentRepositoryProperties properties = bindProperties(i, propertiesClass, environment);
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(EnvironmentRepository.class).setFactoryMethodOnBean("build", factoryName).addConstructorArgValue(properties).getBeanDefinition();
        String beanName = String.format("%s-env-repo%d", type, i);
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        registry.registerBeanDefinition(beanName, beanDefinition);
    }
}
Also used : EnvironmentRepository(org.springframework.cloud.config.server.environment.EnvironmentRepository) Type(java.lang.reflect.Type) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) EnvironmentRepositoryProperties(org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 45 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project com.revolsys.open by revolsys.

the class ModuleImport method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof BeanDefinitionRegistry) {
        final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        postProcessBeanDefinitionRegistry(registry);
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Aggregations

BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)72 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)29 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)17 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)13 Test (org.junit.jupiter.api.Test)12 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)12 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)11 Map (java.util.Map)9 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)9 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)6 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)5 Test (org.junit.Test)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)4 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 HashMap (java.util.HashMap)3