如何将用户定义的属性值应用于@RequestMapping [英] how to apply user defined properties value to @RequestMapping

查看:86
本文介绍了如何将用户定义的属性值应用于@RequestMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个@RequestMapping,其值有一天会从"/XXX"更改为"/V100".所以我需要在属性中定义它.我已经搜索过,并且有使用application.properties的方法,但是我必须在用户定义的属性(例如"local.properties")中保留"/XXX"值.是否可以在用户定义的属性上定义@RequestMapping值?

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

my.properties

api.version=V100

mvc-context.xml

mvc-context.xml

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

控制器

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

tomcat日志

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

推荐答案

除了@JustinB提供的xml解决方案之外,还有一个仅注释的解决方案(已通过Spring Boot测试):

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 {

...

}

api.version的值是从 src/main/resources/user.properties (如果存在)中读取的.如果文件丢失或未设置api.version,它将默认为空字符串.

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.

请注意,如果在 application.properties 中也定义了api.version,则是否存在 user.properties 并在其中设置了api.version都将具有优先权.

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.

此处中提供了@PropertySource的更多示例.

More examples of @PropertySource are provided here.

这篇关于如何将用户定义的属性值应用于@RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