简体   繁体   English

Spring容器在Spring MVC Web应用程序中的作用

[英]Role of spring container in spring mvc web application

We are developing a web application which uses spring mvc, rest, jquery, ajax and json. 我们正在开发一个使用spring mvc,rest,jquery,ajax和json的Web应用程序。 Also we use mongodb as our db. 另外,我们使用mongodb作为数据库。 We use maven as build tool. 我们使用Maven作为构建工具。
I have a project structure like below: 我有一个如下的项目结构:

  • eem (parent pom) eem (父pom)
    • eem-db (sub module, and it is a Eclipse project for mongo db dao level and model resides here) eem-db (子模块,它是用于mongo db dao级别的Eclipse项目,模型位于此处)
    • eem-net (sub module, and it is again a Eclipse project for some networking code) eem-net (子模块,它还是一些网络代码的Eclipse项目)
    • eem-webapp (sub module, and it is a Eclipse web app project for web application, which has Spring MVC) eem-webapp (子模块,它是用于Web应用程序的Eclipse Web应用程序项目,具有Spring MVC)

My doubt is about the design of our web application in which I'm not knowing how to use spring container on this web app. 我的疑问是关于我们的Web应用程序的设计,我不知道如何在此Web应用程序上使用spring容器。

Below are my requirements and need help: 以下是我的要求并需要帮助:

  1. Is it a correct way to have mongodb related dao in a separate project? 将mongodb相关的dao放在一个单独的项目中是正确的方法吗?
    (we use dependency on eem-webapp to get db code (as .jar ) on our web app). (我们使用对eem-webapp依赖关系来在我们的Web应用程序上获取数据库代码(如.jar ))。

  2. How can i use @Autowired on the model in my controller to get mongo db model "emp" (say, com.eem.db.model.emp )? 如何在控制器中的模型上使用@Autowired以获得mongo db模型“ emp”(例如com.eem.db.model.emp )? ie What configuration do i need to provide on my eem-webapp to autowire emp (and all model classes) model to my controller? 即我需要在eem-webapp上提供什么配置以将emp (和所有模型类)模型自动emp到控制器?

  3. I want to instantiate a class (say, com.eem.net.discovery.discover ) from eem-net on my controller. 我想从控制器上的com.eem.net.discovery.discover eem-net实例化一个类(例如com.eem.net.discovery.discover )。 How should i do this from spring config file. 我应该如何从spring配置文件中做到这一点。 I know using new is not necessary when we are on spring container. 我知道当我们在弹簧容器上时,不需要使用new。 What kinda DI i should use for this situation? 在这种情况下,我应该使用哪种DI?

Any pointers for my learning? 对我的学习有什么建议吗?

The best practique is create one session factory bean (scope=singleton) that you can @Autowired in all your Controllers, you can also create one pool connections to managing your clients db requests. 最好的做法是创建一个会话工厂bean(scope = singleton),您可以在所有Controller中@Autowired,也可以创建一个池连接来管理客户端数据库请求。

For this you can create your own .jar, and use your own .jar classes to register your bean in your spring container. 为此,您可以创建自己的.jar,并使用自己的.jar类在spring容器中注册bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM