如何使用 spring java 配置在单例 bean 中生成原型对象 [英] Howto generate prototype objects with in a singleton bean using spring java configurations

查看:27
本文介绍了如何使用 spring java 配置在单例 bean 中生成原型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在所拥有的,效果很好.它所做的只是一个市场类,它返回一个项目对象数组:

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

我有班级市场

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 是一个简单的类,带有名称和值的 getter 和 setter

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(); 

在 then 方法中.我想让 Spring 把它注入 Market)

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());
    }
}

我知道每次调用 item() 时,prototype 作用域都会给我新的 bean;现在我想要在 createItems 方法的 for 循环中为每次迭代创建新的 bean.我怎么能告诉春天给我.

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.

我知道的一种方法是做

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

但是有没有其他方法可以完成我的工作.谢谢

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

推荐答案

是的,可以使用look-up方法按需创建原型方法

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

public abstract class ItemFactory {

    public abstract Item createItem();

}

现在在 applicationContext.xml 中输入以下内容:

now in applicationContext.xml just put the following:

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

并配置工厂:

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

现在你需要做的就是在任何 bean 中自动装配它:

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

并调用您的查找方法:

@Service 
public class MyService{

   @Autowired
   ItemFactory itemFactory;

   public someMethod(){
      Item item = itemFactrory.createItem();
   } 

}

每次调用 createItem() 时,您都会收到对新创建的 Item 类实例的引用.

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

P.S: 我看到您使用的是 @Configuration 而不是 xml,您需要检查是否可以在配置 bean 中配置查找方法.

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.

希望有帮助.

更新:诀窍很简单:

@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();
            }
        };
    }
}

这篇关于如何使用 spring java 配置在单例 bean 中生成原型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