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:com.fitbur.testify.di.spring.SpringServiceLocator.java

@Override
public void addService(ServiceDescriptor descriptor) {
    checkState(!context.containsBeanDefinition(descriptor.getName()),
            "Service with the name '%s' already exists.", descriptor.getName());

    GenericBeanDefinition bean = new GenericBeanDefinition();
    bean.setBeanClass(descriptor.getType());
    bean.setAutowireCandidate(descriptor.getDiscoverable());
    bean.setPrimary(descriptor.getPrimary());
    bean.setLazyInit(descriptor.getLazy());
    bean.setRole(ROLE_APPLICATION);//  w w  w . j  a v  a  2  s.c  o  m

    if (descriptor.getInjectable()) {
        bean.setAutowireMode(AUTOWIRE_CONSTRUCTOR);
    } else {
        bean.setAutowireMode(AUTOWIRE_NO);
        ConstructorArgumentValues values = new ConstructorArgumentValues();

        Object[] arguments = descriptor.getArguments();
        for (int i = 0; i < arguments.length; i++) {
            Object arg = arguments[i];

            if (arg == null) {
                //TODO: warn user that the argument was not specified and there
                //for the real instance will be injected.
                continue;
            }

            values.addIndexedArgumentValue(i, arg);
        }

        bean.setConstructorArgumentValues(values);
    }

    ServiceScope scope = descriptor.getScope();
    switch (scope) {
    case PROTOTYPE:
        bean.setScope("prototype");
        break;
    case SINGLETON:
        bean.setScope("singleton");
        break;
    case REQUEST:
        bean.setScope("request");
        break;
    case SESSION:
        bean.setScope("session");
        break;
    case APPLICATION:
        bean.setScope("application");
        break;
    default:
        checkState(false, "Scope '{}' is not supported by Spring IoC.", scope.name());

    }
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
    beanFactory.registerBeanDefinition(descriptor.getName(), bean);
}

From source file:com.googlecode.ehcache.annotations.key.SpELCacheKeyGenerator.java

/**
 * Create a new key generator with the specified name.
 *///from  ww w .  j  av a  2  s  .  c o  m
@SuppressWarnings("unchecked")
protected CacheKeyGenerator<Serializable> createKeyGenerator(String name,
        Class<CacheKeyGenerator<Serializable>> keyGeneratorClass, MutablePropertyValues properties) {

    final AbstractBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(keyGeneratorClass);

    if (this.reflectionHelper != null
            && ReflectionHelperAware.class.isAssignableFrom(beanDefinition.getBeanClass())) {
        properties.addPropertyValue("reflectionHelper", this.reflectionHelper);
    }
    beanDefinition.setPropertyValues(properties);

    this.cacheKeyBeanFactory.registerBeanDefinition(name, beanDefinition);

    return this.cacheKeyBeanFactory.getBean(name, CacheKeyGenerator.class);
}

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

@Test
public void testScanWithPropertyPlaceholders() {
    GenericBeanDefinition definition = (GenericBeanDefinition) applicationContext
            .getBeanDefinition("mapperScanner");

    // use a property placeholder for basePackage
    definition.getPropertyValues().removePropertyValue("basePackage");
    definition.getPropertyValues().add("basePackage", "${basePackageProperty}");
    definition.getPropertyValues().add("processPropertyPlaceHolders", true);

    // also use a property placeholder for an SqlSessionFactory property
    // to make sure the configLocation was setup correctly and MapperScanner did not change
    // regular property placeholder substitution
    definition = (GenericBeanDefinition) applicationContext.getBeanDefinition("sqlSessionFactory");
    definition.getPropertyValues().removePropertyValue("configLocation");
    definition.getPropertyValues().add("configLocation", "${configLocationProperty}");

    Properties props = new java.util.Properties();
    props.put("basePackageProperty", "org.mybatis.spring.mapper");
    props.put("configLocationProperty", "classpath:org/mybatis/spring/mybatis-config.xml");

    GenericBeanDefinition propertyDefinition = new GenericBeanDefinition();
    propertyDefinition.setBeanClass(PropertyPlaceholderConfigurer.class);
    propertyDefinition.getPropertyValues().add("properties", props);

    applicationContext.registerBeanDefinition("propertiesPlaceholder", propertyDefinition);

    testInterfaceScan();/*from   ww  w .j  ava 2  s. c o m*/

    // make sure the configLocation was setup correctly
    // mybatis-config.xml changes the executor from the default SIMPLE type
    SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
    assertSame(ExecutorType.REUSE, sessionFactory.getConfiguration().getDefaultExecutorType());
}

