Issue
I am using Springboot 3 with Java20.
I am looking for the simplest way to use a different implementation depending on the environment.
I have the following interface:
MyService
public interface MyService {
boolean send(String fileName, final String userName);
}
With two implementations:
MyService1Impl
@Service
@Qualifier("myService1Impl")
public class MyService1Impl implements MyService {
MyService2Impl
@Service
@Qualifier("myService2Impl")
public class MyService2Impl implements MyService {
Then I want to use the following:
@Autowired
private @Qualifier("myService1Impl") MyService myService1Impl;
@Autowired
private @Qualifier("myService2Impl") MyService myService2Impl;
// if @Profile("dev") use myService1Impl
// if @Profile("*") use myService2Impl
I would like to keep this as simple as possible (not to have to use @Cofiguration
if possible).
Solution
you don't need @Qualifier
in this case just replace them with @Profile("nameOfYourProfile")
:
@Service
@Profile("dev")
public class MyService1Impl implements MyService {
and
@Service
@Profile("!dev")
public class MyService2Impl implements MyService {
Answered By - J Asgarov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.