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

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

Introduction

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

Prototype

public GenericBeanDefinition() 

Source Link

Document

Create a new GenericBeanDefinition, to be configured through its bean properties and configuration methods.

Usage

From source file:org.bytesoft.bytejta.supports.dubbo.TransactionConfigPostProcessor.java

public void initializeForConsumer(ConfigurableListableBeanFactory beanFactory, String application,
        String targetBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    // <dubbo:reference id="yyy"
    // interface="org.bytesoft.bytejta.supports.wire.RemoteCoordinator"
    // timeout="6000" group="org.bytesoft.bytejta" loadbalance="transaction" cluster="failfast" />
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ReferenceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("timeout", "6000");
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "transaction");
    mpv.addPropertyValue("group", "org.bytesoft.bytejta");
    mpv.addPropertyValue("check", "false");

    String stubBeanId = String.format("stub@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(stubBeanId, beanDef);

    // <bean id="xxx" class="org.bytesoft.bytejta.supports.dubbo.TransactionBeanRegistry"
    // factory-method="getInstance">
    // <property name="consumeCoordinator" ref="yyy" />
    // </bean>
    BeanDefinition targetBeanDef = beanFactory.getBeanDefinition(targetBeanName);
    MutablePropertyValues targetMpv = targetBeanDef.getPropertyValues();
    targetMpv.addPropertyValue("consumeCoordinator", new RuntimeBeanReference(stubBeanId));
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableConfigPostProcessor.java

public void initializeForProvider(ConfigurableListableBeanFactory beanFactory, String application,
        String refBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    // <dubbo:service interface="org.bytesoft.bytejta.supports.wire.RemoteCoordinator"
    // ref="dispatcherCoordinator" group="org.bytesoft.bytetcc" loadbalance="compensable" cluster="failfast" />
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ServiceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("ref", new RuntimeBeanReference(refBeanName));
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "compensable");
    mpv.addPropertyValue("group", "org.bytesoft.bytetcc");
    mpv.addPropertyValue("retries", "0");
    mpv.addPropertyValue("timeout", String.valueOf(1000L * 6));

    String skeletonBeanId = String.format("skeleton@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(skeletonBeanId, beanDef);
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableConfigPostProcessor.java

public void initializeForConsumer(ConfigurableListableBeanFactory beanFactory, String application,
        String targetBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    // <dubbo:reference id="yyy"
    // interface="org.bytesoft.bytejta.supports.wire.RemoteCoordinator"
    // timeout="3000" group="org.bytesoft.bytetcc" loadbalance="compensable" cluster="failfast" />
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ReferenceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("timeout", "3000");
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "compensable");
    mpv.addPropertyValue("group", "org.bytesoft.bytetcc");

    String stubBeanId = String.format("stub@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(stubBeanId, beanDef);

    // <bean id="xxx" class="org.bytesoft.bytetcc.supports.dubbo.CompensableBeanRegistry"
    // factory-method="getInstance">
    // <property name="consumeCoordinator" ref="yyy" />
    // </bean>
    BeanDefinition targetBeanDef = beanFactory.getBeanDefinition(targetBeanName);
    MutablePropertyValues targetMpv = targetBeanDef.getPropertyValues();
    targetMpv.addPropertyValue("consumeCoordinator", new RuntimeBeanReference(stubBeanId));
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableCoordinatorProcessor.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    String application = null;// w w  w  .  ja va2 s  .c  om
    String[] appBeanNameArray = beanFactory.getBeanNamesForType(ApplicationConfig.class);
    if (appBeanNameArray != null && appBeanNameArray.length == 1) {
        String beanName = appBeanNameArray[0];
        BeanDefinition beanDef = registry.getBeanDefinition(beanName);
        MutablePropertyValues values = beanDef.getPropertyValues();
        String propertyName = "name";
        PropertyValue pv = values.getPropertyValue(propertyName);
        application = pv == null ? null : (String) pv.getValue();
    }

    if (StringUtils.isBlank(application)) {
        throw new FatalBeanException(
                "There is no application name specified, or there is more than one application name!");
    }

    String[] coordinatorNameArray = beanFactory.getBeanNamesForType(TransactionCoordinator.class);
    if (coordinatorNameArray != null && coordinatorNameArray.length == 1) {
        String beanName = coordinatorNameArray[0];

        GenericBeanDefinition beanDef = new GenericBeanDefinition();
        beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ServiceBean.class);
        MutablePropertyValues values = beanDef.getPropertyValues();
        values.addPropertyValue("group", application);
        values.addPropertyValue("interface", TransactionCoordinator.class.getName());
        values.addPropertyValue("ref", new RuntimeBeanReference(beanName));
        values.addPropertyValue("retries", "0");
        values.addPropertyValue("timeout", String.valueOf(1000L * 6));
        registry.registerBeanDefinition(String.format("%s@%s", beanName, application), beanDef);
    } else {
        throw new FatalBeanException("No available(or redundant) org.bytesoft.bytetcc.TransactionCoordinator!");
    }

}

From source file:org.eclipse.gemini.blueprint.extender.internal.blueprint.activator.BlueprintContainerProcessor.java

public void preProcessRefresh(final ConfigurableOsgiBundleApplicationContext context) {
    final BundleContext bundleContext = context.getBundleContext();
    // create the ModuleContext adapter
    final BlueprintContainer blueprintContainer = createBlueprintContainer(context);

    // 1. add event listeners
    // add service publisher
    context.addApplicationListener(new BlueprintContainerServicePublisher(blueprintContainer, bundleContext));
    // add waiting event broadcaster
    context.addApplicationListener(new BlueprintWaitingEventDispatcher(context.getBundleContext()));

    // 2. add environmental managers
    context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

        private static final String BLUEPRINT_BUNDLE = "blueprintBundle";
        private static final String BLUEPRINT_BUNDLE_CONTEXT = "blueprintBundleContext";
        private static final String BLUEPRINT_CONTAINER = "blueprintContainer";
        private static final String BLUEPRINT_EXTENDER = "blueprintExtenderBundle";
        private static final String BLUEPRINT_CONVERTER = "blueprintConverter";

        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            // lazy logger evaluation
            Log logger = LogFactory.getLog(context.getClass());

            if (!(beanFactory instanceof BeanDefinitionRegistry)) {
                logger.warn("Environmental beans will be registered as singletons instead "
                        + "of usual bean definitions since beanFactory " + beanFactory
                        + " is not a BeanDefinitionRegistry");
            }/*from  www  . j  av a 2s  .  c  om*/

            // add blueprint container bean
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_BUNDLE, bundleContext.getBundle(), logger);
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_BUNDLE_CONTEXT, bundleContext, logger);
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_CONTAINER, blueprintContainer, logger);
            // addPredefinedBlueprintBean(beanFactory, BLUEPRINT_EXTENDER, extenderBundle, logger);
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_CONVERTER,
                    new SpringBlueprintConverter(beanFactory), logger);

            // add Blueprint conversion service
            // String[] beans = beanFactory.getBeanNamesForType(BlueprintConverterConfigurer.class, false, false);
            // if (ObjectUtils.isEmpty(beans)) {
            // beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar());
            // }
            beanFactory.setConversionService(
                    new SpringBlueprintConverterService(beanFactory.getConversionService(), beanFactory));
        }

        private void addPredefinedBlueprintBean(ConfigurableListableBeanFactory beanFactory, String beanName,
                Object value, Log logger) {
            if (!beanFactory.containsLocalBean(beanName)) {
                logger.debug("Registering pre-defined bean named " + beanName);
                if (beanFactory instanceof BeanDefinitionRegistry) {
                    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

                    GenericBeanDefinition def = new GenericBeanDefinition();
                    def.setBeanClass(ENV_FB_CLASS);
                    ConstructorArgumentValues cav = new ConstructorArgumentValues();
                    cav.addIndexedArgumentValue(0, value);
                    def.setConstructorArgumentValues(cav);
                    def.setLazyInit(false);
                    def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
                    registry.registerBeanDefinition(beanName, def);

                } else {
                    beanFactory.registerSingleton(beanName, value);
                }

            } else {
                logger.warn("A bean named " + beanName
                        + " already exists; aborting registration of the predefined value...");
            }
        }
    });

    // 3. add cycle breaker
    context.addBeanFactoryPostProcessor(cycleBreaker);

    BlueprintEvent creatingEvent = new BlueprintEvent(BlueprintEvent.CREATING, context.getBundle(),
            extenderBundle);
    listenerManager.blueprintEvent(creatingEvent);
    dispatcher.beforeRefresh(creatingEvent);
}

