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.gzj.tulip.load.context.RoseWebAppContext.java

/** ?messageSourceRose? */
public static void registerMessageSourceIfNecessary(BeanDefinitionRegistry registry,
        String[] messageBaseNames) {
    if (!registry.containsBeanDefinition(MESSAGE_SOURCE_BEAN_NAME)) {
        GenericBeanDefinition messageSource = new GenericBeanDefinition();
        messageSource.setBeanClass(ReloadableResourceBundleMessageSource.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue("useCodeAsDefaultMessage", true);
        propertyValues.addPropertyValue("defaultEncoding", "UTF-8"); // propertiesUTF-8?ISO-9959-1
        propertyValues.addPropertyValue("cacheSeconds", 60); // hardcode! seconds
        propertyValues.addPropertyValue("basenames", messageBaseNames);

        messageSource.setPropertyValues(propertyValues);
        registry.registerBeanDefinition(MESSAGE_SOURCE_BEAN_NAME, messageSource);
    }//from  ww  w. java2  s . com
}

From source file:org.mybatis.spring.annotation.EnableMapperScanningTest.java

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

From source file:org.codehaus.griffon.runtime.spring.GriffonApplicationContext.java

/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory/* w  ww  . j av  a 2 s .c om*/
 */
public void registerPrototype(String name, Class clazz) throws BeansException {
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
    bd.setBeanClass(clazz);
    getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}

From source file:com.fitbur.jestify.junit.spring.IntegrationTestReifier.java

@Override
public Object reifyCut(CutDescriptor cutDescriptor, Object[] arguments) {
    return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        try {/*  w  ww. j ava 2  s.c  om*/
            Cut cut = cutDescriptor.getCut();
            Field field = cutDescriptor.getField();
            Type fieldType = field.getGenericType();
            String fieldName = field.getName();

            field.setAccessible(true);

            Constructor<?> constructor = cutDescriptor.getConstructor();
            constructor.setAccessible(true);

            ResolvableType resolver = ResolvableType.forType(fieldType);

            Class rawType;

            if (resolver.hasGenerics()) {
                if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) {
                    rawType = resolver.getRawClass();
                } else {
                    rawType = resolver.resolve();
                }
            } else {
                rawType = resolver.resolve();
            }

            Module module = testInstance.getClass().getDeclaredAnnotation(Module.class);

            GenericBeanDefinition bean = new GenericBeanDefinition();
            bean.setBeanClass(rawType);
            bean.setAutowireCandidate(false);
            bean.setScope(SCOPE_PROTOTYPE);
            bean.setPrimary(true);
            bean.setLazyInit(true);
            bean.setRole(RootBeanDefinition.ROLE_APPLICATION);
            bean.setAutowireMode(RootBeanDefinition.AUTOWIRE_NO);
            appContext.registerBeanDefinition(fieldName, bean);
            if (module != null) {
                appContext.register(module.value());
            }
            appContext.refresh();

            Object instance = appContext.getBean(fieldName, arguments);

            if (cut.value()) {
                instance = spy(instance);
            }

            field.set(testInstance, instance);

            return instance;
        } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) {
            throw new RuntimeException(e);
        }
    });
}

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

@Test
public void testProcessPropertiesWithoutBasePackage() {
    configurer.setBasePackage("com.example");
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(SimplePropetyAnnotatedBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);

    properties.put(TEST_KEY, TEST_VALUE);

    configurer.processProperties(beanFactory, properties);

    assertTrue(beanFactory.getBeanDefinition(TEST_BEAN_NAME).getPropertyValues().isEmpty());
}

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

protected ViewResolver getJspViewResolver() throws IOException {
    if (this.jspViewResolver != null) {
        return this.jspViewResolver;
    }/*from  ww w. jav a2  s.  c  om*/
    String beanName = "jspViewResolver";
    this.jspViewResolver = (ViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
    if (jspViewResolver == null) {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(InternalResourceViewResolver.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("viewClass", JstlView.class));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
                beanDefinition);
        logger.info("registered bean definition named " + beanName + ": "
                + InternalResourceViewResolver.class.getName());
        jspViewResolver = (ViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
    }
    return this.jspViewResolver;
}

From source file:org.mybatis.spring.annotation.EnableMapperScanningTest.java

@Test
public void testScanWithExplicitSqlSessionTemplate() throws Exception {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(SqlSessionTemplate.class);
    ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
    constructorArgs.addGenericArgumentValue(new RuntimeBeanReference("sqlSessionFactory"));
    definition.setConstructorArgumentValues(constructorArgs);
    applicationContext.registerBeanDefinition("sqlSessionTemplate", definition);

    applicationContext.register(AppConfigWithSqlSessionTemplate.class);

    startContext();//w  w w  .j av a 2  s .  com

    // all interfaces with methods should be loaded
    applicationContext.getBean("mapperInterface");
    applicationContext.getBean("mapperSubinterface");
    applicationContext.getBean("mapperChildInterface");
    applicationContext.getBean("annotatedMapper");

}

From source file:org.codehaus.griffon.runtime.spring.GriffonApplicationContext.java

/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory// ww  w. j a v  a  2 s  .  c  om
 */
public void registerPrototype(String name, Class clazz, MutablePropertyValues pvs) throws BeansException {
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
    bd.setBeanClass(clazz);
    bd.setPropertyValues(pvs);
    getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}