You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Controller的RequestMapping中启用ConditionalOnProperty

How to Enable Conditional Request Mappings with @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=custom exists in your securityConfig.properties, Spring registers the entire IndexController, 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 matchIfMissing to true if 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 prefix parameter to group related properties:
    @ConditionalOnProperty(prefix = "auth", name = "mode", havingValue = "custom")
    
    This maps to auth.mode=custom in your properties file, following Spring's best practices.
  • For complex conditions (like combining multiple properties), you can create a custom Condition class and use @Conditional(YourCustomCondition.class) instead.

内容的提问来源于stack exchange,提问作者Prateek Narendra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:52:22