Spring boot 在Utils类[Spring和Kotlin]中使用服务方法
我曾经遇到过一个众所周知的场景,在这个场景中,我确实需要将一些实用程序私有函数(一开始就不应该存在)从我的服务类移动到实用程序类。我的问题是在实用程序类中使用服务方法。我尝试了以下重构:Spring boot 在Utils类[Spring和Kotlin]中使用服务方法,spring-boot,kotlin,utility-method,Spring Boot,Kotlin,Utility Method,我曾经遇到过一个众所周知的场景,在这个场景中,我确实需要将一些实用程序私有函数(一开始就不应该存在)从我的服务类移动到实用程序类。我的问题是在实用程序类中使用服务方法。我尝试了以下重构: class Utils( val serviceIneed : ServiceIneed) { companion object { private val someMapper = ObjectMapperConfig.mapper } fun someUtility():
class Utils(
val serviceIneed : ServiceIneed) {
companion object {
private val someMapper = ObjectMapperConfig.mapper
}
fun someUtility(): ResponseEntity<Any> {
// lots of stuff
return serviceIneed.someFunction()
}
}
这是正确的方法吗?您能推荐任何重构服务类的方法,使我的服务类中只保留面向服务的方法而不保留辅助方法吗
谢谢您需要将它们移出服务类别定义的原因是什么?可重用性?我这样问是因为这会影响我如何进行设计。另外,您提到它们是私有函数,但它们在上面似乎不是私有的。主要是为了避免冗长而混乱的服务类,因为这不是一个好的做法。我的示例显示了一种可能的方法,因此它没有私有函数,而私有函数现在是实用程序中的someUtility()函数Class@Tenfour04您认为上述方法是推荐的方法还是您会采取不同的方法?机具本身可以工作,因此与实际工作部件无关。
class anotherService(
private val serviceIneed: ServiceIneed
) {
fun someMethod() {
// lots of things happening
val utilityUsage = Utils(serviceIneed).someUtility()
}
}