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 9312077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:32:26+00:00 2026-06-19T01:32:26+00:00

Here is what i have right now which works fine. All it does is

  • 0

Here is what i have right now which works fine. All it does is a market class which returns an array of items objects:

I have class market

class market {

    public ArrayList<Items> createItems(HashMap<String,String> map) {
        ArrayList<Items> array = new ArrayList<Items>();
        for (Map.Entry<String, String> m : map.entrySet()) {
            Item item = new Item();
            item.setName(m.key());
            item.setValue(m.value());
            array.add(item);
        }
        return array;
    }
}

class Item is simple class with getter and setter for name and value

Here is how my config file looks:

@Configuration
public class MarketConfig {

    @Bean
    public Market market() {
        return new Market();
    }
}

How I want to change my code:( REASON: i dont want

Item item = new Item(); 

in then method. I want Spring to inject it into Market)

class market {

    public Item item;
    //getters and setters for item

    public ArrayList<Items> createItems(HashMap<String,String> map) {
        ArrayList<Items> array = new ArrayList<Items>();
        for (Map.Entry<String, String> m : map.entrySet()) {
             item.setName(m.key());
             item.setValue(m.value());
             array.add(item);
        }
        return array;
    }
}

@Configuration
public class MarketConfig {

    @Bean
    @Scope("prototype")
    public Item item() {
        return new Item();
    }

    @Bean
    public Market market() {
        Market bean = new Market();
        bean.setItem(item());
    }
}

I know that prototype scope will give me new bean each time i call item();
Now i want new bean for each iteration in the for loop of createItems method. How can i tell spring to give me.

One way i know is Do

applicationContext context = new AnnotationConfigApplicationContext();
context.getBean(Item.class);

But is there any other way to get my work done.
Thanks

  • 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-06-19T01:32:28+00:00Added an answer on June 19, 2026 at 1:32 am

    Yes, you can create prototype method on demand using look-up method

    public abstract class ItemFactory {
    
        public abstract Item createItem();
    
    }
    

    now in applicationContext.xml just put the following:

    <bean id="item" class="x.y.z.Item" scope="prototype" lazy-init="true"/>
    

    and configure factory:

    <bean id="itemFactory" class="x.y.z.ItemFactory">
    <lookup-method name="createItem" bean="item"/>
    </bean>
    

    Now all that you need to do in order to use it is Autowire it inside any bean:

    and call yout look-up method:

    @Service 
    public class MyService{
    
       @Autowired
       ItemFactory itemFactory;
    
       public someMethod(){
          Item item = itemFactrory.createItem();
       } 
    
    }
    

    each time you call createItem() you will receive the reference to newly created instance of Item class.

    P.S: I see that you are using @Configuration instead of xml you need check if look-up method can be configured inside configuration bean.

    Hope it helps.

    Update: The trick is simple:

    @Configuration
    public class BeanConfig {
    
        @Bean
        @Scope(value="prototype")
        public Item item(){
            return new Item();
        }
    
    
        @Bean
        public ItemManager itemManager(){
            return new ItemManager() {
    
                @Override
                public Item createItem() {
                    return item();
                }
            };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem here. Right now I'm doing my Web Based Application, my
Here's my question. Right now I have a Linux server application (written using C++
Here is the thing. Right now I have this e-commerce web site where people
I have this code right here, where the $friends variable is an array with
I have the following issue.I have a camera with lookAt method which works fine.I
I have a query which works for every order except one. Here's the part
i have this code right here: <select name=group> <option value=>Choose a group....</option> <?php foreach($groups
I have this form right here, where the user will add the start date
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
OK, I don't think the title says it right... but here goes: I have

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.