From source file:com.sinosoft.one.mvc.web.impl.view.ViewDispatcherImpl.java

private VelocityViewResolver createVelocityViewResolver(Invocation inv, String beanName, String layoutUrl)
        throws MalformedURLException, IOException {
    if (SpringUtils.getBean(getApplicationContext(), VelocityConfig.class) == null) {
        URL propertiesLocation = inv.getServletContext().getResource("/WEB-INF/velocity.properties");
        Properties velocityProperties = new Properties();
        if (propertiesLocation != null) {
            InputStream is = propertiesLocation.openStream();
            velocityProperties.load(is);
            is.close();//from ww  w. jav a2  s  . com
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("input.encoding"))) {
            velocityProperties.setProperty("input.encoding", "UTF-8");
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("output.encoding"))) {
            velocityProperties.setProperty("output.encoding", "UTF-8");
        }
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(MvcVelocityConfigurer.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("velocityProperties", velocityProperties));
        propertyValues.addPropertyValue(new PropertyValue("resourceLoaderPath", "/"));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory())
                .registerBeanDefinition("velocityConfigurer", beanDefinition);
        logger.info("registered bean definition named" + " velocityConfigurer: "
                + MvcVelocityConfigurer.class.getName());
    }
    //
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    if (layoutUrl == null) {
        beanDefinition.setBeanClass(VelocityViewResolver.class);
    } else {
        beanDefinition.setBeanClass(VelocityLayoutViewResolver.class);
        propertyValues.addPropertyValue(new PropertyValue("layoutUrl", layoutUrl));
    }
    propertyValues.addPropertyValue(new PropertyValue("contentType", "text/html;charset=UTF-8"));
    String toolboxConfigLocation = "/WEB-INF/velocity-toolbox.xml";
    propertyValues.addPropertyValue(new PropertyValue("cache", Boolean.TRUE));
    URL toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    if (toolbox == null) {
        toolboxConfigLocation = "/WEB-INF/toolbox.xml";
        toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    }
    if (toolbox != null) {
        propertyValues.addPropertyValue(new PropertyValue("toolboxConfigLocation", toolboxConfigLocation));
    }
    beanDefinition.setPropertyValues(propertyValues);
    ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
            beanDefinition);
    logger.info("registered bean definition named " + beanName + ": " + VelocityViewResolver.class.getName());
    return (VelocityViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
}

From source file:net.paoding.rose.web.impl.view.ViewDispatcherImpl.java

private VelocityViewResolver createVelocityViewResolver(Invocation inv, String beanName, String layoutUrl)
        throws MalformedURLException, IOException {
    if (SpringUtils.getBean(getApplicationContext(), VelocityConfig.class) == null) {
        URL propertiesLocation = inv.getServletContext().getResource("/WEB-INF/velocity.properties");
        Properties velocityProperties = new Properties();
        if (propertiesLocation != null) {
            InputStream is = propertiesLocation.openStream();
            velocityProperties.load(is);
            is.close();/*w  ww  .  ja v  a  2  s  .  c  o  m*/
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("input.encoding"))) {
            velocityProperties.setProperty("input.encoding", "UTF-8");
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("output.encoding"))) {
            velocityProperties.setProperty("output.encoding", "UTF-8");
        }
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(RoseVelocityConfigurer.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("velocityProperties", velocityProperties));
        propertyValues.addPropertyValue(new PropertyValue("resourceLoaderPath", "/"));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory())
                .registerBeanDefinition("velocityConfigurer", beanDefinition);
        logger.info("registered bean definition named" + " velocityConfigurer: "
                + RoseVelocityConfigurer.class.getName());
    }
    //
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    if (layoutUrl == null) {
        beanDefinition.setBeanClass(VelocityViewResolver.class);
    } else {
        beanDefinition.setBeanClass(VelocityLayoutViewResolver.class);
        propertyValues.addPropertyValue(new PropertyValue("layoutUrl", layoutUrl));
    }
    propertyValues.addPropertyValue(new PropertyValue("contentType", "text/html;charset=UTF-8"));
    String toolboxConfigLocation = "/WEB-INF/velocity-toolbox.xml";
    propertyValues.addPropertyValue(new PropertyValue("cache", Boolean.TRUE));
    URL toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    if (toolbox == null) {
        toolboxConfigLocation = "/WEB-INF/toolbox.xml";
        toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    }
    if (toolbox != null) {
        propertyValues.addPropertyValue(new PropertyValue("toolboxConfigLocation", toolboxConfigLocation));
    }
    beanDefinition.setPropertyValues(propertyValues);
    ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
            beanDefinition);
    logger.info("registered bean definition named " + beanName + ": " + VelocityViewResolver.class.getName());
    return (VelocityViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
}

