Since Spring 4.x is possible newly to control the profile directly with annotating by @Profile
on the method level.
... as a method-level annotation on any @Bean
method
Personally, I do not use profiles on the method level, read more at Is Spring's @Profile on methods a good practice?
In your case, I rather suggest you the following approach:
Create two classes and make the implement the common interface.
public interface Foo {
public void bar();
}
Annotate each with @Profile
based on the environment.
@Component
@Profile("local")
public class LocalFoo implements Foo {
@Override
public void bar() { ... }
}
@Component
@Profile("prod")
public class ProdFoo implements Foo {
@Override
public void bar() { ... }
}
Since now, they are ready to be injected according to the active profile.