how to apply user defined properties value to @Req

2019-03-03 02:05发布

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 there's way using application.properties but I have to keep "/XXX" value in a user defined properties like a "local.properties". Is it possible to define @RequestMapping value on a user defined properties?

@Controller
@RequestMapping("/XXX")
public class MyController {
...
}

** UPDATE : tried several hours and get it to work.

my.properties

api.version=V100

mvc-context.xml

<context:property-placeholder ignore-unresolvable="true" location="/WEB-INF/config/property/my.properties"/>

controller

@RequestMapping("/${api.version}")

tomcat log

localhost-startStop-1> [2016-04-28 15:01:35.410] [INFO] [RequestMappingHandlerMapping] [534] Mapped "{[/V100/detail],methods=[GET]}"...

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-03 02:52

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.

查看更多
登录 后发表回答