我是SpringBoot的新手。我不知道如何以能够稍后在控制器中使用此对象的方式创建一些相同类型的对象。
假设我想在应用程序启动时创建对象的集合/列表(比如说Rabbits的集合):
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
// I would like to create here a collection
// or a list of objects (let's say collection of Rabbits)
}
}
我希望以后可以在控制器中使用此对象来获取一些信息(例如,通过列表中的索引获得)。
在没有数据库的情况下保持模型状态的正确方法是什么?
答案 0 :(得分:1)
使用spring,您需要在spring的上下文中创建对象,否则spring创建的实例将找不到它们。一种解决方案是将它们放在带有@Configuration
注释的类中,并使用注释@Autowired
检索它们。
注释@Configuration
:
表示一个类声明了一个或多个 @Bean方法,并且可以由Spring容器进行处理以生成Bean定义,并在运行时为这些Bean提供服务请求。 / p>
您可以按以下方式使用它:
@Configuration
public class MyConfiguration {
@Bean
public MyClass getMyClass() {
MyClass myClass = ...
...
return myClass;
}
}
@Controller
public class MyController {
@Autowired
private MyClass myClass;
public void method() {
// Use myClass instance as you like
}
}
您还可以生成标准的java.util.List
,但是在这种情况下,最好为生成的bean命名,例如:
@Configuration
public class MyConfiguration {
@Bean("myname") // To give an explicit name to the List
public List getMyList() {
List myList = ...
...
return myList;
}
}
@Controller
public class MyController {
@Autowired
@Qualifier("myname") // To retrieve a List with a specific name
private List myList;
public void method() {
// Use myList instance as you like
}
}
答案 1 :(得分:1)
忽略同步问题。
您可以创建一个列表并注入到控制器中。
或者我想做的是将其包装在存储库中。这使您与基础数据源隔离开来,以后可以更改它。
请注意,同步对于这种类型的数据结构非常重要,因为您可以有许多线程来更新存储库。
package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
@Bean
public RabbitRepository rabbitRepository() {
RabbitRepository rabbitRepository = new RabbitRepository();
rabbitRepository.add("Bugs");
rabbitRepository.add("Flopsy");
return rabbitRepository;
}
public static class RabbitRepository {
private List<Rabbit> rabbits = Collections.synchronizedList(new ArrayList<Rabbit>());
public List<Rabbit> getAll() {
return rabbits;
}
public Rabbit add(String rabbitName) {
Rabbit rabbit = new Rabbit(rabbitName);
this.rabbits.add(rabbit);
return rabbit;
}
public Optional<Rabbit> findById(int id) {
return this.rabbits.stream().filter(r-> r.getId() == id).findFirst();
}
}
public static class Rabbit {
private final String name;
private final int id;
private static AtomicInteger counter = new AtomicInteger();
public Rabbit(String name) {
super();
this.name = name;
this.id = counter.incrementAndGet();
}
public String getName() {
return name;
}
public int getId() {
return this.id;
}
}
@RestController
@RequestMapping("/rabbits")
public static class RabbitController {
private final RabbitRepository repository;
public RabbitController(final RabbitRepository repository) {
this.repository = repository;
}
@GetMapping
public List<Rabbit> getAll() {
return repository.getAll();
}
@PostMapping("/{name}")
//You can also use requestparam / requestbody and probably should
public Rabbit addRabbit(@PathVariable("name") String name) {
return repository.add(name);
}
@GetMapping("/id/{id}")
public Optional<Rabbit> findById(@PathVariable("id") int id) {
return repository.findById(id);
}
}
}
卷曲测试
➜ ~ curl localhost:8080/rabbits
[{"name":"Bugs","id":1},{"name":"Flopsy","id":2}]%
➜ ~ curl localhost:8080/rabbits/id/2
{"name":"Flopsy","id":2}%
➜ ~ curl -XPOST localhost:8080/rabbits/Babs
{"name":"Babs","id":3}%
➜ ~ curl localhost:8080/rabbits
[{"name":"Bugs","id":1},{"name":"Flopsy","id":2},{"name":"Babs","id":3}]%
答案 2 :(得分:0)
我猜你需要这样的东西!
@Component
public class RabbitListHolder {
private List<Rabbit> rabbits = new ArrayList<Rabbit>
public void initializeList(){
rabbits.add(new Rabbit('white', 3));
...
}
}
@Controller
public class RabbitsRessource{
@Autowired
RabbitListHolder rabbitListHolder;
...
@GetMapping("/rabbits")
public List<Rabbits> getRabbits(){
rabbitListHolder.initializeList();
return rabbitListHolder.getRabbits();
}
}