如何在Controller的RequestMapping中启用ConditionalOnProperty
@ConditionalOnProperty in Spring Controller Hey there! Let's figure out how to add conditional activation to your request mappings in IndexController using @ConditionalOnProperty—since you can't apply this annotation directly to @RequestMapping, we have two solid approaches to achieve what you need.
Approach 1: Conditionalize the Entire Controller
If you want all request mappings in your controller to be enabled only when a specific property is set, apply @ConditionalOnProperty at the class level. This will make Spring load the entire controller only if the property condition is met.
Here's the updated code:
@PropertySource(value = "classpath:securityConfig.properties", ignoreResourceNotFound = true) @Controller // Load this controller ONLY when auth.mode equals "custom" @ConditionalOnProperty( name = "auth.mode", havingValue = "custom", matchIfMissing = false // Don't load if the property is missing ) public class IndexController { private static final String LOGIN_PAGE = "login"; private static final String HOME_PAGE = "home"; private static final String LOBBY_PAGE = "lobby"; private static final String FORGOT_USER_PAGE = "forgotUserName"; private static final String FORGOT_PASSWORD_PAGE = "forgotPassWord"; @GetMapping("/login") public String loginPage() { return LOGIN_PAGE; } @GetMapping("/home") public String homePage() { return HOME_PAGE; } // Add your other request mapping methods here... }
How it works:
- When
auth.mode=customexists in yoursecurityConfig.properties, Spring registers the entireIndexController, and all its request mappings are accessible. - If the property is missing or set to any other value, the controller won't be loaded, and none of its endpoints will work.
- Adjust
matchIfMissingtotrueif you want the controller to load by default when the property isn't present.
Approach 2: Conditionalize Individual Request Mappings
If you only want specific endpoints to be conditionally enabled, apply @ConditionalOnProperty directly to the method level. This gives you granular control over each request mapping.
Example implementation:
@PropertySource(value = "classpath:securityConfig.properties", ignoreResourceNotFound = true) @Controller public class IndexController { private static final String LOGIN_PAGE = "login"; private static final String HOME_PAGE = "home"; private static final String LOBBY_PAGE = "lobby"; private static final String FORGOT_USER_PAGE = "forgotUserName"; private static final String FORGOT_PASSWORD_PAGE = "forgotPassWord"; // Only enable /login when auth.mode is "custom" @GetMapping("/login") @ConditionalOnProperty(name = "auth.mode", havingValue = "custom") public String loginPage() { return LOGIN_PAGE; } // This endpoint is always enabled (no condition) @GetMapping("/home") public String homePage() { return HOME_PAGE; } // Enable /lobby if auth.mode is "enabled" OR the property is missing @GetMapping("/lobby") @ConditionalOnProperty( name = "auth.mode", havingValue = "enabled", matchIfMissing = true ) public String lobbyPage() { return LOBBY_PAGE; } // Add other conditional or unconditional mappings here... }
Pro Tips:
- For cleaner configuration, use the
prefixparameter to group related properties:
This maps to@ConditionalOnProperty(prefix = "auth", name = "mode", havingValue = "custom")auth.mode=customin your properties file, following Spring's best practices. - For complex conditions (like combining multiple properties), you can create a custom
Conditionclass and use@Conditional(YourCustomCondition.class)instead.
内容的提问来源于stack exchange,提问作者Prateek Narendra

