Announcement Announcement Module
Collapse
No announcement yet.
JavaConfig - dynamic multiple bean (instance) creation of single class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JavaConfig - dynamic multiple bean (instance) creation of single class

    Class A {

    private LDAPTemplate ldapTemplate;

    private JDBCTemplate jdbcTemplate;

    private Class B classBInstance;


    .......
    .......
    }



    The values for constructing LDAPTemplate, well contextSource inside it for LDAP details gets fetched from a property file. Same applies for JDBCTemplate.


    there can be multiple property files, or perhaps let's DB rows holding the configurations. We need to create one instance of Class A per property file found.

    I would have deviated away from requiring to create Spring bean and have it used as domain objects but Spring's dependency on Jdbctemplate or ldapTemplate which subsequently uses InitializingBean makes me feel it's good if i can get them created from IoC Container.


    Is there a way through JavaConfig where I can programatically create bean definitions for multiple instances of same Class. I am aware I can do something like this with BeanFactoryPostProcessor or BeanRegistryPostProcessor and using BeanDefinitonBuilder but to me doing it thru JavaConfig would look more elegant.


    I want that every time I drop a property file that contains configuration for a new instance of Class A, automatically a new instance is created. All property files stay in a pre-known directory path which gets loaded by propertyplaceholderconfigured and gets injected into a class that's responsible for creating these dynamic beans.

  • #2
    have a look at the java doc for AutowireCapableBeanFactory.

    you woudl have to @Autowire in your ApplicationContext and get the bean factory

    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory() ;

    Comment

    Working...
    X