14

I have a spring mvc controller and I want to enable it only in some profiles (for example development and test).

I know I can use the profile attribute of the beans element in xml configuration to limit the scope of my beans, but I'm using convenient annotations for the controllers now.

Can I bind the annotated controller to given profile somehow?

Or do I have to use the "old way" (of implementing and declaring controller) without annotations and use the beans element in xml configuration?

Will the annotated controllers mix well with the "old ones"?

EDIT: another way, which comes to my mind, is to check the profile in runtime from autowired Environment instance, but this denies the inversion of control

Roman Cherepanov
  • 1,639
  • 2
  • 24
  • 44
Kojotak
  • 2,020
  • 2
  • 16
  • 29

1 Answers1

21

Is this what you mean ?

@Controller
@Profile("test")
public class CacheController {
}

javadoc

Roman Cherepanov
  • 1,639
  • 2
  • 24
  • 44
MikePatel
  • 2,593
  • 2
  • 24
  • 38
  • 2
    Here's [javadoc](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/context/annotation/Profile.html). – Sotirios Delimanolis Jun 12 '13 at 14:05
  • 1
    I should RTFM more carefully next time, I thought that the Profile annotation is only useful in java based configuration and not with the beans. – Kojotak Jun 12 '13 at 14:21
  • Using the right import is also important, because `org.opengis.annotation.Profile` for example is also a type annotation. – Valerij Dobler May 10 '23 at 08:09