如何在Spring Boot中共享ArrayList

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

How to share the ArrayList across spring boot

问题

以下是您要的代码部分的翻译:

我有一个使用AWS Kinesis流的Java应用程序

在应用程序中,我通过Kinesis流实时获取学生数据,然后根据学生ID执行某些操作

我需要从数据库中查询一组学生,并需要检查Kinesis流中的每个学生是否在列表中

然而,我希望只查询一次学生列表,因为这是要求之一。

此外,我希望在应用程序启动时尽快执行查询;这样,如果查询失败,我们也可以使应用程序失败。换句话说,如果查询不成功,应用程序不应运行。

这是我想要做的:

```groovy
@SpringBootApplication
@EnableScheduling
@EnableCaching
@Configuration
class Application implements CommandLineRunner {

  @Autowired
  RestTemplate restTemplate

  @Autowired
  List<Student> students

  static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application)
    app.webApplicationType = WebApplicationType.NONE
    app.run(args)
  }

  @Override
  void run(String... args) throws Exception {
    try {
      // 发出请求
      List<Student> people = this.restTemplate.exchange("https://some.url.com", ...其他参数)
      this.students = people

      // 运行KCL工作者
      KclConsumer.kclWorker.run()
    } catch (Exception ex) {
      // ...
    }
  }
}
@Service
class SomeService {

  @Autowired
  RestTemplate restTemplate

  @Autowired
  List<Student> students

  void func(Student id) {
    this.students.each {
      if (id == it.id) {
        println("找到你了")
        return
      }
    }
    println("未找到")
  }
}

我尝试创建Bean并在整个项目中共享,但似乎不起作用...

class SomeConfig {
  @Bean
  List<Student> students() {
    return new ArrayList<>();
  }
}

我猜想我使用Bean的方式有问题。有人可以帮助我检查我的代码吗?
非常感谢!


<details>
<summary>英文:</summary>

I have a java application that uses aws kinesis stream. 

In the application, I get the student data in real time via kinesis stream and I have to make some action depending on the student id. 

There is a list of students that I need to query from the database, and the each student from kinesis stream needs a checking if the student is in the list or not. 

However I would like to query the list of students to the database only once, because that is one of the requirements. 

On top of that I would like to make the query as soon as the application starts; that way if the query fails we can also fail the application. In other words, if the query does not succeed the application should not run.

This is what I would like to do

```groovy
@SpringBootApplication
@EnabledScheduling
@EnableCaching
@Configuration
class Application implements CommandLineRuner {

  @Autowired
  RestTemplate restTemplate

  @Autowired
  List&lt;Student&gt; students

  static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application);
    app.webApplicationType = WebApplicationType.NONE;
    app.run(args)
  }

  @Overrde
  void run(String... args) thows Exception {
    try {
      // make a request
      List&lt;Student&gt; people = this.restTemplate.exchange(&quot;https://some.url.com&quot;, ...other params)
      this.students = people

      // running kcl worker
      KclConsumer.kclWorker.run()
    } catch (Exception ex) {
      ....
    }
  }
}
@Service
class SomeService {

  @Autowired
  RestTemplate restTemplate

  @Autowired
  List&lt;Student&gt; students

  void func(Student id) {
    this.students.each {
      if(id == it.id) {
        println(&quot;found you&quot;)
        return;
      }
    }
    println(&quot;not found&quot;)
  }
}

I have tried creating the bean and share across the project as following but it does not seem to work...

class SomeConfig {
  @Bean
  List&lt;Student&gt; students() {
    return new ArrayList&lt;&gt;();
  }
}

I am guess how I am using bean is wrong.
Can someone help me with my code please?
Thank you in advance

答案1

得分: 0

In theory, providing it in a bean should work, but you should be sure to initialize its data in the bean.

@Bean
public List<Student> getStudents() {
RestTemplate restTemplate = new RestTemplate();
List<Student> people = restTemplate.exchange("https://some.url.com", ...other params);
return people;
}

This bean should be a singleton by default, so it should be the same list provided anywhere else a List&lt;Student&gt; is injected (either through constructor injection or with @Autowired).

If you are needing to use the RestTemplate in other places, you can make that a bean as well, which I believe you’ve already done, and inject it via constructor.

@Bean
public List<Student> getStudents(RestTemplate restTemplate) {
List<Student> people = restTemplate.exchange("https://some.url.com", ...other params);
return people.
}

Edit: I did a bit more research and it seems to make the list accessible, you may need to use @Resource instead of @Autowired as suggested here how to define a list bean in spring

英文:

In theory, providing it in a bean should work, but you should be sure to initialize its data in the bean.

@Bean
public List&lt;Student&gt; getStudents() {
    RestTemplate restTemplate = new RestTemplate();
    List&lt;Student&gt; people = restTemplate.exchange(&quot;https://some.url.com&quot;, ...other params);
    return people;
}

This bean should be a singleton by default, so it should be the same list provided anywhere else a List&lt;Student&gt; is injected (either through constructor injection or with @Autowired)

If you are needing to use the RestTemplate in other places, you can make that a bean as well, which I believe you’ve already done, and inject it via constructor.

@Bean
public List&lt;Student&gt; getStudents(RestTemplate restTemplate) {
    List&lt;Student&gt; people = restTemplate.exchange(&quot;https://some.url.com&quot;, ...other params);
    return people;
}

Edit: I did a bit more research and it seems to make the list accessible, you may need to use @Resource instead of @Autowired as suggested here
how to define a list bean in spring

答案2

得分: 0

不要制作List&lt;Student&gt;的bean。创建一个带有public List&lt;Student&gt; getStudents()方法的业务类 - 这个类应该是一个bean。

英文:

Don't make a bean of List&lt;Student&gt;. Make a business class with a public List&lt;Student&gt; getStudents() method - this class should be a bean.

huangapple
  • 本文由 发表于 2023年3月4日 09:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633154.html
匿名

发表评论

匿名网友

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

确定