简体   繁体   English

创建通过方法初始化的Spring bean List

[英]create spring bean List initializing by the method

I have the method: 我有方法:

public List<IEntity> wordsCollection()

I need to create the bean with that list. 我需要使用该列表创建bean。 This method ask DB for data and gets it out. 此方法向DB索取数据并将其取出。 When I used method call - I do call several times. 当我使用方法调用时-我会多次调用。 But I need the bean with that value. 但是我需要具有该值的bean。 How can I do this? 我怎样才能做到这一点?

I need something like this: 我需要这样的东西:

<util:list id ="wc">
   value = collections.wordsCollection
</util:list>

You can use @PostConstruct to load the data in your spring bean 您可以使用@PostConstruct将数据加载到Spring bean中

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. PostConstruct批注用于需要依赖注入完成以执行任何初始化之后需要执行的方法上。

public class SomeService{

  @PostConstruct
  public void loadCollection() throws Exception {
      wordsCollection();   
 }
}

Inject your dao in the spring bean and call the required method in the postconstruct method. 将您的dao注入spring bean中,并在postconstruct方法中调用所需的方法。

you can store the retrived list in the session and reuse it among spring controllers when required. 您可以将检索到的列表存储在会话中,并在需要时在spring控制器之间重用。 Otherwise you can use Spring's expression language(SpEL). 否则,您可以使用Spring的表达语言(SpEL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM