如何在Spring Boot中共享ArrayList

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

How to share the ArrayList across spring boot

问题

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

  1. 我有一个使用AWS Kinesis流的Java应用程序
  2. 在应用程序中,我通过Kinesis流实时获取学生数据,然后根据学生ID执行某些操作
  3. 我需要从数据库中查询一组学生,并需要检查Kinesis流中的每个学生是否在列表中
  4. 然而,我希望只查询一次学生列表,因为这是要求之一。
  5. 此外,我希望在应用程序启动时尽快执行查询;这样,如果查询失败,我们也可以使应用程序失败。换句话说,如果查询不成功,应用程序不应运行。
  6. 这是我想要做的:
  7. ```groovy
  8. @SpringBootApplication
  9. @EnableScheduling
  10. @EnableCaching
  11. @Configuration
  12. class Application implements CommandLineRunner {
  13. @Autowired
  14. RestTemplate restTemplate
  15. @Autowired
  16. List<Student> students
  17. static void main(String[] args) {
  18. SpringApplication app = new SpringApplication(Application)
  19. app.webApplicationType = WebApplicationType.NONE
  20. app.run(args)
  21. }
  22. @Override
  23. void run(String... args) throws Exception {
  24. try {
  25. // 发出请求
  26. List<Student> people = this.restTemplate.exchange("https://some.url.com", ...其他参数)
  27. this.students = people
  28. // 运行KCL工作者
  29. KclConsumer.kclWorker.run()
  30. } catch (Exception ex) {
  31. // ...
  32. }
  33. }
  34. }
  1. @Service
  2. class SomeService {
  3. @Autowired
  4. RestTemplate restTemplate
  5. @Autowired
  6. List<Student> students
  7. void func(Student id) {
  8. this.students.each {
  9. if (id == it.id) {
  10. println("找到你了")
  11. return
  12. }
  13. }
  14. println("未找到")
  15. }
  16. }

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

  1. class SomeConfig {
  2. @Bean
  3. List<Student> students() {
  4. return new ArrayList<>();
  5. }
  6. }

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

  1. <details>
  2. <summary>英文:</summary>
  3. I have a java application that uses aws kinesis stream.
  4. 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.
  5. 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.
  6. However I would like to query the list of students to the database only once, because that is one of the requirements.
  7. 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.
  8. This is what I would like to do
  9. ```groovy
  10. @SpringBootApplication
  11. @EnabledScheduling
  12. @EnableCaching
  13. @Configuration
  14. class Application implements CommandLineRuner {
  15. @Autowired
  16. RestTemplate restTemplate
  17. @Autowired
  18. List&lt;Student&gt; students
  19. static void main(String[] args) {
  20. SpringApplication app = new SpringApplication(Application);
  21. app.webApplicationType = WebApplicationType.NONE;
  22. app.run(args)
  23. }
  24. @Overrde
  25. void run(String... args) thows Exception {
  26. try {
  27. // make a request
  28. List&lt;Student&gt; people = this.restTemplate.exchange(&quot;https://some.url.com&quot;, ...other params)
  29. this.students = people
  30. // running kcl worker
  31. KclConsumer.kclWorker.run()
  32. } catch (Exception ex) {
  33. ....
  34. }
  35. }
  36. }
  1. @Service
  2. class SomeService {
  3. @Autowired
  4. RestTemplate restTemplate
  5. @Autowired
  6. List&lt;Student&gt; students
  7. void func(Student id) {
  8. this.students.each {
  9. if(id == it.id) {
  10. println(&quot;found you&quot;)
  11. return;
  12. }
  13. }
  14. println(&quot;not found&quot;)
  15. }
  16. }

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

  1. class SomeConfig {
  2. @Bean
  3. List&lt;Student&gt; students() {
  4. return new ArrayList&lt;&gt;();
  5. }
  6. }

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.

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

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.

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

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:

确定