0

Currently I have an endpoint in my controller that calls different methods from my service. Each of these methods recieve a parameter called executionId that is different for every http request.

@RestController
public class ProcedureController {

    @Autowired
    MyService service;

    @GetMapping
    public void execute(){
        String executionId = ...; //randomly generated

        service.executeMethodA(executionId);
        service.executeMethodB(executionId);
    }

}

@Service
public class MyService {

    public void executeMethodA(String executionId){
        ...
    }

    public void executeMethodB(String executionId){
        ...
    }

}

It works fine but it seems very repetitive to pass executionId as a parameter to each method. So one solution that I thought was to instantiate my service with the executionId so it can hold this information and every method can access it.

@RestController
public class ProcedureController {

    @GetMapping
    public void execute(){
        String executionId = ...; //randomly generated

        MyService service = new MyService(executionId);

        service.executeMethodA();
        service.executeMethodB();
    }

}

@Service
public class MyService {

    String executionId;

    public MyService(String executionId){
        this.executionId = executionId;
    }

    public void executeMethodA(){
        // Use executionId here
        ...
    }

    public void executeMethodB(){
        // Use executionId here
        ...
    }

}

Is this solution a bad practice? If so, how can I achieve something similar?

Gustavo Cesário
  • 1,310
  • 1
  • 12
  • 23
  • I hope this [**answer**](https://stackoverflow.com/questions/6739566/is-there-a-way-to-autowire-a-bean-that-requires-constructor-arguments) will help you to understand – Vivek Jain Mar 23 '22 at 18:42
  • @VivekJain With this solution it doesn't look like it's possible to construct my service with a dynamic parameter for each http request made to the api. – Gustavo Cesário Mar 23 '22 at 19:02
  • try to set scope of your bean as request, I hope this should work for you. – Vivek Jain Mar 23 '22 at 19:31
  • 1
    visit this [page](https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/request-scope.html) – Vivek Jain Mar 23 '22 at 19:37
  • @VivekJain it worked perfectly. If you want to, please edit you answer to refer to this last page you sent. – Gustavo Cesário Mar 31 '22 at 18:01

1 Answers1

0

Spring allows you to inject a managed object (bean) as a dependency into another object via the @Autowired annotation.

For example, if I have a UserService that has a dependency on UserRepository, I can have the UserRepository injected using @Autowired annotation like this:

UserRepository Class

class UserRepository {
UserRepository () {}
}

UserService Class

class UserService {

@Autowired
private UserRepository userRepository;

UserService () {}

}

This is done using Field Injection. The same thing can be accomplice using Setter Injection:

class UserService {

private UserRepository userRepository;

UserService () {}

@Autowired // Using setter injection
public void setUserRepository(
UserRepository userRepository) {
this.userRepository = userRepository
}

}

or via Constructor Injection:

class UserService {

private UserRepository userRepository;

@Autowired // Using constructor Injection
UserService (UserRepository userRepository) {
this.userRepository = userRepository
}

}
Vivek Jain
  • 317
  • 3
  • 12