覆盖SpringMVC控制器上的RequestMapping

ltf*_*hie 6 spring-mvc

通过我们的应用程序的源代码,我找到了一个通用的Spring MVC控制器,它可以多次显示复制和粘贴的配置键和值.除了RequestMapping值之外,类定义完全相同,因为每个应用程序都希望在不同的URL下使用此页面.

我想将此控制器移动到公共库中,并提供默认的RequestMapping值.

@Controller 
@RequestMapping (value="/property")
public class CommonPropertyController {
   ....
}
Run Code Online (Sandbox Code Playgroud)

如果每个应用程序想要使用自己的url模式,它们将如何覆盖此值?

Pav*_*ral 11

查看源代码,我知道如何在不必返回手动(预注释)处理程序定义的情况下完成它(这也是一种如何实现所需的方法).

Spring允许您在@RequestMapping值中使用属性占位符配置器.所以有可能使用这个事实并定义@RequestMapping如下:

@Controller
@RequestMapping("${routing.property.path}")
public class CommonPropertyController {
    ....
}
Run Code Online (Sandbox Code Playgroud)

然后,您只需PropertySourcesPlaceholderConfigurer在应用程序上下文中使用正确的属性进行定义,就可以了.


更新您还可以使用属性占位符指定默认值,如果您希望在不对属性进行修改的情况下进行回退映射:

@RequestMapping("${routing.property.path:/property}")
Run Code Online (Sandbox Code Playgroud)