how to apply user defined properties value to @RequestMapping

后端 未结 1 1686
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 21:07

I have several @RequestMapping which value will subject to change from \"/XXX\" to \"/V100\" on someday. So I need to define it in properties. I\'ve googled and the

1条回答
  •  粉色の甜心
    2021-01-22 21:22

    In addition to the xml solution provided by @JustinB, here is an annotation-only solution (tested with Spring Boot):

    @Controller
    @PropertySource(value = "classpath:/user.properties", ignoreResourceNotFound = true)
    @RequestMapping("/${api.version:}")
    public class MyController {
    
    ...
    
    }
    

    The value of api.version is read from If src/main/resources/user.properties if it exists. If the file is missing or api.version is not set, it will default to an empty string.

    Beware, if api.version is also defined in application.properties it will take precedence whether or not user.properties exists and api.version is set in it.

    More examples of @PropertySource are provided here.

    0 讨论(0)
提交回复
热议问题