Spring: profile-based @Component selection

I have an interface as shown below:

@Service public interface MyService { } 

And two implementing it:

 @Component @Profile("dev") public class DevImplementation implements MyService { } 

and

 @Component @Profile("prod") public class ProdImplementation implements MyService { } 

And another service is trying to use it:

 @Service public MyClass { @Autowire MyService myservice; } 

The problem is that I NoSuchBeanException when I run the code. It starts with

mvn spring-boot:run -P dev

What am I doing wrong?

+5
source share
1 answer

With -P you activate the Maven profile. However, Maven profiles are independent of Spring profiles. As long as you don't have a Maven profile configured to set the corresponding Spring property, you need to enable the Spring profile this way:

 mvn spring-boot:run -Dspring.profiles.active=dev 
+3
source

Source: https://habr.com/ru/post/1263436/


All Articles