Java Code Examples for org.springframework.beans.factory.support.BeanDefinitionBuilder#genericBeanDefinition()
The following examples show how to use
org.springframework.beans.factory.support.BeanDefinitionBuilder#genericBeanDefinition() .
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: ScheduleModule.java From bulbasaur with Apache License 2.0 | 6 votes |
private void registerTrigger( String ownSign, String name, String jobDetailBeanName, String cronExpression, String triggerName ) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( CronTriggerFactoryBean.class); builder.addPropertyValue("name", name); builder.addPropertyValue("group", ownSign); builder.addPropertyValue("jobDetail", beanFactory.getBean(jobDetailBeanName)); builder.addPropertyValue("cronExpression", cronExpression); registry.registerBeanDefinition(triggerName, builder.getRawBeanDefinition()); }
Example 2
Source File: AnnotationDrivenBeanDefinitionParser.java From java-technology-stack with MIT License | 6 votes |
private void registerAsyncExecutionAspect(Element element, ParserContext parserContext) { if (!parserContext.getRegistry().containsBeanDefinition(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME)) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ASYNC_EXECUTION_ASPECT_CLASS_NAME); builder.setFactoryMethod("aspectOf"); String executor = element.getAttribute("executor"); if (StringUtils.hasText(executor)) { builder.addPropertyReference("executor", executor); } String exceptionHandler = element.getAttribute("exception-handler"); if (StringUtils.hasText(exceptionHandler)) { builder.addPropertyReference("exceptionHandler", exceptionHandler); } parserContext.registerBeanComponent(new BeanComponentDefinition(builder.getBeanDefinition(), TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME)); } }
Example 3
Source File: RegisterBean.java From spring-zk-test with Apache License 2.0 | 6 votes |
/** * 动态注册bean * * @author JohnGao */ public <T> void register(String beanName, Class<T> classType, Map<String, String> values) { ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) aContext; DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext .getBeanFactory(); if (defaultListableBeanFactory.isBeanNameInUse(beanName)) { defaultListableBeanFactory.removeBeanDefinition(beanName); logger.info("beanName-->" + beanName + "成功删除"); } BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder .genericBeanDefinition(classType); for (String key : values.keySet()) beanDefinitionBuilder.addPropertyValue(key, values.get(key)); defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getRawBeanDefinition()); logger.info("beanName-->" + beanName + "成功注册"); }
Example 4
Source File: AbstractApiClentRegistrar.java From onetwo with Apache License 2.0 | 6 votes |
/*** * @see RestApiClientConfiguration#apiClientRestExecutor() * @author wayshall * @param annotationMetadataHelper * @param registry */ @Deprecated protected void regiseterRestExecutor(AnnotationMetadataHelper annotationMetadataHelper, BeanDefinitionRegistry registry){ if(registry.containsBeanDefinition(RestExecutorFactory.REST_EXECUTOR_FACTORY_BEAN_NAME)){ return ; } Class<?> restExecutorFacotryClass = (Class<?>)annotationMetadataHelper.getAttributes().get(ATTRS_REST_EXECUTOR_FACTORY); // RestExecutorFactory factory = null; if(restExecutorFacotryClass==null || restExecutorFacotryClass==RestExecutorFactory.class){ restExecutorFacotryClass = DefaultRestExecutorFactory.class; } BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(restExecutorFacotryClass); definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); registry.registerBeanDefinition(RestExecutorFactory.REST_EXECUTOR_FACTORY_BEAN_NAME, definition.getBeanDefinition()); }
Example 5
Source File: NodeBeanRegistrationStrategy.java From eventeum with Apache License 2.0 | 6 votes |
private String registerNodeServicesBean(Node node, Web3j web3j, String web3jServiceBeanName, BeanDefinitionRegistry registry) { final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( NodeServices.class); builder.addPropertyValue("nodeName", node.getName()) .addPropertyValue("web3j", web3j) .addPropertyReference("blockchainService", web3jServiceBeanName); final String beanName = String.format(NODE_SERVICES_BEAN_NAME, node.getName()); registry.registerBeanDefinition(beanName, builder.getBeanDefinition()); return beanName; }
Example 6
Source File: CratePersistentEntitySchemaManagerBeanDefinitionParser.java From spring-data-crate with Apache License 2.0 | 6 votes |
@Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) { BeanComponentDefinitionBuilder helper = new BeanComponentDefinitionBuilder(element, context); String crateTemplateRef = element.getAttribute("crate-template-ref"); String exportOptionString = element.getAttribute("export-option"); BeanDefinitionBuilder schemaManagerBuilder = BeanDefinitionBuilder.genericBeanDefinition(CratePersistentEntitySchemaManager.class); schemaManagerBuilder.addConstructorArgReference(crateTemplateRef); schemaManagerBuilder.addConstructorArgValue(valueOf(exportOptionString)); setPropertyValue(schemaManagerBuilder, element, "ignoreFailures", "ignoreFailures"); schemaManagerBuilder.getBeanDefinition(); return (AbstractBeanDefinition)helper.getComponentIdButFallback(schemaManagerBuilder, SCHEMA_EXPORT_MANAGER) .getBeanDefinition(); }
Example 7
Source File: AbstractSingleBeanDefinitionParser.java From spring-analysis-note with MIT License | 5 votes |
/** * Creates a {@link BeanDefinitionBuilder} instance for the * {@link #getBeanClass bean Class} and passes it to the * {@link #doParse} strategy method. * @param element the element that is to be parsed into a single BeanDefinition * @param parserContext the object encapsulating the current state of the parsing process * @return the BeanDefinition resulting from the parsing of the supplied {@link Element} * @throws IllegalStateException if the bean {@link Class} returned from * {@link #getBeanClass(org.w3c.dom.Element)} is {@code null} * @see #doParse */ @Override protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(); String parentName = getParentName(element); if (parentName != null) { builder.getRawBeanDefinition().setParentName(parentName); } Class<?> beanClass = getBeanClass(element); if (beanClass != null) { builder.getRawBeanDefinition().setBeanClass(beanClass); } else { String beanClassName = getBeanClassName(element); if (beanClassName != null) { builder.getRawBeanDefinition().setBeanClassName(beanClassName); } } builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); BeanDefinition containingBd = parserContext.getContainingBeanDefinition(); if (containingBd != null) { // Inner bean definition must receive same scope as containing bean. builder.setScope(containingBd.getScope()); } if (parserContext.isDefaultLazyInit()) { // Default-lazy-init applies to custom bean definitions as well. builder.setLazyInit(true); } // 注释 3.11 在这里调用了我们写的解析方法 doParse(element, parserContext, builder); return builder.getBeanDefinition(); }
Example 8
Source File: Parsers.java From krpc with Apache License 2.0 | 5 votes |
void registerAsyncReferer(String beanName, String interfaceName, ParserContext parserContext) { //log.info("register referer "+interfaceName+", beanName="+beanName); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(RefererFactory.class); beanDefinitionBuilder.addConstructorArgValue(beanName); beanDefinitionBuilder.addConstructorArgValue(interfaceName); beanDefinitionBuilder.addDependsOn("rpcApp"); beanDefinitionBuilder.setLazyInit(true); parserContext.getRegistry().registerBeanDefinition(beanName, beanDefinitionBuilder.getRawBeanDefinition()); }
Example 9
Source File: ContextConfigurationUtils.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
public static void registerInstanceDataPropertySource(BeanDefinitionRegistry registry, String valueSeparator, String attributeSeparator) { BeanDefinitionBuilder builder = BeanDefinitionBuilder .genericBeanDefinition(POST_PROCESSOR_CLASS_NAME); builder.addPropertyValue("valueSeparator", valueSeparator); builder.addPropertyValue("attributeSeparator", attributeSeparator); registry.registerBeanDefinition(POST_PROCESSOR_BEAN_NAME, builder.getBeanDefinition()); }
Example 10
Source File: BeanInstantiationExceptionDemo.java From geekbang-lessons with Apache License 2.0 | 5 votes |
public static void main(String[] args) { // 创建 BeanFactory 容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // 注册 BeanDefinition Bean Class 是一个 CharSequence 接口 BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(CharSequence.class); applicationContext.registerBeanDefinition("errorBean", beanDefinitionBuilder.getBeanDefinition()); // 启动应用上下文 applicationContext.refresh(); // 关闭应用上下文 applicationContext.close(); }
Example 11
Source File: CustomBeanDefinitionRegistryPostProcessor.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { System.out.println("postProcessBeanDefinitionRegistry"); Class<?> cls = PersonBean.class; BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(cls); GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition(); definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE); definition.getPropertyValues().add("name", "pepsi02"); // 注册bean名,一般为类名首字母小写 registry.registerBeanDefinition("person2", definition); BeanDefinition beanDefinition = registry.getBeanDefinition("person1"); System.out.println("postProcessBeanDefinitionRegistry修改属性name值"); beanDefinition.getPropertyValues().add("name", "tom"); }
Example 12
Source File: ArchaiusConfigMapSourceRegistar.java From spring-cloud-kubernetes with Apache License 2.0 | 5 votes |
private void registerSourceConfiguration(BeanDefinitionRegistry registry, Object name, Object namespace) { BeanDefinitionBuilder configMapSourceConfigBuilder = BeanDefinitionBuilder.genericBeanDefinition(ArchaiusConfigMapSourceConfiguration.class); BeanDefinitionBuilder dynamicWatchedConfigBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicWatchedConfiguration.class); configMapSourceConfigBuilder.addConstructorArgReference(KUBERNETES_CLIENT_REF); configMapSourceConfigBuilder.addConstructorArgValue(name); configMapSourceConfigBuilder.addConstructorArgValue(namespace); String configMapSourceConfigName = name + CONFIG_MAP_SOURCE_SUFFIX; registry.registerBeanDefinition(configMapSourceConfigName, configMapSourceConfigBuilder.getBeanDefinition()); String dynamicWatchedConfigName = name + DYNAMIC_WATCH_CONFIG_SUFFIX; dynamicWatchedConfigBuilder.addConstructorArgReference(configMapSourceConfigName); registry.registerBeanDefinition(dynamicWatchedConfigName, dynamicWatchedConfigBuilder.getBeanDefinition()); }
Example 13
Source File: CmisRegistrar.java From spring-content with Apache License 2.0 | 5 votes |
BeanDefinition createTypeDefinition(Class<?> bd, Class<?> rbd, Class<?> sbd, AnnotationMetadata annotationMetadata) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CmisTypeDefinitionFactoryBean.class); builder.getRawBeanDefinition().setSource(annotationMetadata); try { builder.addPropertyValue("entityClass", bd); builder.addPropertyValue("repoClass", rbd); builder.addPropertyValue("storeClass", sbd); } catch (Throwable t) { } return builder.getBeanDefinition(); }
Example 14
Source File: ZmqOutboundGatewayParser.java From spring-integration-zmq with Apache License 2.0 | 5 votes |
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( "com.github.moonkev.spring.integration.zmq.ZmqOutboundGateway"); builder.addPropertyValue("address", element.getAttribute("address")); builder.addPropertyValue("socketType", element.getAttribute("socket-type")); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "bind"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout", "socketSendTimeout"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "receive-timeout", "socketReceiveTimeout"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "linger"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "context-manager"); if ("".equals(element.getAttribute("request-converter"))) { builder.addPropertyValue("requestConverter", new MapToJsonByteArrayConverter()); } else { builder.addPropertyReference("request-converter", element.getAttribute("requestConverter")); } if ("".equals(element.getAttribute("reply-converter"))) { builder.addPropertyValue("replyConverter", new JsonByteArrayToMapConverter()); } else { builder.addPropertyReference("reply-converter", element.getAttribute("replyConverter")); } return builder; }
Example 15
Source File: ScheduledTasksBeanDefinitionParser.java From spring-analysis-note with MIT License | 5 votes |
private RuntimeBeanReference cronTaskReference(String runnableBeanName, String cronExpression, Element taskElement, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( "org.springframework.scheduling.config.CronTask"); builder.addConstructorArgReference(runnableBeanName); builder.addConstructorArgValue(cronExpression); return beanReference(taskElement, parserContext, builder); }
Example 16
Source File: DefaultsBeanDefinitionParser.java From jdal with Apache License 2.0 | 5 votes |
/** * @param element * @param parserContext * @return */ @SuppressWarnings("unused") private ComponentDefinition registerControlInitializer(Element element, ParserContext parserContext) { BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition( AnnotationControlInitializer.class); bdb.addPropertyReference(PERSISTENT_SERVICE, PERSISTENT_SERVICE); BeanComponentDefinition bcd = new BeanComponentDefinition(bdb.getBeanDefinition(), CONTROL_INITIALIZER); registerBeanComponentDefinition(element, parserContext, bcd); return bcd; }
Example 17
Source File: LoadBalancerClientConfigurationRegistrar.java From spring-cloud-loadbalancer with Apache License 2.0 | 5 votes |
private static void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object configuration) { BeanDefinitionBuilder builder = BeanDefinitionBuilder .genericBeanDefinition(LoadBalancerClientSpecification.class); builder.addConstructorArgValue(name); builder.addConstructorArgValue(configuration); registry.registerBeanDefinition(name + ".LoadBalancerClientSpecification", builder.getBeanDefinition()); }
Example 18
Source File: AnnotationDrivenJmsBeanDefinitionParser.java From spring-analysis-note with MIT License | 4 votes |
@Override @Nullable public BeanDefinition parse(Element element, ParserContext parserContext) { Object source = parserContext.extractSource(element); // Register component for the surrounding <jms:annotation-driven> element. CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source); parserContext.pushContainingComponent(compDefinition); // Nest the concrete post-processor bean in the surrounding component. BeanDefinitionRegistry registry = parserContext.getRegistry(); if (registry.containsBeanDefinition(JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)) { parserContext.getReaderContext().error( "Only one JmsListenerAnnotationBeanPostProcessor may exist within the context.", source); } else { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( "org.springframework.jms.annotation.JmsListenerAnnotationBeanPostProcessor"); builder.getRawBeanDefinition().setSource(source); String endpointRegistry = element.getAttribute("registry"); if (StringUtils.hasText(endpointRegistry)) { builder.addPropertyReference("endpointRegistry", endpointRegistry); } else { registerDefaultEndpointRegistry(source, parserContext); } String containerFactory = element.getAttribute("container-factory"); if (StringUtils.hasText(containerFactory)) { builder.addPropertyValue("containerFactoryBeanName", containerFactory); } String handlerMethodFactory = element.getAttribute("handler-method-factory"); if (StringUtils.hasText(handlerMethodFactory)) { builder.addPropertyReference("messageHandlerMethodFactory", handlerMethodFactory); } registerInfrastructureBean(parserContext, builder, JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME); } // Finally register the composite component. parserContext.popAndRegisterContainingComponent(); return null; }
Example 19
Source File: JedisProviderFactoryBean.java From azeroth with Apache License 2.0 | 4 votes |
/** * */ private void registerRedisProvier() { String beanName = getGroup() + REDIS_PROVIDER_SUFFIX; if (context.containsBean(beanName)) { throw new RuntimeException("已包含group为[" + this.group + "]的缓存实例"); } String[] servers = StringUtils.tokenizeToStringArray(this.servers, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); //检查ip和port格式 for (String server : servers) { if (!pattern.matcher(server).matches()) { throw new RuntimeException("参数servers:" + this.servers + "错误"); } } Class<?> beanClass = null; if (JedisStandaloneProvider.MODE.equalsIgnoreCase(mode)) { beanClass = JedisStandaloneProvider.class; } else if (JedisClusterProvider.MODE.equalsIgnoreCase(mode)) { beanClass = JedisClusterProvider.class; } else if (JedisSentinelProvider.MODE.equalsIgnoreCase(mode)) { beanClass = JedisSentinelProvider.class; // Validate.notBlank(masterName, "Sentinel模式[masterName]参数 required"); } else { throw new RuntimeException("参数mode:" + this.mode + "不支持"); } DefaultListableBeanFactory acf = (DefaultListableBeanFactory) context .getAutowireCapableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder .genericBeanDefinition(beanClass); beanDefinitionBuilder.addConstructorArgValue(getGroup())// .addConstructorArgValue(jedisPoolConfig)// .addConstructorArgValue(servers)// .addConstructorArgValue(timeout);// if (JedisStandaloneProvider.MODE.equalsIgnoreCase(mode) || JedisSentinelProvider.MODE.equalsIgnoreCase(mode)) { beanDefinitionBuilder .addConstructorArgValue(org.apache.commons.lang3.StringUtils.trimToNull(password))// .addConstructorArgValue(database)// .addConstructorArgValue(clientName); } if (JedisSentinelProvider.MODE.equalsIgnoreCase(mode)) { beanDefinitionBuilder.addConstructorArgValue(masterName); } acf.registerBeanDefinition(beanName, beanDefinitionBuilder.getRawBeanDefinition()); // logger.info("register JedisProvider OK,Class:{},beanName:{}", beanClass.getSimpleName(), beanName); }
Example 20
Source File: DatasourceBeanDefinitionParser.java From compass with Apache License 2.0 | 4 votes |
/** * 继承prototype基类的属性配置 * @param targetDataSourceElement * @param dataSourcePrototypeAttributeValue * @param parserContext * @return */ private AbstractBeanDefinition parseSingleTargetDatasourceBeanDefinition( Element targetDataSourceElement, String dataSourcePrototypeAttributeValue, ParserContext parserContext) { BeanDefinitionBuilder targetDataSourceBeanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(); if(StringUtils.hasText(dataSourcePrototypeAttributeValue)) { targetDataSourceBeanDefinitionBuilder.setParentName(dataSourcePrototypeAttributeValue.trim()); } AbstractBeanDefinition targetDataSourceBeanDefinition = targetDataSourceBeanDefinitionBuilder.getBeanDefinition(); List<Element> propertyElements = DomUtils.getChildElementsByTagName(targetDataSourceElement, PROPERTY); NamedNodeMap attributes=targetDataSourceElement.getAttributes(); if (!CollectionUtils.isEmpty(propertyElements)) { for (Element propertyElement : propertyElements) { parserContext.getDelegate().parsePropertyElement(propertyElement, targetDataSourceBeanDefinition); } } if (attributes!=null && attributes.getLength() > 0) { for (int i=0;i<attributes.getLength();i++) { Node node=attributes.item(i); if (!(node instanceof Attr)) { continue; } Attr attr = (Attr) node; String attributeName = attr.getLocalName(); String attributeValue = attr.getValue(); MutablePropertyValues pvs = targetDataSourceBeanDefinition.getPropertyValues(); if (pvs.contains(attributeName)) { parserContext.getReaderContext().error("Property '" + attributeName + "' is already defined using " + "both <property> and inline syntax. Only one approach may be used per property.", attr); continue; } if (attributeName.endsWith(REF_SUFFIX)) { attributeName = attributeName.substring(0, attributeName.length() - REF_SUFFIX.length()); pvs.addPropertyValue(Conventions.attributeNameToPropertyName(attributeName), new RuntimeBeanReference(attributeValue)); } else { pvs.addPropertyValue(Conventions.attributeNameToPropertyName(attributeName), attributeValue); } } } return targetDataSourceBeanDefinition; }