From source file:org.flockdata.spring.xml.ClientBeanDefinitionParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    // When node is not null, we should build a client.
    // When node is null, we want to build a transport client.

    String id = XMLParserUtil.getElementStringValue(element, "id");

    String properties = XMLParserUtil.getElementStringValue(element, "properties");

    BeanDefinition client;/*from   w  w w . ja v  a2 s .  co  m*/

    GenericBeanDefinition bdef = new GenericBeanDefinition();

    bdef.setBeanClass(FlockDataClientFactoryBean.class);
    BeanDefinitionBuilder clientBuilder = startClientBuilder(FlockDataClientFactoryBean.class, properties);
    client = ClientBeanDefinitionParser.buildClientDef(clientBuilder);

    parserContext.getRegistry().registerBeanDefinition(id, client);

    return bdef;
}

From source file:org.hx.rainbow.server.oc.monitor.service.MonitorService.java

private GenericBeanDefinition getDefinition(String serverName, String url, String userName, String password) {
    GenericBeanDefinition sessionFactoryDef = new GenericBeanDefinition();
    Map<String, Object> paramData = new HashMap<String, Object>();
    paramData.put("driverClassName", "com.mysql.jdbc.Driver");
    paramData.put("url", url);
    paramData.put("username", userName);
    paramData.put("password", password);
    paramData.put("initialSize", 5);
    paramData.put("maxActive", 20);
    paramData.put("maxIdle", 10);
    sessionFactoryDef.setBeanClass(BasicDataSource.class);
    sessionFactoryDef.setPropertyValues(new MutablePropertyValues(paramData));
    return sessionFactoryDef;
}

