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

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

Introduction

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

Prototype

@Override
public ConstructorArgumentValues getConstructorArgumentValues() 

Source Link

Document

Return constructor argument values for this bean (never null ).

Usage

From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java

/**
 * Create a bean definition for the scripted object, based on the given script
 * definition, extracting the definition data that is relevant for the scripted
 * object (that is, everything but bean class and constructor arguments).
 * @param bd the full script bean definition
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptSource the ScriptSource for the scripted bean
 * @param interfaces the interfaces that the scripted bean is supposed to implement
 * @return the extracted ScriptFactory bean definition
 * @see org.springframework.scripting.ScriptFactory#getScriptedObject
 *//*from ww  w  . ja v a 2s.c om*/
protected BeanDefinition createScriptedObjectBeanDefinition(BeanDefinition bd, String scriptFactoryBeanName,
        ScriptSource scriptSource, Class<?>[] interfaces) {

    GenericBeanDefinition objectBd = new GenericBeanDefinition(bd);
    objectBd.setFactoryBeanName(scriptFactoryBeanName);
    objectBd.setFactoryMethodName("getScriptedObject");
    objectBd.getConstructorArgumentValues().clear();
    objectBd.getConstructorArgumentValues().addIndexedArgumentValue(0, scriptSource);
    objectBd.getConstructorArgumentValues().addIndexedArgumentValue(1, interfaces);
    return objectBd;
}

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

private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
    GenericBeanDefinition definition;
    for (BeanDefinitionHolder holder : beanDefinitions) {
        definition = (GenericBeanDefinition) holder.getBeanDefinition();

        if (logger.isDebugEnabled()) {
            logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '"
                    + definition.getBeanClassName() + "' mapperInterface");
        }// w w w. j  a v a  2s  .  c o  m

        // the mapper interface is the original class of the bean
        // but, the actual class of the bean is MapperFactoryBean
        definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59
        definition.setBeanClass(this.mapperFactoryBean.getClass());

        definition.getPropertyValues().add("addToConfig", this.addToConfig);

        boolean explicitFactoryUsed = false;
        if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
            definition.getPropertyValues().add("sqlSessionFactory",
                    new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
            explicitFactoryUsed = true;
        } else if (this.sqlSessionFactory != null) {
            definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
            explicitFactoryUsed = true;
        }

        if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
            if (explicitFactoryUsed) {
                logger.warn(
                        "Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
            }
            definition.getPropertyValues().add("sqlSessionTemplate",
                    new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
            explicitFactoryUsed = true;
        } else if (this.sqlSessionTemplate != null) {
            if (explicitFactoryUsed) {
                logger.warn(
                        "Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
            }
            definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
            explicitFactoryUsed = true;
        }

        if (!explicitFactoryUsed) {
            if (logger.isDebugEnabled()) {
                logger.debug("Enabling autowire by type for MapperFactoryBean with name '"
                        + holder.getBeanName() + "'.");
            }
            definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
        }
    }
}

From source file:com.dianping.avatar.cache.spring.CacheBeanDefinitionParser.java

/**
 * Register {@link CacheService} definition.
 * DefaultCacheServiceProxy delegates the DefaultCacheService on behalf of the cache hit-rate statistics.
 *///  w w  w . j  a  v a 2  s  .co m
protected GenericBeanDefinition initCacheServiceDefinition(Element element,
        BeanDefinitionRegistry beanDefinitionRegistry) {
    GenericBeanDefinition cacheDefinition = new GenericBeanDefinition();
    cacheDefinition.setBeanClass(DefaultCacheService.class);
    cacheDefinition.setAutowireCandidate(false);
    //JDK proxy not available, as the objects are managed by the Spring IOC
    /*        DefaultCacheService cacheServiceProxy = (DefaultCacheService)Proxy.newProxyInstance(DefaultCacheService.class.getClassLoader(),new Class<?>[]{DefaultCacheService.class}, new CacheServiceInvokerHandler());
            definition.setBeanClass(cacheServiceProxy.getClass());*/

    cacheServiceId = element.getAttribute(CACHE_SERVICE_ID_ATTR);

    if (!StringUtils.hasText(cacheServiceId)) {
        cacheServiceId = DEFAULT_CACHE_SERVICE_ID;
    }

    // Add reference to CacheFactory
    ConstructorArgumentValues constructorArgumentValues = cacheDefinition.getConstructorArgumentValues();

    String cacheClientFactoryId = element.getAttribute(CACHE_CLIENT_FACTORY_ID_ATTR);
    cacheItemConfigManager = element.getAttribute(CACHE_ITEM_MANAGER_ID_ATTR);
    if (!StringUtils.hasText(cacheClientFactoryId) || !StringUtils.hasText(cacheItemConfigManager)) {
        registerCacheRelatedWebService(beanDefinitionRegistry);
    }

    if (!StringUtils.hasText(cacheClientFactoryId)) {
        cacheClientFactoryId = DEFAULT_CACHE_CLIENT_FACTORY_ID;
        // Register default cache client factory
        registerDefaultCacheClientFactory(beanDefinitionRegistry);
    }
    constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference(cacheClientFactoryId));
    constructorArgumentValues
            .addGenericArgumentValue(new RuntimeBeanReference(ONEWAY_CACHE_MANAGE_WEB_SERVICE_ID));

    if (!StringUtils.hasText(cacheItemConfigManager)) {
        cacheItemConfigManager = DEFAULT_ITEM_CONFIG_MANAGER_ID;
        // Register default cache item config manager
        registerDefaultCacheItemConfigManager(beanDefinitionRegistry);
    }
    constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference(cacheItemConfigManager));

    return cacheDefinition;

    /*        BeanDefinitionHolder holder = new BeanDefinitionHolder(cacheDefinition, this.cacheServiceId);
            
            BeanDefinitionReaderUtils.registerBeanDefinition(holder, beanDefinitionRegistry);*/
}

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.
 *///from  w  ww.j a v a 2  s .  c o  m
