Spring MVC的控制器的运行时加载和动态映射请求/ URL [英] Runtime loading of Controllers for Spring MVC and dynamically mapping requests/URLs

查看:1071
本文介绍了Spring MVC的控制器的运行时加载和动态映射请求/ URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Spring MVC开始一个新项目,我们希望摆脱注释驱动的请求/ url映射。我们希望实现以下用例:

We are starting a new project using Spring MVC, and we would like to move away from annotation-driven request/url mapping. We wish to implement the following use case:

用例A


  1. 用户输入URL。

  2. 请求映射处理程序检索映射列表(例如,从数据库中),并根据此动态映射列表调用相关的控制器。

这是因为我们希望能够做到以下几点:

This is because we want to be able to do the following as well:

用例B


  1. 我们要加载一个新的控制器(也许是一个新的报告模块)进入Web应用程序而无需重新部署或重新启动服务器。

  2. 我们将这个新的Controller映射到一个URL并将其保存在某个地方(很可能是数据库)。

  3. 我们希望Controller在Spring应用程序上下文中注册(由Spring管理)。

  4. 我们希望在请求中使用这个新的Controller映射。

  1. We want to load a new Controller (perhaps a new reports module) into the web app without having to redeploy or do a server restart.
  2. We will map this new Controller to a URL and persist it somewhere (most likely the DB).
  3. We would like the Controller to be registered in the Spring app context (managed by Spring).
  4. We would then like to use this new Controller in the request mapping.

我们初步了解了我们可以实施的不同方式这,但我们不确定这条路线的最佳架构/方法。几个问题:

We've taken an initial look at the different ways we can implement this, but we are unsure of the best architecture/method to go about this route. A couple of questions:


  1. 对于用例A,我们如何在Spring MVC框架内实现它(或者如果可能的话)?

  2. 对于用例B,是否有一个很好的框架或方法可以为Web应用程序动态加载和注册?我们粗略地看了一下OSGI,但似乎建议在非Web应用程序中使用。


推荐答案

对于用例A:
您可以将url映射保留在属性文件中,然后使用属性占位符在上下文中使用xml配置初始化bean,而不是DB。这种方式保留在spring框架内,你可以避免注释。

For Use case A : Instead of DB you can keep the url mappings in a property file and then use property place holder to initialize beans using xml configuration on context up. This way remaining inside the spring framework, you can avoid annotations.

对于用例B:
Tomcat支持动态重新加载类,但只支持非结构类类文件中的更改。但这有内存泄漏,并且它不会清除类加载器的旧实例,而是创建一个新实例。

For Use Case B : Tomcat supports dynamic reloading of classes but that to of only non structural changes in class file. But this has memory leaks as well as it doesnt cleans up old instance of class loader rather it creates a new instance.

这篇关于Spring MVC的控制器的运行时加载和动态映射请求/ URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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