I'm new with Spring Boot.
I want to separate the domain from the application, so, to do that I need to avoid annotations in the domain layer and I would like to know how to configure a class as a Service (without @Service
annotation) and inject a class with dependency injection without @Autowired
?
The domain does not need to know that. I just want to write a configuration for the application and declare my services.
For example I have this query (from CQRS pattern) and I want to configure it as a service inside the application. Same thing with the @Autowired
annotation.
package fr.rtransat.tvtracker.model.application.query.actor
import fr.rtransat.tvtracker.domain.actor.ActorRepository
import fr.rtransat.tvtracker.domain.actor.exceptions.ActorNotFoundException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class GetActorByUuidQuery(
@Autowired
private val actorRepository: ActorRepository
) {
fun handle(request: GetActorByUuidRequest): GetActorByUuidResponse {
val actor = actorRepository.findByUuid(request.uuid)
if (actor === null) {
throw ActorNotFoundException(request.uuid)
}
return GetActorByUuidResponse(actor)
}
}
You don't have to use annotations like @Autowired
and @Service
in classes where you don't want to use them. Instead, you can use the configuration class with the annotation @Configuration
. For example:
@Configuration
open class MyConfiguration {
@Bean
open fun getActorByUuidQuery(actorRepository: ActorRepository): GetActorByUuidQuery {
return GetActorByUuidQuery(actorRepository)
}
}
This will allow you to keep the class GetActorByUuidQuery
as a POJO class.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments