Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setBeanClass()

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setBeanClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericScope.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
		throws BeansException {
	for (String name : registry.getBeanDefinitionNames()) {
		BeanDefinition definition = registry.getBeanDefinition(name);
		if (definition instanceof RootBeanDefinition) {
			RootBeanDefinition root = (RootBeanDefinition) definition;
			if (root.getDecoratedDefinition() != null && root.hasBeanClass()
					&& root.getBeanClass() == ScopedProxyFactoryBean.class) {
				if (getName().equals(root.getDecoratedDefinition().getBeanDefinition()
						.getScope())) {
					root.setBeanClass(LockedScopedProxyFactoryBean.class);
					root.getConstructorArgumentValues().addGenericArgumentValue(this);
					// surprising that a scoped proxy bean definition is not already
					// marked as synthetic?
					root.setSynthetic(true);
				}
			}
		}
	}
}
 
Example 2
Source File: BaymaxBeanDefinitionParser.java    From baymax with Apache License 2.0 6 votes vote down vote up
/**
 * ID处理
 * 如果标签上没有定义name 则自动生成name
 * @param element
 * @param parserContext
 */
private Pair<RootBeanDefinition, String/*name*/> fixName(Element element, ParserContext parserContext, Class beanClass){
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(beanClass);
    beanDefinition.setLazyInit(false);

    String name = element.getAttribute("name");

    synchronized (BaymaxBeanDefinitionParser.class){
        if (name == null || name.length() == 0) {
            name = "Baymax-" + beanClass.getSimpleName();
            while (parserContext.getRegistry().containsBeanDefinition(name)) {
                name += (counter++);
            }
        }
        if (name != null && name.length() > 0) {
            if (parserContext.getRegistry().containsBeanDefinition(name)) {
                throw new IllegalStateException("Duplicate spring bean id " + name);
            }
            parserContext.getRegistry().registerBeanDefinition(name, beanDefinition);
        }
    }
    return new Pair<RootBeanDefinition, String>(beanDefinition, name);
}
 
Example 3
Source File: LensBootstrap.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public LensJLineShellComponent getLensJLineShellComponent() {
  GenericApplicationContext ctx = (GenericApplicationContext) getApplicationContext();
  RootBeanDefinition rbd = new RootBeanDefinition();
  rbd.setBeanClass(LensJLineShellComponent.class);
  DefaultListableBeanFactory bf = (DefaultListableBeanFactory) ctx.getBeanFactory();
  bf.registerBeanDefinition(LensJLineShellComponent.class.getSimpleName(), rbd);
  return ctx.getBean(LensJLineShellComponent.class);
}
 
Example 4
Source File: CustomNamespaceHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
	RootBeanDefinition definition = new RootBeanDefinition();
	definition.setBeanClass(TestBean.class);

	MutablePropertyValues mpvs = new MutablePropertyValues();
	mpvs.add("name", element.getAttribute("name"));
	mpvs.add("age", element.getAttribute("age"));
	definition.setPropertyValues(mpvs);

	parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), definition);
	return null;
}
 
Example 5
Source File: InitializingFacade.java    From dubbo-plus with Apache License 2.0 5 votes vote down vote up
private void registerSpringBean(Class<?> type){
    RootBeanDefinition definition = new RootBeanDefinition();
    definition.setBeanClass(ClientFactory.class);
    definition.setScope(BeanDefinition.SCOPE_SINGLETON);
    definition.setLazyInit(false);
    definition.getPropertyValues().addPropertyValue("type",type);
    beanFactory.registerBeanDefinition(type.getName(),definition);
}
 
Example 6
Source File: BootShim.java    From spring-data-dev-tools with Apache License 2.0 5 votes vote down vote up
protected void createAndRegisterBeanDefinition(ConfigurableApplicationContext annctx, Class<?> clazz, String name) {
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setBeanClass(clazz);
	DefaultListableBeanFactory bf = (DefaultListableBeanFactory) annctx.getBeanFactory();
	if (name != null) {
		bf.registerBeanDefinition(name, rbd);
	} else {
		bf.registerBeanDefinition(clazz.getSimpleName(), rbd);
	}
}
 
