英文:
using spring dependency injection for runtime generated dependencies
问题
我刚接触Spring,并且为了更好地理解我正在学习的内容,我决定将Spring集成到我的一个项目中。在这个项目中,会在运行时生成一系列的事件(Event),其中Event是一个POJO类。FetcherA和FetcherB是两个类,它们依赖于Event实例来获取它们的输出,这意味着FetcherA和FetcherB的输出因不同的Event实例而异。
由于这些事件是在运行时生成的,我应该如何使用Spring依赖注入,来将一个事件Bean注入到每个在运行时创建的FetcherA和FetcherB对象中呢?下面是一个示例,展示了我的代码是什么样子的。
public class Event {
//一些数据字段
}
public class FetcherA {
private Event event;
FetcherA(Event event) {
this.event = event;
}
public String fetch() {
//基于这个事件获取输出
}
}
public class FetcherB {
private Event event;
FetcherB(Event event) {
this.event = event;
}
public String fetch() {
//基于这个事件获取输出
}
}
public static void main(String[] args) {
List<Event> events = EventsFetcher.fetchEvent();
List<String> myTextBook = new ArrayList<String>();
events.forEach(event -> {
String messageBody = new FetcherA(event).fetch();
String messageTitle = new FetcherB(event).fetch();
myTextBook.add(messageBody);
myTextBook.add(messageTitle);
});
}
英文:
I am new to Spring and to better understand what I'm learning, I decide to integrate Spring into one of my projects. In the project, a collection of Events is generated at runtime, where Event is a POJO. FetcherA and FetcherB are two classes that depended on an Event instance to fetch their output, which means the output of FetcherA and FetcherB is different for different Event instances.
Since these Events are generated at runtime, how can I use Spring dependency injection to inject an event bean into every FetcherA and FetcherB object created at runtime. Below is an example of what my code looks like.
public class Event {
//some data fields
}
public class FetcherA {
private Event event;
FetcherA (Event event) {this.event=event}
public String fetch () {//fetch output based on this event}
}
public class FetcherB {
private Event event;
FetcherB (Event event) {this.event=event}
public String fetch () {//fetch output based on this event}
}
public static void main (String[] args) {
List<Event> events = EventsFetcher.fetchEvent();
List<String> myTextBook = new ArrayList<String>();
events.forEach ( event -> {
String messageBody= new FetcherA (event).fetch();
String messageTitle = new FetcherB (event).fetch();
myTextBook.add(messageBody);
myTextBook.add(messageTitle);
});
} ```
</details>
# 答案1
**得分**: 2
在您的用例中,`Event`、`FetcherA`或者`FetcherB`都不应该由Spring进行管理,也就是说它们不应该是Spring的bean。
如果将`Fetch`字段移到`fetch()`方法的参数中,这将允许`FetcherX`类都成为*singleton(单例)* bean。
您也可以选择坚持原来方式,这样`FetcherX`类将成为*prototype(原型)* bean,您的代码将与Spring容器集成,在循环内请求新的实例。但这并不是最优的解决方案。
<details>
<summary>英文:</summary>
In your use case, none of `Event`, `FetcherA`, or `FetcherB` should be Spring-managed, i.e. they should not be Spring beans.
If you moved the `Fetch` field to be a parameter to the `fetch()` method, that would allow both `FetcherX` classes to be *singleton* beans.
You could insist, in which case the `FetcherX` classes would be *prototype* beans, and your code would integrate with the spring container to ask for new instances inside the loop. Not really optimal.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论