Java Code Examples for org.springframework.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition()

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition() . 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: NacosBeanDefinitionRegistrar.java    From nacos-spring-project with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
		BeanDefinitionRegistry registry) {
	BeanDefinition annotationProcessor = BeanDefinitionBuilder
			.genericBeanDefinition(PropertySourcesPlaceholderConfigurer.class)
			.getBeanDefinition();
	registry.registerBeanDefinition(
			PropertySourcesPlaceholderConfigurer.class.getName(),
			annotationProcessor);

	AnnotationAttributes attributes = AnnotationAttributes
			.fromMap(importingClassMetadata
					.getAnnotationAttributes(EnableNacos.class.getName()));

	// Register Global Nacos Properties Bean
	registerGlobalNacosProperties(attributes, registry, environment,
			GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
	// Register Nacos Annotation Beans
	registerNacosAnnotationBeans(registry);
	// Invoke NacosPropertySourcePostProcessor immediately
	// in order to enhance the precedence of @NacosPropertySource process
	invokeNacosPropertySourcePostProcessor(beanFactory);
}
 
Example 2
Source File: EnableMenuImportSelector.java    From spring-backend-boilerplate with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
	AnnotationAttributes enableMenu = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(EnableMenu.class
																											.getName(),
																									false));

	if (enableMenu != null) {
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DefaultMenuPlugin.class);
		AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();

		MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
		mutablePropertyValues.add("extensionPointId", enableMenu.getString("extensionPointId"));
		mutablePropertyValues.add("pluginId", enableMenu.getString("pluginId"));
		mutablePropertyValues.add("menu", toMenu(enableMenu.getAnnotationArray("menu")));
		beanDefinition.setPropertyValues(mutablePropertyValues);

		registry.registerBeanDefinition("menuPlugin:" + enableMenu.getString("pluginId"), beanDefinition);
	}
}
 
Example 3
Source File: DDDBeanRegistryPostProcessor.java    From flower with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
  Set<String> serviceTypes = new HashSet<>();
  Class<?> services[] =
      new Class<?>[] {CommandHandlerService.class, EventHandlerService.class, QueryHandlerService.class,
          DDDStartService.class, DDDEndService.class, DefaultCommandGateway.class, DefaultQueryGateway.class};

  for (Class<?> service : services) {
    BeanDefinition beanDefinition = new RootBeanDefinition(service);
    String handlerServiceName = BeanDefinitionReaderUtils.generateBeanName(beanDefinition, registry);
    registry.registerBeanDefinition(handlerServiceName, beanDefinition);
    if (Service.class.isAssignableFrom(service)) {
      serviceTypes.add(service.getName());
    }
  }

  final String dddFactoryName = "dddFactory";
  if (registry.containsBeanDefinition(dddFactoryName)) {
    BeanDefinition dddFactoryBeanDefinition = registry.getBeanDefinition(dddFactoryName);
    dddFactoryBeanDefinition.getPropertyValues().add("serviceTypes", serviceTypes);
  }

}
 
Example 4
Source File: AbstractCacheProviderFacadeParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Parses the specified XML element which contains the properties of the
 * <code>{@link org.springmodules.cache.provider.CacheProviderFacade}</code>
 * to register in the given registry of bean definitions.
 * 
 * @param element
 *          the XML element to parse
 * @param parserContext
 *          the parser context
 * @throws IllegalStateException
 *           if the value of the property <code>serializableFactory</code>
 *           is not equal to "NONE" or "XSTREAM"
 * 
 * @see BeanDefinitionParser#parse(Element, ParserContext)
 */
public final BeanDefinition parse(Element element, ParserContext parserContext)
    throws IllegalStateException {
  String id = element.getAttribute("id");

  // create the cache provider facade
  Class clazz = getCacheProviderFacadeClass();
  MutablePropertyValues propertyValues = new MutablePropertyValues();
  RootBeanDefinition cacheProviderFacade = new RootBeanDefinition(clazz,
      propertyValues);
  propertyValues.addPropertyValue(parseFailQuietlyEnabledProperty(element));
  propertyValues.addPropertyValue(parseSerializableFactoryProperty(element));

  BeanDefinitionRegistry registry = parserContext.getRegistry();
  registry.registerBeanDefinition(id, cacheProviderFacade);

  doParse(id, element, registry);
  return null;
}
 