@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:com.dianping.avatar.cache.spring.CacheBeanDefinitionParser.java

/**
 * Register jms listener definition/*from w  w w . ja  v a2 s .  co m*/
 */
private void registerJmsDefinition(Element element, ParserContext parserContext) {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = definition.getPropertyValues();

    definition = new GenericBeanDefinition();
    definition.setBeanClass(CacheKeyTypeVersionUpdateListener.class);
    propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("cacheItemConfigManager", new RuntimeBeanReference(cacheItemConfigManager));
    BeanDefinitionReaderUtils.registerBeanDefinition(
            new BeanDefinitionHolder(definition, "keyTypeVersionUpdateListener"), parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    definition.setBeanClass(SingleCacheRemoveListener.class);
    propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("cacheClientFactory",
            new RuntimeBeanReference(DEFAULT_CACHE_CLIENT_FACTORY_ID));
    BeanDefinitionReaderUtils.registerBeanDefinition(
            new BeanDefinitionHolder(definition, "singleCacheRemoveListener"), parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    definition.setBeanClass(CacheConfigurationUpdateListener.class);
    propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("cacheClientFactory",
            new RuntimeBeanReference(DEFAULT_CACHE_CLIENT_FACTORY_ID));
    BeanDefinitionReaderUtils.registerBeanDefinition(
            new BeanDefinitionHolder(definition, "cacheConfigUpdateListener"), parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    definition.setBeanClass(CacheKeyConfigUpdateListener.class);
    propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("cacheItemConfigManager", new RuntimeBeanReference(cacheItemConfigManager));
    BeanDefinitionReaderUtils.registerBeanDefinition(
            new BeanDefinitionHolder(definition, "cacheKeyConfigUpdateListener"), parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    definition.setBeanClass(MessageReceiver.class);
    propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("mappingList[0]", new RuntimeBeanReference("keyTypeVersionUpdateListener"));
    propertyValues.addPropertyValue("mappingList[1]", new RuntimeBeanReference("singleCacheRemoveListener"));
    propertyValues.addPropertyValue("mappingList[2]", new RuntimeBeanReference("cacheConfigUpdateListener"));
    propertyValues.addPropertyValue("mappingList[3]", new RuntimeBeanReference("cacheKeyConfigUpdateListener"));
    BeanDefinitionReaderUtils.registerBeanDefinition(
            new BeanDefinitionHolder(definition, "dispatchMessageListener"), parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    definition.setBeanClass(MongoMQService.class);
    ConstructorArgumentValues constructorArgumentValues = definition.getConstructorArgumentValues();
    constructorArgumentValues.addGenericArgumentValue("${avatar-cache.swallow.url}", "java.lang.String");
    BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(definition, "MQService"),
            parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    definition.setBeanClass(Destination.class);
    definition.setFactoryMethodName(DEFAULT_CACHE_JMS_MODE);
    constructorArgumentValues = definition.getConstructorArgumentValues();
    constructorArgumentValues.addGenericArgumentValue(DEFAULT_CACHE_JMS_TOPIC_NAME, "java.lang.String");
    BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(definition, "CacheDestination"),
            parserContext.getRegistry());

    definition = new GenericBeanDefinition();
    //        definition.setBeanClass(MongoMessageConsumer.class);
    definition.setFactoryBeanName("MQService");
    definition.setFactoryMethodName("createConsumer");
    constructorArgumentValues = definition.getConstructorArgumentValues();
    //TODO where to get the DEFAULT_CACHE_JMS_TOPIC_NAME
    constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference("CacheDestination"));
    propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("messageListener", new RuntimeBeanReference("dispatchMessageListener"));
    BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(definition, "cacheConsumer"),
            parserContext.getRegistry());
}

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/*from www .  ja  va2  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");
    }
}

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

@Test
public void stringConcatenationWithDebugLogging() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(String.class);
    bd.getConstructorArgumentValues()
            .addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
    ac.registerBeanDefinition("str", bd);
    ac.refresh();/*from  ww w.  j  a v a 2 s.c  o m*/

    String str = ac.getBean("str", String.class);
    assertTrue(str.startsWith("test-"));
}

From source file:org.springframework.scripting.support.ScriptFactoryPostProcessor.java

/**
 * Create a bean definition for the scripted object, based on the given script
 * definition, extracting the definition data that is relevant for the scripted
 * object (that is, everything but bean class and constructor arguments).
 * @param bd the full script bean definition
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptSource the ScriptSource for the scripted bean
 * @param interfaces the interfaces that the scripted bean is supposed to implement
 * @return the extracted ScriptFactory bean definition
 * @see org.springframework.scripting.ScriptFactory#getScriptedObject
 *//*www  .j ava 2s. c om*/
protected BeanDefinition createScriptedObjectBeanDefinition(BeanDefinition bd, String scriptFactoryBeanName,
        ScriptSource scriptSource, @Nullable Class<?>[] interfaces) {

    GenericBeanDefinition objectBd = new GenericBeanDefinition(bd);
    objectBd.setFactoryBeanName(scriptFactoryBeanName);
    objectBd.setFactoryMethodName("getScriptedObject");
    objectBd.getConstructorArgumentValues().clear();
    objectBd.getConstructorArgumentValues().addIndexedArgumentValue(0, scriptSource);
    objectBd.getConstructorArgumentValues().addIndexedArgumentValue(1, interfaces);
    return objectBd;
}