Example 7
Source File: JupiterBeanDefinitionParser.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private BeanDefinition parseJupiterProvider(Element element, ParserContext parserContext) {
    RootBeanDefinition def = new RootBeanDefinition();
    def.setBeanClass(beanClass);

    addPropertyReference(def, element, "server", true);
    addPropertyReference(def, element, "providerImpl", true);

    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item instanceof Element) {
            String localName = item.getLocalName();
            if ("property".equals(localName)) {
                addProperty(def, (Element) item, "weight", false);
                addPropertyReferenceArray(
                        def,
                        (Element) item,
                        ProviderInterceptor.class.getName(),
                        "providerInterceptors",
                        false);
                addPropertyReference(def, (Element) item, "executor", false);
                addPropertyReference(def, (Element) item, "flowController", false);
                addPropertyReference(def, (Element) item, "providerInitializer", false);
                addPropertyReference(def, (Element) item, "providerInitializerExecutor", false);
            }
        }
    }

    return registerBean(def, element, parserContext);
}
 
Example 8
Source File: RemoteDirectorySpringInitiator.java    From adam with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nonnull
protected BeanDefinition extend(@Nonnull BeanDefinition original, @Nonnull Class<? extends RemoteDirectory> newDirectoryType) throws Exception {
    final RootBeanDefinition result = new RootBeanDefinition();
    result.overrideFrom(original);
    result.setBeanClass(newDirectoryType);
    final ConstructorArgumentValues constructor = result.getConstructorArgumentValues();
    if (!constructor.getGenericArgumentValues().isEmpty()) {
        constructor.addGenericArgumentValue(_directoryHelper);
    } else {
        final int newIndex = constructor.getIndexedArgumentValues().size();
        constructor.addIndexedArgumentValue(newIndex, _directoryHelper);
    }
    return result;
}
 
Example 9
Source File: CustomNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
	RootBeanDefinition definition = new RootBeanDefinition();
	definition.setBeanClass(TestBean.class);

	MutablePropertyValues mpvs = new MutablePropertyValues();
	mpvs.add("name", element.getAttribute("name"));
	mpvs.add("age", element.getAttribute("age"));
	definition.setPropertyValues(mpvs);

	parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), definition);

	return null;
}
 
Example 10
Source File: EmbeddedPostgresContextCustomizerFactory.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
@Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
    context.addBeanFactoryPostProcessor(new EnvironmentPostProcessor(context.getEnvironment()));

    BeanDefinitionRegistry registry = getBeanDefinitionRegistry(context);
    RootBeanDefinition registrarDefinition = new RootBeanDefinition();

    registrarDefinition.setBeanClass(PreloadableEmbeddedPostgresRegistrar.class);
    registrarDefinition.getConstructorArgumentValues()
            .addIndexedArgumentValue(0, databaseAnnotations);

    registry.registerBeanDefinition("preloadableEmbeddedPostgresRegistrar", registrarDefinition);
}
 
Example 11
Source File: BootShim.java    From hdfs-shell with Apache License 2.0 5 votes vote down vote up
private void createAndRegisterBeanDefinition(ConfigurableApplicationContext annctx, Class<?> clazz, String name) {
    RootBeanDefinition rbd = new RootBeanDefinition();
    rbd.setBeanClass(clazz);
    DefaultListableBeanFactory bf = (DefaultListableBeanFactory) annctx.getBeanFactory();
    if (name != null) {
        bf.registerBeanDefinition(name, rbd);
    } else {
        bf.registerBeanDefinition(clazz.getSimpleName(), rbd);
    }

}
 