Example 5
Source File: NettyRpcClientBeanDefinitionRegistrar.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
private void registerNettyRpcClient(AnnotatedBeanDefinition beanDefinition,BeanDefinitionRegistry registry)  {
    AnnotationMetadata metadata = beanDefinition.getMetadata();
    Map<String, Object> nettyRpcClientAttributes = metadata.getAnnotationAttributes(nettyRpcClientCanonicalName);
    Map<String, Object> lazyAttributes = metadata.getAnnotationAttributes(lazyCanonicalName);

    Class<?> beanClass;
    try {
        beanClass = ClassUtils.forName(metadata.getClassName(), classLoader);
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException("NettyRpcClientsRegistrar failure! notfound class",e);
    }

    String serviceName = resolve((String) nettyRpcClientAttributes.get("serviceName"));
    beanDefinition.setLazyInit(lazyAttributes == null || Boolean.TRUE.equals(lazyAttributes.get("value")));
    ((AbstractBeanDefinition)beanDefinition).setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    ((AbstractBeanDefinition)beanDefinition).setInstanceSupplier(newInstanceSupplier(beanClass,serviceName,(int)nettyRpcClientAttributes.get("timeout")));

    String beanName = generateBeanName(beanDefinition.getBeanClassName());
    registry.registerBeanDefinition(beanName,beanDefinition);
}
 
Example 6
Source File: TopologyBeanDefinitionParser.java    From breeze with Apache License 2.0 6 votes vote down vote up
private static BeanDefinition
define(BeanDefinitionBuilder builder, Element element, BeanDefinitionRegistry registry) {
	builder.setScope("prototype");

	builder.addConstructorArgValue(element.getAttribute("beanType"));
	builder.addConstructorArgValue(element.getAttribute("signature"));
	builder.addConstructorArgValue(tokenize(element.getAttribute("outputFields")));
	builder.addPropertyValue("parallelism", Integer.valueOf(element.getAttribute("parallelism")));
	builder.addPropertyValue("scatterOutput", Boolean.valueOf(element.getAttribute("scatterOutput")));

	Map<String,String> outputBinding = new HashMap<>();
	for (Element field : getChildElementsByTagName(element, "field"))
		outputBinding.put(field.getAttribute("name"), field.getAttribute("expression"));
	builder.addPropertyValue("outputBinding", outputBinding);

	AbstractBeanDefinition definition = builder.getBeanDefinition();

	String id = element.getAttribute(ID_ATTRIBUTE);
	if (hasText(id)) {
		builder.addPropertyValue("id", id);
		registry.registerBeanDefinition(id, definition);
	}

	return definition;
}
 
Example 7
Source File: SingletonTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Bean
public static BeanDefinitionRegistryPostProcessor processor() {
	return new BeanDefinitionRegistryPostProcessor() {

		@Override
		public void postProcessBeanFactory(
				ConfigurableListableBeanFactory beanFactory)
				throws BeansException {
		}

		@Override
		public void postProcessBeanDefinitionRegistry(
				BeanDefinitionRegistry registry) throws BeansException {
			// Simulates what happens when you add a compiled function
			RootBeanDefinition beanDefinition = new RootBeanDefinition(
					MySupplier.class);
			registry.registerBeanDefinition("words", beanDefinition);
		}
	};
}
 
Example 8
Source File: AnnotationConfigUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static BeanDefinitionHolder registerPostProcessor(
		BeanDefinitionRegistry registry, RootBeanDefinition definition, String beanName) {

	definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	registry.registerBeanDefinition(beanName, definition);
	return new BeanDefinitionHolder(definition, beanName);
}
 
