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.mybatis.spring.support.SqlSessionDaoSupportTest.java

private void setupContext() {
    applicationContext = new GenericApplicationContext();

    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(MockSqlSessionDao.class);
    applicationContext.registerBeanDefinition("dao", definition);

    // add support for autowiring fields
    AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext);
}

From source file:com.rvantwisk.cnctools.ScreensConfiguration.java

/**
 * Add's a new bean to Spring's context/*from   www .  ja  va 2 s. co  m*/
 *
 * @param name
 */
public void registerBean(final String name) {
    if (registeredbeans.containsKey(name)) {
        return;
    }
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClassName(name);
    beanDefinition.setAutowireCandidate(true);
    AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    registry.registerBeanDefinition(name, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    registeredbeans.put(name, Boolean.TRUE);
}

From source file:org.wallride.autoconfigure.WebAdminComponentScanRegistrar.java

private void addWebAdminComponentScanBeanPostProcessor(BeanDefinitionRegistry registry,
        Set<String> packagesToScan) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(WebAdminComponentScanBeanPostProcessor.class);
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(toArray(packagesToScan));
    beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    // We don't need this one to be post processed otherwise it can cause a
    // cascade of bean instantiation that we would rather avoid.
    beanDefinition.setSynthetic(true);/*from  ww  w  .ja va 2s  . c  o  m*/
    registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
}

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurerTest.java

@Test
public void testProcessPropertiesWithFieldAnnotation() {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(AnnotatedFieldTestBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);

    properties.put(TEST_KEY, TEST_VALUE);

    configurer.processProperties(beanFactory, properties);

    assertNotNull(//from ww  w . j a  v  a  2s . c o m
            beanFactory.getBeanDefinition(TEST_BEAN_NAME).getPropertyValues().getPropertyValue("property"));
    assertEquals(TEST_VALUE, beanFactory.getBeanDefinition(TEST_BEAN_NAME).getPropertyValues()
            .getPropertyValue("property").getValue());
}

From source file:org.wallride.autoconfigure.WebGuestComponentScanRegistrar.java

private void addWebGuestComponentScanBeanPostProcessor(BeanDefinitionRegistry registry,
        Set<String> packagesToScan) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(WebGuestComponentScanBeanPostProcessor.class);
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(toArray(packagesToScan));
    beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    // We don't need this one to be post processed otherwise it can cause a
    // cascade of bean instantiation that we would rather avoid.
    beanDefinition.setSynthetic(true);/*from   w ww  .ja  v  a 2 s .c  o  m*/
    registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
}

From source file:org.romaz.spring.scripting.ext.ExtScriptBeanDefinitionParser.java

/**
 * Parses the dynamic object element and returns the resulting bean definition.
 * Registers a {@link ScriptFactoryPostProcessor} if needed.
 *///  ww w .  ja v  a 2 s  . c om
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    // Resolve the script source.
    String value = resolveScriptSource(element, parserContext.getReaderContext());
    if (value == null) {
        return null;
    }

    // Set up infrastructure.
    LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry());

    // Create script factory bean definition.
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClassName(this.scriptFactoryClassName);
    bd.setSource(parserContext.extractSource(element));

    // Determine bean scope.
    String scope = element.getAttribute(SCOPE_ATTRIBUTE);
    if (StringUtils.hasLength(scope)) {
        bd.setScope(scope);
    }

    // Determine autowire mode.
    String autowire = element.getAttribute(AUTOWIRE_ATTRIBUTE);
    int autowireMode = parserContext.getDelegate().getAutowireMode(autowire);
    // Only "byType" and "byName" supported, but maybe other default inherited...
    if (autowireMode == GenericBeanDefinition.AUTOWIRE_AUTODETECT) {
        autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE;
    } else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
        autowireMode = GenericBeanDefinition.AUTOWIRE_NO;
    }
    bd.setAutowireMode(autowireMode);

    // Determine dependency check setting.
    String dependencyCheck = element.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
    bd.setDependencyCheck(parserContext.getDelegate().getDependencyCheck(dependencyCheck));

    // Retrieve the defaults for bean definitions within this parser context
    BeanDefinitionDefaults beanDefinitionDefaults = parserContext.getDelegate().getBeanDefinitionDefaults();

    // Determine init method and destroy method.
    String initMethod = element.getAttribute(INIT_METHOD_ATTRIBUTE);
    if (StringUtils.hasLength(initMethod)) {
        bd.setInitMethodName(initMethod);
    } else if (beanDefinitionDefaults.getInitMethodName() != null) {
        bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName());
    }

    String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
    if (StringUtils.hasLength(destroyMethod)) {
        bd.setDestroyMethodName(destroyMethod);
    } else if (beanDefinitionDefaults.getDestroyMethodName() != null) {
        bd.setDestroyMethodName(beanDefinitionDefaults.getDestroyMethodName());
    }

    // Attach any refresh metadata.
    String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
    if (StringUtils.hasText(refreshCheckDelay)) {
        bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, new Long(refreshCheckDelay));
    }

    // Add constructor arguments.
    ConstructorArgumentValues cav = bd.getConstructorArgumentValues();
    int constructorArgNum = 0;
    cav.addIndexedArgumentValue(constructorArgNum++, value);
    if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) {
        cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE));
    }
    if (element.hasAttribute(SCRIPT_CONFIG_ATTRIBUTE)) {
        cav.addIndexedArgumentValue(constructorArgNum++,
                new RuntimeBeanReference(element.getAttribute(SCRIPT_CONFIG_ATTRIBUTE)));
    }
    // Add any property definitions that need adding.
    parserContext.getDelegate().parsePropertyElements(element, bd);

    return bd;
}

