如何使用spring java配置在单例bean中生成原型对象 [英] Howto generate prototype objects with in a singleton bean using spring java configurations
问题描述
以下是我现在的工作正常。所有这一切都是一个市场类,它返回一个项目对象数组:
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 for name的简单类和价值
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();
然后方法。我希望Spring将它注入市场)
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());
}
}
我知道原型范围会给我每个新的bean我称之为item();
现在我想在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
推荐答案
是的,您可以使用查找方法按需创建原型方法
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:
并打电话给你查询方法:
and call yout look-up method:
@Service
public class MyService{
@Autowired
ItemFactory itemFactory;
public someMethod(){
Item item = itemFactrory.createItem();
}
}
$ c> createItem()您将收到对新创建的Item类实例的引用。
each time you call createItem()
you will receive the reference to newly created instance of Item class.
PS:我看到你正在使用 @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屋!