Example 9
Source File: StatefulFactory.java    From statefulj with Apache License 2.0 5 votes vote down vote up
private String registerFSM(
		ReferenceFactory referenceFactory,
		Class<?> statefulControllerClass,
		StatefulController scAnnotation,
		String persisterId,
		Class<?> managedClass,
		String finderId,
		Class<? extends Annotation> idAnnotationType,
		BeanDefinitionRegistry reg) {
	int retryAttempts = scAnnotation.retryAttempts();
	int retryInterval = scAnnotation.retryInterval();

	String fsmBeanId = referenceFactory.getFSMId();
	BeanDefinition fsmBean = BeanDefinitionBuilder
			.genericBeanDefinition(FSM.class)
			.getBeanDefinition();
	ConstructorArgumentValues args = fsmBean.getConstructorArgumentValues();
	args.addIndexedArgumentValue(0, fsmBeanId);
	args.addIndexedArgumentValue(1, new RuntimeBeanReference(persisterId));
	args.addIndexedArgumentValue(2, retryAttempts);
	args.addIndexedArgumentValue(3, retryInterval);
	args.addIndexedArgumentValue(4, managedClass);
	args.addIndexedArgumentValue(5, idAnnotationType);
	args.addIndexedArgumentValue(6, this.appContext);

	if (finderId != null) {
		args.addIndexedArgumentValue(7, new RuntimeBeanReference(finderId));
	}

	reg.registerBeanDefinition(fsmBeanId, fsmBean);
	return fsmBeanId;
}
 
Example 10
Source File: AlfrescoRestRegistrar.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
private void processDispatcherWebscript(AnnotationAttributes webscriptAttributes, BeanDefinitionRegistry registry) {
	String webscript = webscriptAttributes.getString("name");
	Assert.hasText(webscript, "Webscript name cannot be empty!");

	Class<?> servletContext = webscriptAttributes.getClass("servletContext");

	ServletConfigOptions[] servletConfigOptions = (ServletConfigOptions[]) webscriptAttributes
			.get("servletConfigOptions");
	Class<? extends WebApplicationContext> servletContextClass = webscriptAttributes
			.getClass("servletContextClass");
	HttpMethod[] httpMethods = (HttpMethod[]) webscriptAttributes.get("httpMethods");
	boolean inheritGlobalProperties = (Boolean) webscriptAttributes.get("inheritGlobalProperties");

	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(DispatcherWebscript.class);

	DispatcherWebscript ws = new DispatcherWebscript(webscript, inheritGlobalProperties);
	ws.setContextClass(servletContextClass);
	ws.setContextConfigLocation(servletContext.getName());
	ws.addServletConfigOptions(servletConfigOptions);
	beanDefinition.setInstanceSupplier(() -> ws);
	beanDefinition.setRole(BeanDefinition.ROLE_APPLICATION);

	registry.registerBeanDefinition(webscript, beanDefinition);

	for (HttpMethod httpMethod : httpMethods) {
		registry.registerAlias(webscript, getWebscriptName(webscript, httpMethod));
	}
}
 
Example 11
Source File: MyImportBeanDefinitionRegistrar.java    From SpringAll with MIT License 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    final String beanName = "strawberry";
    boolean contain = registry.containsBeanDefinition(beanName);
    if (!contain) {
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Strawberry.class);
        registry.registerBeanDefinition(beanName, rootBeanDefinition);
    }
}
 
Example 12
Source File: StatefulFactory.java    From statefulj with Apache License 2.0 5 votes vote down vote up
private String registerBinderBean(
		String key,
		ReferenceFactory referenceFactory,
		Class<?> binderClass,
		BeanDefinitionRegistry reg) {
	BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(binderClass)
			.getBeanDefinition();
	String binderId = referenceFactory.getBinderId(key);
	reg.registerBeanDefinition(binderId, def);
	return binderId;
}
 
Example 13
Source File: CustomChatDefinitionRegistrar.java    From netty-chat with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry){
    Class beanClass = CustomOfflineInfoHelper.class;
    RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
    String beanName = StringUtils.uncapitalize(beanClass.getSimpleName());
    //在这里可以拿到所有注解的信息,可以根据不同注解来返回不同的class,从而达到开启不同功能的目的
    //通过这种方式可以自定义beanName
    registry.registerBeanDefinition(beanName, beanDefinition);
}
 
Example 14
Source File: DefaultsBeanDefinitionParser.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * @param registry
 * @param bcd
 */
private void registerBeanComponentDefinition(Element element, ParserContext parserContext, BeanComponentDefinition bcd) {
	parserContext.getDelegate().parseBeanDefinitionAttributes(element, bcd.getBeanName(), null, 
			(AbstractBeanDefinition) bcd.getBeanDefinition());
	BeanDefinitionRegistry registry = parserContext.getRegistry();
	registry.registerBeanDefinition(bcd.getBeanName(), bcd.getBeanDefinition());
}
 