From source file:fr.pilato.spring.elasticsearch.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 name = XMLParserUtil.getElementStringValue(element, "name");

    String node = XMLParserUtil.getElementStringValue(element, "node");
    String esNodes = XMLParserUtil.getElementStringValue(element, "esNodes");

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

    boolean forceMapping = XMLParserUtil.getElementBooleanValue(element, "forceMapping");
    boolean forceTemplate = XMLParserUtil.getElementBooleanValue(element, "forceTemplate");
    boolean mergeMapping = XMLParserUtil.getElementBooleanValue(element, "mergeMapping");
    boolean mergeSettings = XMLParserUtil.getElementBooleanValue(element, "mergeSettings");
    boolean autoscan = XMLParserUtil.getElementBooleanValue(element, "autoscan", true);

    String classpathRoot = XMLParserUtil.getElementStringValue(element, "classpathRoot");
    String mappings = XMLParserUtil.getElementStringValue(element, "mappings");
    String aliases = XMLParserUtil.getElementStringValue(element, "aliases");
    String templates = XMLParserUtil.getElementStringValue(element, "templates");

    String taskExecutor = XMLParserUtil.getElementStringValue(element, "taskExecutor");
    String async = XMLParserUtil.getElementStringValue(element, "async");

    // Checking bean definition
    boolean isClientNode = (node != null && node.length() > 0);
    boolean isEsNodesEmpty = (esNodes == null || esNodes.length() == 0);

    if (isClientNode && !isEsNodesEmpty) {
        throw new RuntimeException("Incorrect settings. You should not set esNodes when using a client node.");
    }/*from w  ww .  j  av a 2s  .com*/

    if (!isClientNode && isEsNodesEmpty) {
        throw new RuntimeException(
                "Incorrect settings. You must set esNodes when creating a transport client.");
    }

    BeanDefinition client;

    GenericBeanDefinition bdef = new GenericBeanDefinition();
    if (isClientNode) {
        bdef.setBeanClass(ElasticsearchClientFactoryBean.class);
        BeanDefinitionBuilder clientBuilder = startClientBuilder(ElasticsearchClientFactoryBean.class,
                settingsFile, properties, forceMapping, forceTemplate, mergeMapping, mergeSettings, autoscan,
                classpathRoot, mappings, aliases, templates, async, taskExecutor);
        client = ClientBeanDefinitionParser.buildClientDef(clientBuilder, node);
    } else {
        bdef.setBeanClass(ElasticsearchTransportClientFactoryBean.class);
        BeanDefinitionBuilder clientBuilder = startClientBuilder(ElasticsearchTransportClientFactoryBean.class,
                settingsFile, properties, forceMapping, forceTemplate, mergeMapping, mergeSettings, autoscan,
                classpathRoot, mappings, aliases, templates, async, taskExecutor);
        client = ClientBeanDefinitionParser.buildTransportClientDef(clientBuilder, esNodes);
    }

    // Register NodeBeanDefinition
    if (id != null && id.length() > 0) {
        parserContext.getRegistry().registerBeanDefinition(id, client);
    } else {
        parserContext.getRegistry().registerBeanDefinition(name, client);
    }

    return bdef;
}

From source file:com.github.steveash.spring.WiringFactoryBeanFactoryPostProcessor.java

private void addPrototypeDef(ConfigurableListableBeanFactory beanFactory, String beanDefName,
        Class<?> protoBeanClass) {
    String beanName = getPrototypeBeanNameFromBeanClass(protoBeanClass);
    if (beanFactory.containsBeanDefinition(beanName)) {
        throw new BeanDefinitionValidationException("Trying to register a bean definition for a synthetic "
                + "prototype bean with name " + beanName + " due to the bean factory of name " + beanDefName
                + " but a bean with this name already exists!");
    }/* w w w  .j  av  a2s .c o m*/

    GenericBeanDefinition protoBean = new GenericBeanDefinition();
    protoBean.setLazyInit(true);
    protoBean.setBeanClass(protoBeanClass);
    protoBean.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    protoBean.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    protoBean.setBeanClassName(protoBeanClass.getName());

    log.debug("Dynamically adding prototype bean {} from factory {}", beanName, beanDefName);
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    registry.registerBeanDefinition(beanName, protoBean);
}

From source file:org.mybatis.spring.mapper.MapperScannerConfigurerTest.java

@Test
public void testNameGenerator() {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(BeanNameGenerator.class);
    applicationContext.registerBeanDefinition("beanNameGenerator", definition);

    applicationContext.getBeanDefinition("mapperScanner").getPropertyValues().add("nameGenerator",
            new RuntimeBeanReference("beanNameGenerator"));

    startContext();/*w ww . jav  a 2 s  . c o m*/

    // only child inferfaces should be loaded and named with its class name
    applicationContext.getBean(MapperInterface.class.getName());
    applicationContext.getBean(MapperSubinterface.class.getName());
    applicationContext.getBean(MapperChildInterface.class.getName());
    applicationContext.getBean(AnnotatedMapper.class.getName());
}

From source file:org.mybatis.spring.support.SqlSessionDaoSupportTest.java

private void setupSqlSessionFactory(String name) {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(SqlSessionFactoryBean.class);
    definition.getPropertyValues().add("dataSource", dataSource);

    applicationContext.registerBeanDefinition(name, definition);
}