Example 12
Source File: BindPointAspectRegistar.java    From GyJdbc with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    if (!inited) {
        RootBeanDefinition root = new RootBeanDefinition();
        root.setBeanClass(BindPointAspect.class);
        registry.registerBeanDefinition(BindPointAspect.class.getName(), root);
        inited = true;
    }
}
 
Example 13
Source File: CustomNamespaceHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
	RootBeanDefinition definition = new RootBeanDefinition();
	definition.setBeanClass(TestBean.class);

	MutablePropertyValues mpvs = new MutablePropertyValues();
	mpvs.add("name", element.getAttribute("name"));
	mpvs.add("age", element.getAttribute("age"));
	definition.setPropertyValues(mpvs);

	parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), definition);
	return null;
}
 
Example 14
Source File: AnnotationDrivenBeanDefinitionParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void registerTransactionalEventListenerFactory(ParserContext parserContext) {
	RootBeanDefinition def = new RootBeanDefinition();
	def.setBeanClass(TransactionalEventListenerFactory.class);
	parserContext.registerBeanComponent(new BeanComponentDefinition(def,
			TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example 15
Source File: AnnotationDrivenBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void registerTransactionalEventListenerFactory(ParserContext parserContext) {
	RootBeanDefinition def = new RootBeanDefinition();
	def.setBeanClass(TransactionalEventListenerFactory.class);
	parserContext.registerBeanComponent(new BeanComponentDefinition(def,
			TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example 16
Source File: TarsBeanDefinitionParser.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(clssze);
    String id = null;
    id = element.getAttribute("name");
    if (id == null || id.length() == 0) {
        if (clssze == ServantConfig.class) {
            throw new TarsSpringConfigException("Messing servant name");
        } else if (clssze == ListenerConfig.class) {
            id = "listener";

            int counter = 0;
            while (parserContext.getRegistry().containsBeanDefinition(id + counter)) {
                counter++;
            }

            id = id + counter;
        }
    }

    if (id != null && id.length() > 0) {
        if (parserContext.getRegistry().containsBeanDefinition(id)) {
            throw new TarsSpringConfigException("Duplicate spring bean id " + id);
        }
        parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
    }

    NamedNodeMap nnm = element.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Node node = nnm.item(i);
        String key = node.getLocalName();
        String value = node.getNodeValue();
        if (key.equals("entity")) {
            if (parserContext.getRegistry().containsBeanDefinition(value)) {
                beanDefinition.getPropertyValues().add(key, parserContext.getRegistry().getBeanDefinition(value));
            } else {
                beanDefinition.getPropertyValues().add(key, new RuntimeBeanReference(value));
            }
        } else {
            beanDefinition.getPropertyValues().add(key, value);
        }
    }

    return beanDefinition;
}
 
Example 17
Source File: AbstractBeanDefinitionParser.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public BeanDefinition parse(final Element element, final ParserContext context) {
    RootBeanDefinition definition = new RootBeanDefinition();
    definition.setBeanClass(beanClass);
    definition.setLazyInit(false);
    String id = element.getAttribute("id");

    if (requireId) {
        if (isEmpty(id)) {
            throw new IllegalConfigureException("spring.bean", id, "do not set", ExceptionCode.COMMON_VALUE_ILLEGAL);
        } else {
            if (context.getRegistry().containsBeanDefinition(id)) {
                throw new IllegalConfigureException("spring.bean", id, "duplicate spring bean id", ExceptionCode.COMMON_VALUE_ILLEGAL);
            }
            context.getRegistry().registerBeanDefinition(id, definition);
        }
    }
    //set各个属性值
    String methodName;
    String property;
    CustomParser parser;
    List<Method> methods = getPublicMethod(beanClass);
    Map<String, CustomParser> parserMap = new HashMap<>(methods.size());
    for (Method setter : methods) {
        //略过不是property的方法
        if (!isSetter(setter)) {
            continue;
        }
        methodName = setter.getName();
        property = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
        parser = parsers.get(property);
        if (parser != null) {
            parserMap.put(property, parser);
        } else {
            Alias alias = setter.getAnnotation(Alias.class);
            //对象属性对应的xml中的attribute名称
            if (alias != null && !StringUtils.isEmpty(alias.value())) {
                parserMap.put(alias.value(), new AliasParser(property));
            } else {
                //判断是否已经设置了别名
                parserMap.putIfAbsent(property, new AliasParser(property));
            }
        }
    }
    parserMap.forEach((a, p) -> p.parse(definition, id, element, a, context));

    return definition;
}
 
Example 18
Source File: JupiterBeanDefinitionParser.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
private BeanDefinition parseJupiterConsumer(Element element, ParserContext parserContext) {
    RootBeanDefinition def = new RootBeanDefinition();
    def.setBeanClass(beanClass);

    addPropertyReference(def, element, "client", true);
    addProperty(def, element, "interfaceClass", true);

    List<MethodSpecialConfig> methodSpecialConfigs = Lists.newArrayList();

    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item instanceof Element) {
            String localName = item.getLocalName();
            if ("property".equals(localName)) {
                addProperty(def, (Element) item, "version", false);
                addProperty(def, (Element) item, "serializerType", false);
                addProperty(def, (Element) item, "loadBalancerType", false);
                addProperty(def, (Element) item, "extLoadBalancerName", false);
                addProperty(def, (Element) item, "waitForAvailableTimeoutMillis", false);
                addProperty(def, (Element) item, "invokeType", false);
                addProperty(def, (Element) item, "dispatchType", false);
                addProperty(def, (Element) item, "timeoutMillis", false);
                addProperty(def, (Element) item, "providerAddresses", false);
                addProperty(def, (Element) item, "clusterStrategy", false);
                addProperty(def, (Element) item, "failoverRetries", false);
                addPropertyReferenceArray(
                        def,
                        (Element) item,
                        ConsumerInterceptor.class.getName(),
                        "consumerInterceptors",
                        false);
            } else if ("methodSpecials".equals(localName)) {
                NodeList configList = item.getChildNodes();
                for (int j = 0; j < configList.getLength(); j++) {
                    Node configItem = configList.item(j);
                    if (configItem instanceof Element) {
                        if ("methodSpecial".equals(configItem.getLocalName())) {
                            String methodName = ((Element) configItem).getAttribute("methodName");
                            String timeoutMillis = ((Element) configItem).getAttribute("timeoutMillis");
                            String clusterStrategy = ((Element) configItem).getAttribute("clusterStrategy");
                            String failoverRetries = ((Element) configItem).getAttribute("failoverRetries");

                            MethodSpecialConfig config = MethodSpecialConfig.of(methodName)
                                    .timeoutMillis(Long.parseLong(timeoutMillis))
                                    .strategy(ClusterStrategyConfig.of(clusterStrategy, failoverRetries));
                            methodSpecialConfigs.add(config);
                        }
                    }
                }
            }
        }
    }

    if (!methodSpecialConfigs.isEmpty()) {
        def.getPropertyValues().addPropertyValue("methodSpecialConfigs", methodSpecialConfigs);
    }

    return registerBean(def, element, parserContext);
}
 
Example 19
Source File: AnnotationDrivenBeanDefinitionParser.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void registerTransactionalEventListenerFactory(ParserContext parserContext) {
	RootBeanDefinition def = new RootBeanDefinition();
	def.setBeanClass(TransactionalEventListenerFactory.class);
	parserContext.registerBeanComponent(new BeanComponentDefinition(def,
			TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example 20
Source File: AnnotationDrivenBeanDefinitionParser.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void registerTransactionalEventListenerFactory(ParserContext parserContext) {
	RootBeanDefinition def = new RootBeanDefinition();
	def.setBeanClass(TransactionalEventListenerFactory.class);
	parserContext.registerBeanComponent(new BeanComponentDefinition(def,
			TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME));
}