Chapter 12
[ 343 ]
Integrating with Spring Boot server
Spring Boot application startup is via the main method of a class. You can put
this main method in any class you like. You can build a functioning Spring Boot
application with a single controller class that has a main method. A common
convention, however, is to create a separate application class that contains the
main method, which is what we have done here:
@EnableAutoConfiguration
@ComponentScan("com.dearle.game.engine.tictactoe")
class Application {
static void main(String[] args){
new SpringApplication(Application).run(args)
}
}
In the main method, we create the SpringApplication class and run it. The
configuration of components is made easy by using the @ComponentScan annotation,
which tells Spring Boot to search in the tictactoe package for component classes.
It is important that you only have one main method in the application
JAR. If you include any DSL script classes in the JAR they will also have
a main method. This is why the DSL AST transform takes care to remove
the script main as part of the transformation.
The main work of the server is performed by a single controller class. The component
scan will discover that GameEventController has the @RestController annotation
which means that this controller is prepared to handle web requests.
I mentioned earlier that Spring Boot takes convention over configuration. The
@AutoWired annotation informs Spring to create a bean for GameSessionRepository
and GameEngineService and the instance into the controller. No further
configuration is required:
@RestController
class GameEventController {
@Autowired
GameSessionRepository sessionRepo
@Autowired
GameEngineService engineService
@RequestMapping(value = "/{game}/{event}",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody