9

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:

  • If you define spring.profiles.active in application.properties which is loaded by default from classpath thne it works

  • when you rename the file to test.properties and include it via @PropertySource("classpath:test.properties") it stops working. Will raise a bug against it.

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

Thanks!

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118

2 Answers2

5

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

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
1

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

gyoder
  • 4,530
  • 5
  • 29
  • 37
  • Where are you setting the `spring.profiles.active` ? – Jan Zyka Aug 21 '14 at 14:25
  • @JanZyka, I was setting it in application.properties, hadn't tried it with a different name. Interesting – gyoder Aug 21 '14 at 15:17
  • Please if you have a spare minute could you just rename `application.properties` and use `@PropertySource("newName.properties")` on you top level `@Configuration` file? – Jan Zyka Aug 21 '14 at 15:42
  • @JanZyka, I did what you suggested and it broke just as you described. I even replaced the application.properties file and added a spring.config.name=myotherproperties. The other properties file sets the server port, so I knew it was being picked up. But as soon as I move spring.profiles and spring.profiles.active into the other properties file, it quits working. – gyoder Aug 21 '14 at 16:09