From source file:com.laxser.blitz.web.impl.view.ViewDispatcherImpl.java

private VelocityViewResolver createVelocityViewResolver(Invocation inv, String beanName, String layoutUrl)
        throws MalformedURLException, IOException {
    if (SpringUtils.getBean(getApplicationContext(), VelocityConfig.class) == null) {
        URL propertiesLocation = inv.getServletContext().getResource("/WEB-INF/velocity.properties");
        Properties velocityProperties = new Properties();
        if (propertiesLocation != null) {
            InputStream is = propertiesLocation.openStream();
            velocityProperties.load(is);
            is.close();//from   w ww.  ja v a2  s. c  o  m
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("input.encoding"))) {
            velocityProperties.setProperty("input.encoding", "UTF-8");
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("output.encoding"))) {
            velocityProperties.setProperty("output.encoding", "UTF-8");
        }
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(BlitzVelocityConfigurer.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("velocityProperties", velocityProperties));
        propertyValues.addPropertyValue(new PropertyValue("resourceLoaderPath", "/"));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory())
                .registerBeanDefinition("velocityConfigurer", beanDefinition);
        logger.info("registered bean definition named" + " velocityConfigurer: "
                + BlitzVelocityConfigurer.class.getName());
    }
    //
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    if (layoutUrl == null) {
        beanDefinition.setBeanClass(VelocityViewResolver.class);
    } else {
        beanDefinition.setBeanClass(VelocityLayoutViewResolver.class);
        propertyValues.addPropertyValue(new PropertyValue("layoutUrl", layoutUrl));
    }
    propertyValues.addPropertyValue(new PropertyValue("contentType", "text/html;charset=UTF-8"));
    String toolboxConfigLocation = "/WEB-INF/velocity-toolbox.xml";
    propertyValues.addPropertyValue(new PropertyValue("cache", Boolean.TRUE));
    URL toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    if (toolbox == null) {
        toolboxConfigLocation = "/WEB-INF/toolbox.xml";
        toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    }
    if (toolbox != null) {
        propertyValues.addPropertyValue(new PropertyValue("toolboxConfigLocation", toolboxConfigLocation));
    }
    beanDefinition.setPropertyValues(propertyValues);
    ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
            beanDefinition);
    logger.info("registered bean definition named " + beanName + ": " + VelocityViewResolver.class.getName());
    return (VelocityViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
}

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

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

    properties.put(TEST_KEY, TEST_VALUE);

    try {//w ww  .  j  av  a2  s .  c o m
        configurer.processProperties(beanFactory, properties);
    } catch (BeanConfigurationException e) {
        return;
    }

    fail("Should throw BeanConfigurationException on no property setter.");
}

From source file:co.paralleluniverse.common.spring.SpringContainerHelper.java

public static BeanDefinition defineBean(Class<?> clazz, ConstructorArgumentValues constructorArgs,
        MutablePropertyValues properties) {
    GenericBeanDefinition bean = new GenericBeanDefinition();
    bean.setBeanClass(clazz);/*from   www.  j a  v  a 2 s  .c  o  m*/
    bean.setAutowireCandidate(true);
    bean.setConstructorArgumentValues(constructorArgs);
    bean.setPropertyValues(properties);

    return bean;
}

From source file:org.jdal.beans.DefaultsBeanDefinitionParser.java

/**
 * @param clazz/*from   w ww . j  av a2  s. c o m*/
 * @return
 */
@SuppressWarnings("rawtypes")
private BeanDefinitionHolder createBeanDefinition(Class clazz, ParserContext parserContext) {
    GenericBeanDefinition gbd = new GenericBeanDefinition();
    gbd.setBeanClass(clazz);
    gbd.setInitMethodName(INIT_METHOD_NAME);
    BeanDefinitionHolder holder = new BeanDefinitionHolder(gbd,
            parserContext.getReaderContext().generateBeanName(gbd));

    return holder;
}

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

@Test
public void testProcessPropertiesWithPlaceholderSubstitution() {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(PlaceholderValueTestBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);
    properties.put(TEST_KEY, TEST_VALUE);

    configurer.processProperties(beanFactory, properties);

    assertNotNull(/*from  www.  java2 s .c o  m*/
            beanFactory.getBeanDefinition(TEST_BEAN_NAME).getPropertyValues().getPropertyValue("property"));
    assertEquals("testValue-testValue", beanFactory.getBeanDefinition(TEST_BEAN_NAME).getPropertyValues()
            .getPropertyValue("property").getValue());
}