I've created a server-side application in Kotlin, but it's not yet properly configured. Create a controller class with "Kotorin and Spring Mone-- 2. RestController and Data Class", and the configuration is still such that data is handled directly. Therefore, we will make changes little by little. First, let's consider the "** service **" layer.
Web applications are roughly divided into the following functions.
--Request / response processing --Processing of application logic --Data processing
Until now, these functions were handled in a single layer. With this configuration, the logic to handle increases and the data model becomes complicated, One class becomes huge and development / maintenance efficiency deteriorates.
Separate each function and divide it into multiple layers.
By the way, the class I was making is RestController
,
Originally, it is the responsibility to handle request / response.
Therefore, the other processing is cut out and it is designated as ** service **.
Spring Dependencies
Originally, I was doing things other than request / response processing such as data generation in the controller class as follows.
@RestController
@RequestMapping("/messages")
class SimpleController {
@GetMapping
fun getMessages() : List<Message> {
return listOf(
Message(
UUID.randomUUID().toString(),
"First Message",
"This is a 1st message on ${getDate()}."
),
Message(UUID.randomUUID().toString(),
"Second Message",
"This is a 2nd message on ${getDate()}."
)
)
}
private fun getDate() : String {
val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
return simpleDateFormat.format(Date())
}
}
Move the contents originally implemented in the controller class to the service class as follows.
@Service("Message Service")
class MessageService {
fun getMessages() : List<Message> {
return listOf(
Message(
UUID.randomUUID().toString(),
"First Message",
"This is a 1st message on ${getDate()}."
),
Message(UUID.randomUUID().toString(),
"Second Message",
"This is a 2nd message on ${getDate()}."
)
)
}
private fun getDate() : String {
val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
return simpleDateFormat.format(Date())
}
}
Annotate the service class with @Service
.
This makes it available by Dependency Injection in the controller class.
@Autowired
private lateinit var service: MessageService
Use the lateinit
keyword to declare an instance without initializing it.
In the controller class that uses the service class, only the processing related to the request / response should be handled. Make the following changes to call the process implemented in the service class.
@RestController
@RequestMapping("/messages")
class MessageController() {
@Autowired
private lateinit var service: MessageService
@GetMapping
fun getMessages() = service.getMessages()
}
Developing applications with separate layers for each responsibility is now a standard. Still, in this state, data processing is performed directly from the service layer, so Next, we will separate that part.
Recommended Posts