Spring boot, declare Service and inject class without annotation

Rtransat

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)
    }
}
Maksim Eliseev

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.

edited at
0

Comments

0 comments
Login to comment

Related

spring boot not able to inject spring service in spring test class

Spring inject without autowire annotation

How inject a Spring-boot service into a aspectj class?

@Autowired annotation without spring boot

Spring boot- inject service into doFiler

Unable to inject a @Service in a Spring Boot Test

Junit Test in Spring Boot does not inject the service

Creating KafkaListener without Annotation & without Spring Boot

How to inject config properties in Spring Boot to Spring Retry annotation?

In Spring Boot, Can we add @Service Annotation for Interface? if that interface is not implemented by any class

Spring Boot tests: exclude one specific @Configuration class without using the @Profile annotation

Spring boot - Service class is null

How to declare Transaction bean in spring in class based annotation?

Spring Boot - Hibernate custom constraint doesn't inject Service

How to declare `@Transactional` in a Spring Boot service so exception is thrown "immediately"?

How to inject a service in parent class without passing it in constructor? Angular 6

Java: Junit a class with Inject annotation

Eureka service discovery without Spring-boot

Testing a spring boot service without running @SpringBootApplication

Spring Boot: how to inject dependencies into a class called by a library?

Optaplanner + Spring Boot. Is it possible to inject bean in ConstraintProvider implementation class?

Spring boot applicaion what is the correct naming and annotation for my service

Spring Boot: How to test a service in JUnit with @Validated annotation?

Spring - Inject proper service implementation in abstract class level autowired field

How to inject a service component in Spring Test class using Kotlin?

Spring boot - @Service class calling another @Service class

Spring Boot testing with different service class

Null pointer Exception in Service class Spring boot

Spring boot test unable to autowire service class