From source file:org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader.java

/**
 * Define an inner bean definition.// www.j a  va 2 s  . c  o m
 * @param type the bean type
 * @return the bean definition
 */
public GenericBeanDefinition bean(Class<?> type) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(type);
    return beanDefinition;
}

From source file:org.springframework.boot.autoconfigure.AutoConfigurationPackages.java

/**
 * Programmatically registers the auto-configuration package names. Subsequent
 * invocations will add the given package names to those that have already been
 * registered. You can use this method to manually define the base packages that will
 * be used for a given {@link BeanDefinitionRegistry}. Generally it's recommended that
 * you don't call this method directly, but instead rely on the default convention
 * where the package name is set from your {@code @EnableAutoConfiguration}
 * configuration class or classes.//from  w  ww  .j  av  a 2 s. c  om
 * @param registry the bean definition registry
 * @param packageNames the package names to set
 */
public static void register(BeanDefinitionRegistry registry, String... packageNames) {
    if (registry.containsBeanDefinition(BEAN)) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN);
        ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues();
        constructorArguments.addIndexedArgumentValue(0, addBasePackages(constructorArguments, packageNames));
    } else {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(BasePackages.class);
        beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, packageNames);
        beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(BEAN, beanDefinition);
    }
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void genericApplicationContext() throws Exception {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    ac.getBeanFactory().registerScope("myScope", new Scope() {
        @Override//  www . java2  s .co  m
        public Object get(String name, ObjectFactory<?> objectFactory) {
            return objectFactory.getObject();
        }

        @Override
        public Object remove(String name) {
            return null;
        }

        @Override
        public void registerDestructionCallback(String name, Runnable callback) {
        }

        @Override
        public Object resolveContextualObject(String key) {
            if (key.equals("mySpecialAttr")) {
                return "42";
            } else {
                return null;
            }
        }

        @Override
        public String getConversationId() {
            return null;
        }
    });

    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties placeholders = new Properties();
    placeholders.setProperty("code", "123");
    ppc.setProperties(placeholders);
    ac.addBeanFactoryPostProcessor(ppc);

    GenericBeanDefinition bd0 = new GenericBeanDefinition();
    bd0.setBeanClass(TestBean.class);
    bd0.getPropertyValues().add("name", "myName");
    bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original"));
    ac.registerBeanDefinition("tb0", bd0);

    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClassName("#{tb0.class}");
    bd1.setScope("myScope");
    bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ");
    bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}");
    ac.registerBeanDefinition("tb1", bd1);

    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClassName("#{tb1.class.name}");
    bd2.setScope("myScope");
    bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
    bd2.getPropertyValues().add("age", "#{mySpecialAttr}");
    bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}");
    ac.registerBeanDefinition("tb2", bd2);

    GenericBeanDefinition bd3 = new GenericBeanDefinition();
    bd3.setBeanClass(ValueTestBean.class);
    bd3.setScope("myScope");
    ac.registerBeanDefinition("tb3", bd3);

    GenericBeanDefinition bd4 = new GenericBeanDefinition();
    bd4.setBeanClass(ConstructorValueTestBean.class);
    bd4.setScope("myScope");
    ac.registerBeanDefinition("tb4", bd4);

    GenericBeanDefinition bd5 = new GenericBeanDefinition();
    bd5.setBeanClass(MethodValueTestBean.class);
    bd5.setScope("myScope");
    ac.registerBeanDefinition("tb5", bd5);

    GenericBeanDefinition bd6 = new GenericBeanDefinition();
    bd6.setBeanClass(PropertyValueTestBean.class);
    bd6.setScope("myScope");
    ac.registerBeanDefinition("tb6", bd6);

    System.getProperties().put("country", "UK");
    try {
        ac.refresh();

        TestBean tb0 = ac.getBean("tb0", TestBean.class);

        TestBean tb1 = ac.getBean("tb1", TestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb1.getName());
        assertEquals(42, tb1.getAge());

        TestBean tb2 = ac.getBean("tb2", TestBean.class);
        assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
        assertEquals(42, tb2.getAge());
        assertEquals("123 UK", tb2.getCountry());

        ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
        assertEquals(42, tb3.age);
        assertEquals(42, tb3.ageFactory.getObject().intValue());
        assertEquals("123 UK", tb3.country);
        assertEquals("123 UK", tb3.countryFactory.getObject());
        System.getProperties().put("country", "US");
        assertEquals("123 UK", tb3.country);
        assertEquals("123 US", tb3.countryFactory.getObject());
        System.getProperties().put("country", "UK");
        assertEquals("123 UK", tb3.country);
        assertEquals("123 UK", tb3.countryFactory.getObject());
        assertSame(tb0, tb3.tb);

        tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3);
        assertEquals("123 UK", tb3.countryFactory.getObject());

        ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
        assertEquals(42, tb4.age);
        assertEquals("123 UK", tb4.country);
        assertSame(tb0, tb4.tb);

        MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
        assertEquals(42, tb5.age);
        assertEquals("123 UK", tb5.country);
        assertSame(tb0, tb5.tb);

        PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
        assertEquals(42, tb6.age);
        assertEquals("123 UK", tb6.country);
        assertSame(tb0, tb6.tb);
    } finally {
        System.getProperties().remove("country");
    }
}