使用Spring依赖注入进行运行时生成的依赖项

huangapple go评论77阅读模式
英文:

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&lt;Event&gt; events = EventsFetcher.fetchEvent();
   List&lt;String&gt; myTextBook = new ArrayList&lt;String&gt;();
   events.forEach ( event -&gt; {
      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>



huangapple
  • 本文由 发表于 2020年9月5日 18:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63753025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定