Jan Zyka
Jan Zyka

Reputation: 17898

Using @Profile in spring boot

I have spring boot application (1.1.5.RELEASE) and enabling my profiles via the configuration protperty spring.profiles.active=MyProfile

The profile gets activated correctly which I can see by beans from that profile being created.

Then I have a @Controller used as follows:

@Controller
@RequestMapping("/someUrl")
@Profile("MyProfile")
public class MyController {
...
}

This controller is not instantiated and URL used in the controller are not mapped. In the same package I have another controllers which are not limited by @Profile and these get instsantiated and mapped as expected.

So is using @Profile annotation on controller something which is not compatible with spring boot? Is there other approach I should be using?

Edit: It seems to be a bug after all as if I include -Dspring.profiles.active=MyProfile as JVM property the controller gets instantiated :'(

Edit2: So here comes the interesting part:

Edit 3: As promised: https://github.com/spring-projects/spring-boot/issues/1417

Thanks!

Upvotes: 9

Views: 16670

Answers (2)

Andy Wilkinson
Andy Wilkinson

Reputation: 116091

I've tracked this down to what I believe to be a bug in Spring. See SPR-12111 for more details.

Upvotes: 5

gyoder
gyoder

Reputation: 4738

You can definitely annotate a controller with @Profile in Spring Boot, just as you are doing above. MyController gets instantiated if MyProfile is active. Are you sure that "MyProfile" is the active profile? Are you setting the spring.profiles property?

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/context/annotation/Profile.html The @Profile annotation may be used in any of the following ways:

as a type-level annotation on any class directly or indirectly annotated with @Component, including @Configuration classes as a meta-annotation, for the purpose of composing custom stereotype annotations

Upvotes: 1

Related Questions