Spring MVC Web应用程序-从属性启用/禁用控制器 [英] Spring MVC web application - enabling / disabling controller from property

查看:158
本文介绍了Spring MVC Web应用程序-从属性启用/禁用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Tomcat中运行并使用Spring MVC定义控制器和映射的Web应用程序.我有以下课程:

I have a web application running in Tomcat and using Spring MVC to define controllers and mappings. I have the following class:

@Controller("api.test")
public class TestController {

        @RequestMapping(value = "/test", method = RequestMethod.GET)   
        public @ResponseBody String test(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
            // body
        }
}

我想根据某处定义的属性(例如文件)使此控制器和".../test"路径可用.如果该属性为false,那么我希望该应用程序表现为该路径不存在,并且如果为true,则表现正常.我怎样才能做到这一点?谢谢.

I would like to make this controller and the ".../test" path available according to a property defined somewhere (e.g. file). If the property is, lets say, false, I would like the app to behave as if that path doesn't exist and if it is true, to behave normally. How can I do this? Thanks.

推荐答案

如果您使用的是Spring 3.1+,则使控制器仅在测试配置文件中可用:

If you are using Spring 3.1+, make the controller available only in the test profile:

@Profile("test")
class TestController {
    ...
}

然后通过例如启用该配置文件在Tomcat启动时传递以下系统属性:

then enable that profile by e.g. passing the following system property at Tomcat boot:

-Dspring.profiles.active=test

要禁用控制器,只需忽略给定的配置文件.

To disable the controller simply omit the given profile.

这篇关于Spring MVC Web应用程序-从属性启用/禁用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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