admin管理员组文章数量:1297086
The methods or classes in Kotlin are final by default. But when I mark a method in a Service class to be @Transactional
, I realised the service class and all its methods are open. Because for the @Transactional
aspect to work, the service class needs to be proxied, then it must be an open class so that the proxy class can extend the original class.
(As the tutorial Open Keyword in Kotlin points out, kotlin is final by default.
According to the Spring documentation AOP, because my service class inherits from an abstract class instead of an interface, so the proxy is a CGLIB proxy.)
Did I misunderstand anything or how does it work?
@Service
class SomeService(...): SomeAbstractClass(...){
@Transactional
fun someMethod(...){...}
}
The methods or classes in Kotlin are final by default. But when I mark a method in a Service class to be @Transactional
, I realised the service class and all its methods are open. Because for the @Transactional
aspect to work, the service class needs to be proxied, then it must be an open class so that the proxy class can extend the original class.
(As the tutorial Open Keyword in Kotlin points out, kotlin is final by default.
According to the Spring documentation AOP, because my service class inherits from an abstract class instead of an interface, so the proxy is a CGLIB proxy.)
Did I misunderstand anything or how does it work?
@Service
class SomeService(...): SomeAbstractClass(...){
@Transactional
fun someMethod(...){...}
}
Share
Improve this question
asked Feb 11 at 18:25
user12372439user12372439
231 silver badge4 bronze badges
1 Answer
Reset to default 3Indeed, Spring automatically configures Service classes, etc to be open.
Because this is to important to the way Spring and other frameworks work, Kotlin documentation talks about it here: All-open compiler plugin
Kotlin has classes and their members final by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be
open
. Theall-open
compiler plugin adapts Kotlin to the requirements of those frameworks and makes classes annotated with a specific annotation and their members open without the explicitopen
keyword.For instance, when you use Spring, you don't need all the classes to be open, but only classes annotated with specific annotations like
@Configuration
or@Service
. Theall-open
plugin allows you to specify such annotations.Kotlin provides
all-open
plugin support both for Gradle and Maven with the complete IDE integration.For Spring, you can use the
kotlin-spring
compiler plugin.
本文标签: How does Spring make a service class open I thought Kotlin is final by defaultStack Overflow
版权声明:本文标题:How does Spring make a service class open? I thought Kotlin is final by default - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641664a2389952.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论