Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 809361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:43:00+00:00 2026-05-15T00:43:00+00:00

With an XML configured Spring bean factory, I can easily instantiate multiple instances of

  • 0

With an XML configured Spring bean factory, I can easily instantiate multiple instances of the same class with different parameters. How can I do the same with annotations? I would like something like this:

@Component(firstName="joe", lastName="smith")
@Component(firstName="mary", lastName="Williams")
public class Person { /* blah blah */ }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:43:01+00:00Added an answer on May 15, 2026 at 12:43 am

    Yes, you can do it with a help of your custom BeanFactoryPostProcessor implementation.

    Here is a simple example.

    Suppose we have two components. One is dependency for another.

    First component:

    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.util.Assert;
    
     public class MyFirstComponent implements InitializingBean{
    
        private MySecondComponent asd;
    
        private MySecondComponent qwe;
    
        public void afterPropertiesSet() throws Exception {
            Assert.notNull(asd);
            Assert.notNull(qwe);
        }
    
        public void setAsd(MySecondComponent asd) {
            this.asd = asd;
        }
    
        public void setQwe(MySecondComponent qwe) {
            this.qwe = qwe;
        }
    }
    

    As you could see, there is nothing special about this component. It has dependency on two different instances of MySecondComponent.

    Second component:

    import org.springframework.beans.factory.FactoryBean;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    
    @Qualifier(value = "qwe, asd")
    public class MySecondComponent implements FactoryBean {
    
        public Object getObject() throws Exception {
            return new MySecondComponent();
        }
    
        public Class getObjectType() {
            return MySecondComponent.class;
        }
    
        public boolean isSingleton() {
            return true;
        }
    }
    

    It’s a bit more tricky. Here are two things to explain. First one – @Qualifier – annotation which contains names of MySecondComponent beans. It’s a standard one, but you are free to implement your own. You’ll see a bit later why.

    Second thing to mention is FactoryBean implementation. If bean implements this interface, it’s intended to create some other instances. In our case it creates instances with MySecondComponent type.

    The trickiest part is BeanFactoryPostProcessor implementation:

    import java.util.Map;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    
    
    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
            Map<String, Object> map =  configurableListableBeanFactory.getBeansWithAnnotation(Qualifier.class);
            for(Map.Entry<String,Object> entry : map.entrySet()){
                createInstances(configurableListableBeanFactory, entry.getKey(), entry.getValue());
            }
    
        }
    
        private void createInstances(
                ConfigurableListableBeanFactory configurableListableBeanFactory,
                String beanName,
                Object bean){
            Qualifier qualifier = bean.getClass().getAnnotation(Qualifier.class);
            for(String name : extractNames(qualifier)){
                Object newBean = configurableListableBeanFactory.getBean(beanName);
                configurableListableBeanFactory.registerSingleton(name.trim(), newBean);
            }
        }
    
        private String[] extractNames(Qualifier qualifier){
            return qualifier.value().split(",");
        }
    }
    

    What does it do? It goes through all beans annotated with @Qualifier, extract names from the annotation and then manually creates beans of this type with specified names.

    Here is a Spring config:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean class="MyBeanFactoryPostProcessor"/>
    
        <bean class="MySecondComponent"/>
    
    
        <bean name="test" class="MyFirstComponent">
            <property name="asd" ref="asd"/>
            <property name="qwe" ref="qwe"/>
        </bean>
    
    </beans>
    

    Last thing to notice here is although you can do it you shouldn’t unless it is a must, because this is a not really natural way of configuration. If you have more than one instance of class, it’s better to stick with XML configuration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Hibernate Transaction manager configured inside Spring MVC controller. <bean id=dataSource class=org.apache.commons.dbcp.BasicDataSource destroy-method=close>
I'm adding security to my spring-ws app. i have configured: <sws:interceptors> <bean class=org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor> <property
In my appname-servlet.xml I have: <!-- freemarker config --> <bean id=freemarkerConfig class=org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer> <property name=templateLoaderPath
I have a spring bean that I have configured in applicationContext like below: <bean
How i can define one ApplicationContext as prototype spring bean in other application context.
Can anyone assist me with the following. I've configured spring authentication fine so -
I am using prototype scoped bean definitions in my Spring XML descriptors to configure
I configured Second Level Cache using Ehcache in hibernate.cfg.xml ,ehcache.xml.And setting the cache-usage property
In the web.xml of my web app I have configured the server as following:
I have configured the properies file in JBoss AS 6 using properties-service.xml . here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.