Jon Senchyna
03/17/2023, 2:59 PMobject
as a bean in Spring and am wondering if there's a simpler way to do this. Why I'm doing this is a long story.
What I've tried so far:
1. Annotate my object with @Component
(results in Spring creating a second instance of my object)
2. Create a separate @Configuration
class and expose my object as an @Bean
in a method in there.object MyObject : SomeDependencyInterface {
// ...
}
@Configuration
class RegisterMyObject {
@Bean
fun myObject() = MyObject
}
@Configuration
class and just register my object as a @Component
.Sam
03/17/2023, 3:20 PMDavio
03/17/2023, 3:28 PMashmelev
03/17/2023, 3:30 PMJon Senchyna
03/17/2023, 4:10 PMvbsteven
03/17/2023, 4:24 PM@Configuration
class seems the most sensible, but I would modify it slightly and add the interface as an explicit return type of the bean method so the actual object can be private/internal
private object MyObject : SomeDependencyInterface {
// ...
}
@Configuration
class RegisterMyObject {
@Bean
fun myObject(): SomeDependencyInterface = MyObject
}
SomeDependencyInterface
and not MyObject
.Jon Senchyna
03/17/2023, 5:18 PMUmit AYDIN
03/18/2023, 9:37 AM