Example 15
Source File: AnnotationBeanDefinitionDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void registerUserBeanDefinition(BeanDefinitionRegistry registry, String beanName) {
    BeanDefinitionBuilder beanDefinitionBuilder = genericBeanDefinition(User.class);
    beanDefinitionBuilder
            .addPropertyValue("id", 1L)
            .addPropertyValue("name", "小马哥");

    // 判断如果 beanName 参数存在时
    if (StringUtils.hasText(beanName)) {
        // 注册 BeanDefinition
        registry.registerBeanDefinition(beanName, beanDefinitionBuilder.getBeanDefinition());
    } else {
        // 非命名 Bean 注册方法
        BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinitionBuilder.getBeanDefinition(), registry);
    }
}
 
Example 16
Source File: AnnotationsParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Registers a <code>{@link AnnotationFlushingAttributeSource}</code> and
 * adds it as a property of the flushing interceptor.
 * 
 * @param propertyValues
 *          the set of properties of the caching interceptor
 * @param registry
 *          the registry of bean definitions
 * 
 * @see AbstractMetadataAttributesParser#configureFlushingInterceptor(MutablePropertyValues,
 *      BeanDefinitionRegistry)
 */
@Override
protected void configureFlushingInterceptor(
    MutablePropertyValues propertyValues, BeanDefinitionRegistry registry) {

  String beanName = AnnotationFlushingAttributeSource.class.getName();

  registry.registerBeanDefinition(beanName, new RootBeanDefinition(
      AnnotationFlushingAttributeSource.class));

  propertyValues.addPropertyValue("flushingAttributeSource",
      new RuntimeBeanReference(beanName));
}
 
Example 17
Source File: SmartSwaggerDynamicGroupConfig.java    From smart-admin with MIT License 5 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    this.groupBuild();
    for (Map.Entry<String, List<String>> entry : groupMap.entrySet()) {
        String group = entry.getKey();
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Docket.class, this :: baseDocket);
        BeanDefinition beanDefinition = builder.getRawBeanDefinition();
        registry.registerBeanDefinition(group + "Api", beanDefinition);
    }
}
 
Example 18
Source File: AbstractRetrofitClientsRegistrar.java    From spring-cloud-square with Apache License 2.0 5 votes vote down vote up
private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name,
										 Object configuration) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder
			.genericBeanDefinition(RetrofitClientSpecification.class);
	builder.addConstructorArgValue(name);
	builder.addConstructorArgValue(configuration);
	registry.registerBeanDefinition(
			name + "." + RetrofitClientSpecification.class.getSimpleName(),
			builder.getBeanDefinition());
}
 
Example 19
Source File: ReyClientBeanRegistrar.java    From x7 with Apache License 2.0 4 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {

    String startClassName = annotationMetadata.getClassName();
    String basePackage = startClassName.substring(0, startClassName.lastIndexOf("."));

    Set<Class<?>> set = ClassFileReader.getClasses(basePackage);

    Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(EnableReyClient.class.getName());
    Object obj = attributes.get("basePackages");
    if (obj != null){
        String[] strs = (String[]) obj;
        for (String str : strs){
            Set<Class<?>> set1 = ClassFileReader.getClasses(str);
            set.addAll(set1);
        }
    }

    List<String> beanNameList = new ArrayList<>();

    for (Class clz : set) {
        ReyClient annotation = (ReyClient)clz.getAnnotation(ReyClient.class);
        if (annotation == null)
            continue;

        ClientParser.parse(clz,environment);

        String beanName = clz.getName();
        beanNameList.add(beanName);

        String backend = annotation.circuitBreaker();
        if (backend.equals(" ")){
            backend = null;
        }

        boolean retry = annotation.retry();

        if (!registry.containsBeanDefinition(beanName)) {

            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clz);
            GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
            definition.getPropertyValues().add("objectType", clz);
            definition.getPropertyValues().add("backend",backend);
            definition.getPropertyValues().add("retry",retry);
            definition.setBeanClass(HttpClientProxy.class);
            definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);

            registry.registerBeanDefinition(beanName, definition);

        }
    }
}
 
Example 20
Source File: SerializableBeanFactoryMemoryLeakTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void registerMisconfiguredBeanDefinition(BeanDefinitionRegistry registry) {
	registry.registerBeanDefinition("misconfigured",
		rootBeanDefinition(Object.class).addPropertyValue("nonexistent", "bogus")
			.getBeanDefinition());
}