Git Harpon
Git Harpon

Reputation: 171

Access a variable from all of my Controllers

I'm developing my first REST API with Spring Boot. It was an homework but I extended it to learn more. Actually, the code of the controller look like this :

@RestController
@EnableAutoConfiguration
@EnableSwagger2
@Api(description = "API pour l'ensemble des opérations")
public class MyServiceController {
public static void main(String[] args) throws Exception {
    SpringApplication.run(MyServiceController.class, args);
}

private Centers centers;

public MyServiceController() {
    Cage usa = new Cage(
            "usa",
            new Position(49.305d, 1.2157357d),
            25,
            new LinkedList<>(Arrays.asList(
                    new Animal("Tic", "usa", "Chipmunk", UUID.randomUUID()),
                    new Animal("Tac", "usa", "Chipmunk", UUID.randomUUID())
            ))
    );

    Cage amazon = new Cage(
            "amazon",
            new Position(49.305142d, 1.2154067d),
            15,
            new LinkedList<>(Arrays.asList(
                    new Animal("Canine", "amazon", "Piranha", UUID.randomUUID()),
                    new Animal("Incisive", "amazon", "Piranha", UUID.randomUUID()),
                    new Animal("Molaire", "amazon", "Piranha", UUID.randomUUID()),
                    new Animal("De lait", "amazon", "Piranha", UUID.randomUUID())
            ))
    );

    this.centers = new Centers();
    this.centers.addCenter(new Center(new LinkedList<>(),
            new Position(49.30494d, 1.2170602d), "Biotropica"));
    this.centers.getCenter()
            .stream()
            .findFirst()
            .get()
            .getCages()
            .addAll(Arrays.asList(usa, amazon));
}

// -----------------------------------------------------------------------------------------------------------------
// /animals
@ApiOperation(value="Récupère l'ensemble des animaux")
@RequestMapping(path = "/animals", method = GET, produces = APPLICATION_JSON_VALUE)
public Centers getAnimals(){
    return this.centers;
}...

So as you can see actually the centers are store in a variable (without persistence !). What I want now is to externalize some Operation (like every request on /animals to an AnimalsController...)

But here is my problem : I want to do it without persistence. I want my controller to access/update the same variable center, with the same stat and I do not really see how I proceed

Upvotes: 3

Views: 552

Answers (1)

Lino
Lino

Reputation: 19926

You can create a holder object, and register that to the spring lifecycle:

@Component
public class CentersHolder {
     private Centers centers;

     // getter + setter
}

And then autowire that component into every controller you want. (I suggest always using constructor injection, makes your code easier to test and generally safer)

private final CentersHolder holder;

@Autowired
public YourController(CentersHolder holder) {
    this.holder = holder;
}

By default every spring component/bean is a singleton unless you explicitly tell it not to be one. So all your controllers will share the same instance.

Upvotes: 3

Related Questions