Java Code Examples for org.springframework.beans.factory.config.RuntimeBeanReference

Following code examples demonstrate how to use org.springframework.beans.factory.config.RuntimeBeanReferencefrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.beans.factory.config.RuntimeBeanReferenceand various code implementation of this class.

    .....
    private boolean hasPrototypeDependency(ConfigurableListableBeanFactory beanFactory,
        BeanDefinition beanDefinition) {
	boolean isPrototype = false;
	MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
	PropertyValue[] propertyValues = mutablePropertyValues.getPropertyValues();
	for (int j = 0; j < propertyValues.length; j++) {
	    if (propertyValues[j].getValue() instanceof RuntimeBeanReference) {
		String dependencyBeanName = ((RuntimeBeanReference) propertyValues[j]
		    .getValue()).getBeanName();
		BeanDefinition dependencyBeanDef = beanFactory
		    .getBeanDefinition(dependencyBeanName);
		if (dependencyBeanDef.isPrototype()) {
		    isPrototype = true;
		    break;
		}
	    }
	}
	return isPrototype;
    }	 


    public BeanDefinition parse(Element element, ParserContext parserContext) {

        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);

        if (ConsumerConfig.class.equals(beanClass)) {
            String id = element.getAttribute("id");
            String interfaceName = element.getAttribute("interface");

            beanDefinition.getPropertyValues().addPropertyValue("id", id);
            beanDefinition.getPropertyValues().addPropertyValue(
                    "interfaceName", interfaceName);

            parserContext.getRegistry().registerBeanDefinition(id,
                    beanDefinition);

        } else if (ProviderConfig.class.equals(beanClass)) {
            String ref = element.getAttribute("ref");
            Object reference = new RuntimeBeanReference(ref);
            beanDefinition.getPropertyValues().addPropertyValue("ref",
                    reference);

            String interfaceName = element.getAttribute("interface");
            beanDefinition.getPropertyValues().addPropertyValue(
                    "interfaceName", interfaceName);

            parserContext.getRegistry().registerBeanDefinition(interfaceName,
                    beanDefinition);
        }

        return beanDefinition;
    } 

	public Collection<BeanDefinition> parse(Element element, ParserContext parserContext) {

		String refAttribute = element.getAttribute("decider");
		String idAttribute = element.getAttribute("id");

		BeanDefinitionBuilder stateBuilder = 
			BeanDefinitionBuilder.genericBeanDefinition(
					"cn.com.sandpay.processmanager.core.flow.support.state.DecisionState");
		stateBuilder.addConstructorArgValue(new RuntimeBeanReference(refAttribute));
		stateBuilder.addConstructorArgValue(idAttribute);
		return TopLevelFlowParser.getNextElements(parserContext, stateBuilder.getBeanDefinition(), element);

	} 

    void beanDefinitionParser(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

        String interfaceName = element.getAttribute("interface");
        String targetBean = element.getAttribute("targetBean");

        builder.addPropertyValue("interfaze", interfaceName);
        builder.addPropertyValue("targetBean", new RuntimeBeanReference(targetBean));
    } 

    static BeanDefinition parseKeyGenerator(Element element, BeanDefinition def) {
        String name = element.getAttribute("key-generator");
        if (StringUtils.hasText(name)) {
            def.getPropertyValues().add("keyGenerator", new RuntimeBeanReference(name.trim()));
        }
        return def;
    } 

    protected void doParse(Element element, BeanDefinitionBuilder builder) {

        String topic = element.getAttribute("topic");
        String server = element.getAttribute("server");

        builder.addPropertyValue("server", new RuntimeBeanReference(server));

        ProducerConfig producerConfig = new ProducerConfig();
        producerConfig.setTopic(topic);
        builder.addPropertyValue("producerConfig",producerConfig);
        builder.setInitMethodName("init");
    } 

    protected void doParse(Element element, BeanDefinitionBuilder builder) {

        String server = element.getAttribute("server");
        builder.addPropertyValue("server", new RuntimeBeanReference(server));

        builder.setInitMethodName("init");
    } 

    static BeanDefinition parseKeyGenerator(Element element, BeanDefinition def) {
        String name = element.getAttribute("key-generator");
        if (StringUtils.hasText(name)) {
            def.getPropertyValues().add("keyGenerator", new RuntimeBeanReference(name.trim()));
        }
        return def;
    } 

    @Override
    protected void doParse(Element element, ParserContext parserContext,
                           BeanDefinitionBuilder builder) {
        super.doParse(element,parserContext,builder);
        String address=element.getAttribute("address");
        builder.addPropertyValue("address",address);
        String registries=element.getAttribute("registries");
        String registryNames[]= registries.split(",");
        ManagedList<RuntimeBeanReference> list = new ManagedList<>(registryNames.length);
        for(String reg:registryNames){
            list.add(new RuntimeBeanReference(reg));
        }
        builder.addPropertyValue("registryConfigs",list);


    } 

    public static void main(String[] args) {
        
        
        
        RuntimeBeanNameReference ref = new RuntimeBeanNameReference("beanName");
        
        RuntimeBeanReference refBean = new RuntimeBeanReference("beanName");
        
        
    } 

Advertisement
Javadoc
Immutable placeholder class used for a property value object when it's a reference to another bean i

n the factory, to be resolved at runtime. @author Rod Johnson @author Juergen Hoeller @see BeanDefinition#getPropertyValues() @see org.springframework.beans.factory.BeanFactory#getBean

Read More
Advertisement