Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create bean definition with injected properties?

Tags:

I want to programmatically add a bean definition to an application context, but some properties of that definition are other beans from that context (I know their names). How can I do this so that those properties will be injected?

For example:

GenericBeanDefinition beanDef = new GenericBeanDefinition();
beanDef.setBeanClass(beanClass);

MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue("intProperty", 10);
values.addPropertyValue("stringProperty", "Hello, world");
values.addPropertyValue("beanProperty", /* What should be here? */);

beanDef.setPropertyValues(values);

I'm using Spring 3.0.

like image 715
Fixpoint Avatar asked Jul 30 '10 09:07

Fixpoint


People also ask

How do I register beans in Spring boot programmatically?

You can implement the ApplicationContextInitializer to register the bean programmatically. It is essentially code that gets executed before the Spring application context gets completely created. In this class, you can call registerBean and register the bean programmatically.

How do you define a bean configuration?

Declaring a bean. To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory .

How do I create a custom bean in Spring boot?

Different Methods to Create a Spring BeanCreating Bean Inside an XML Configuration File (beans. xml) Using @Component Annotation. Using @Bean Annotation.

Is metadata a property of bean?

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.


2 Answers

Use RuntimeBeanReference:

values.addPropertyValue("beanProperty", new RuntimeBeanReference("beanName")); 
like image 185
axtavt Avatar answered Dec 10 '22 16:12

axtavt


I would add a bean like this that has access to the applicationContext:

public class AppContextExtendingBean implements ApplicationContextAware{


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{

        AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();

        // do it like this
        version1(beanFactory);

        // or like this
        version2(beanFactory);

    }

    // let spring create a new bean and then manipulate it (works only for singleton beans, obviously) 
    private void version1(AutowireCapableBeanFactory beanFactory){
        MyObject newBean = (MyObject) beanFactory.createBean(MyObject.class,AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        newBean.setBar("baz");
        newBean.setFoo("foo");
        newBean.setPhleem("phleem");
        beanFactory.initializeBean(newBean, "bean1");
    }

    // create the object manually and then inject it into the spring context
    private void version2(AutowireCapableBeanFactory beanFactory){
        MyObject myObject=new MyObject("foo","phleem");
        myObject.setBar("baz");
        beanFactory.autowireBean(myObject);
        beanFactory.initializeBean(myObject, "bean2");
    }


}
like image 41
Sean Patrick Floyd Avatar answered Dec 10 '22 17:12

Sean Patrick Floyd