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

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

Introduction

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

Prototype

@Override
public MutablePropertyValues getPropertyValues() 

Source Link

Document

Return property values for this bean (never null ).

Usage

From source file:framework.generic.mybatis.ClassPathMapperScanner.java

/**
 * Calls the parent search that will search and register all the candidates.
 * Then the registered objects are post processed to set them as
 * MapperFactoryBeans/*from  ww  w. j  a va  2 s  . co  m*/
 */
@Override
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
    Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
    if (beanDefinitions.isEmpty()) {
        logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages)
                + "' package. Please check your configuration.");
    } else {
        for (BeanDefinitionHolder holder : beanDefinitions) {
            GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();

            if (logger.isDebugEnabled()) {
                logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '"
                        + definition.getBeanClassName() + "' mapperInterface");
            }

            // the mapper interface is the original class of the bean
            // but, the actual class of the bean is MapperFactoryBean
            definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());
            definition.setBeanClass(MapperFactoryBean.class);
            // definition.setBeanClass(ExecutorFactoryBean.class);
            definition.getPropertyValues().add("executor", this.executor);
            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);
            }
        }
    }

    return beanDefinitions;
}

From source file:test.pl.chilldev.web.spring.config.ScriptBeanDefinitionParserTest.java

@Test
public void parse() {
    GenericBeanDefinition bean = new GenericBeanDefinition();
    ScriptBeanDefinitionParser parser = new ScriptBeanDefinitionParser(bean);

    String src = "foo.js";
    String type = "application/javascript";
    String flow = "ASYNC";
    String charset = "utf-8";

    when(this.element.hasAttribute("charset")).thenReturn(true);
    when(this.element.getAttribute("charset")).thenReturn(charset);
    when(this.element.hasAttribute("flow")).thenReturn(true);
    when(this.element.getAttribute("flow")).thenReturn(flow);
    when(this.element.hasAttribute("type")).thenReturn(true);
    when(this.element.getAttribute("type")).thenReturn(type);
    when(this.element.getAttribute("src")).thenReturn(src);

    parser.parse(this.element, null);

    List<BeanDefinition> scripts = (List<BeanDefinition>) bean.getPropertyValues()
            .getPropertyValue(ScriptBeanDefinitionParser.PROPERTY_SCRIPTS).getValue();
    BeanDefinition script = scripts.get(0);
    ConstructorArgumentValues arguments = script.getConstructorArgumentValues();

    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's location.", src,
            arguments.getIndexedArgumentValue(0, null).getValue());
    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's MIME type.", type,
            arguments.getIndexedArgumentValue(1, null).getValue());
    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's loading flow.", flow,
            arguments.getIndexedArgumentValue(2, null).getValue());
    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's charset.", charset,
            arguments.getIndexedArgumentValue(3, null).getValue());
}

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

protected void registerStatisticsCacheInterceptor(Element element,
        BeanDefinitionRegistry beanDefinitionRegistry) {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(getStatisticsCacheInterceptor());
    String cacheInterceptorId = "monitorInterceptor";
    //register the cache item config manager
    cacheItemConfigManager = element.getAttribute(CACHE_ITEM_MANAGER_ID_ATTR);
    if (!StringUtils.hasText(cacheItemConfigManager)) {
        cacheItemConfigManager = DEFAULT_ITEM_CONFIG_MANAGER_ID;
    }/*from  ww w .  j av a  2  s.  c  o  m*/
    MutablePropertyValues propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue(cacheItemConfigManager, new RuntimeBeanReference(cacheItemConfigManager));
    BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, cacheInterceptorId);
    BeanDefinitionReaderUtils.registerBeanDefinition(holder, beanDefinitionRegistry);
}

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");
        }//from  www.  j a va  2 s. co 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:org.bytesoft.bytejta.supports.dubbo.TransactionConfigPostProcessor.java

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

    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", "transaction");
    mpv.addPropertyValue("group", "org.bytesoft.bytejta");
    mpv.addPropertyValue("retries", "0");
    mpv.addPropertyValue("timeout", "5000");

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

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.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   w w  w  . j  a  v  a2s  .  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 systemPropertiesSecurityManager() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(TestBean.class);
    bd.getPropertyValues().add("country", "#{systemProperties.country}");
    ac.registerBeanDefinition("tb", bd);

    SecurityManager oldSecurityManager = System.getSecurityManager();
    try {//from  ww w  .jav  a2 s  . co m
        System.setProperty("country", "NL");

        SecurityManager securityManager = new SecurityManager() {
            @Override
            public void checkPropertiesAccess() {
                throw new AccessControlException("Not Allowed");
            }

            @Override
            public void checkPermission(Permission perm) {
                // allow everything else
            }
        };
        System.setSecurityManager(securityManager);
        ac.refresh();

        TestBean tb = ac.getBean("tb", TestBean.class);
        assertEquals("NL", tb.getCountry());

    } finally {
        System.setSecurityManager(oldSecurityManager);
        System.getProperties().remove("country");
    }
}