xml文件Spring和Spring MVC框架有什么区别

时间:2021-01-09 13:37:17

I start to learn spring recently.
My goal is to use spring MVC to do restful api
I know spring MVC is web framework in spring
I know that in spring,there is beans.xml
And in spring MVC , there is servletname-servlet.xml
I want to know where is difference??
Is it means if I use spring MVC,I don't need to use beans.xml??

我最近开始学弹簧。我的目标是使用spring MVC做restful api我知道spring MVC是春天的web框架我知道春天有bean.xml而且在spring MVC中有servletname-servlet.xml我想知道哪里有区别??是否意味着如果我使用spring MVC,我不需要使用beans.xml?

Please give me some way or give me example project link with spring and spring MVC together

请给我一些方法或者给我一些项目链接与春季和春季MVC在一起

2 个解决方案

#1


The servletname-servlet.xml defines the beans for one servlet's app context. There can be number of servlets in a webapp and for every servlet we have servletname-servlet.xml (e.g. spring1-servlet.xml for servlet1, spring2-servlet.xml for servlet2).

servletname-servlet.xml为一个servlet的app上下文定义bean。 webapp中可以有多个servlet,对于每个servlet,我们都有servletname-servlet.xml(例如,servlet1的spring1-servlet.xml,servlet2的spring2-servlet.xml)。

Beans defined in servletname-servlet.xml can reference beans in beans.xml, but not vice versa.

servletname-servlet.xml中定义的bean可以引用beans.xml中的bean,但反之亦然。

All Spring MVC controllers must go in the servletname-servlet.xml context.

所有Spring MVC控制器都必须进入servletname-servlet.xml上下文。

Beans.xml contain beans that are shared between all servlets in a webapp.Usually the beans.xml context is not necessary if you have only one servlet in your webapp.

Beans.xml包含在webapp中的所有servlet之间共享的bean。如果webapp中只有一个servlet,则通常不需要beans.xml上下文。

#2


You could define all your beans in servletname-servlet.xml but it's not a good practice.

您可以在servletname-servlet.xml中定义所有bean,但这不是一个好习惯。

Usually if you create a web application in 'pure' spring (ie. without spring MVC) then you will add ContextLoaderListener as a filter to your web.xml. Then spring will look for applicationContext.xml when you will usually import beans.xml.

通常,如果您在'pure'spring中创建Web应用程序(即没有spring MVC),那么您将添加ContextLoaderListener作为web.xml的过滤器。然后,当您通常导入beans.xml时,spring将查找applicationContext.xml。

In servletname-servlet.xml you define servlets. Servlets can refer other beans. So it's good practice to separate front (servlets) from backend (beans.xml).

在servletname-servlet.xml中定义servlet。 Servlet可以引用其他bean。因此,将前端(servlet)与后端(beans.xml)分开是一种很好的做法。

Also remember that beans declared in servletname-servlet.xml are overriding the definitions of any beans defined with the same name in the global scope.

还要记住,在servletname-servlet.xml中声明的bean会覆盖在全局范围内使用相同名称定义的任何bean的定义。

See also better answer at: ContextLoaderListener or not?

另请参阅更好的答案:ContextLoaderListener与否?

#1


The servletname-servlet.xml defines the beans for one servlet's app context. There can be number of servlets in a webapp and for every servlet we have servletname-servlet.xml (e.g. spring1-servlet.xml for servlet1, spring2-servlet.xml for servlet2).

servletname-servlet.xml为一个servlet的app上下文定义bean。 webapp中可以有多个servlet,对于每个servlet,我们都有servletname-servlet.xml(例如,servlet1的spring1-servlet.xml,servlet2的spring2-servlet.xml)。

Beans defined in servletname-servlet.xml can reference beans in beans.xml, but not vice versa.

servletname-servlet.xml中定义的bean可以引用beans.xml中的bean,但反之亦然。

All Spring MVC controllers must go in the servletname-servlet.xml context.

所有Spring MVC控制器都必须进入servletname-servlet.xml上下文。

Beans.xml contain beans that are shared between all servlets in a webapp.Usually the beans.xml context is not necessary if you have only one servlet in your webapp.

Beans.xml包含在webapp中的所有servlet之间共享的bean。如果webapp中只有一个servlet,则通常不需要beans.xml上下文。

#2


You could define all your beans in servletname-servlet.xml but it's not a good practice.

您可以在servletname-servlet.xml中定义所有bean,但这不是一个好习惯。

Usually if you create a web application in 'pure' spring (ie. without spring MVC) then you will add ContextLoaderListener as a filter to your web.xml. Then spring will look for applicationContext.xml when you will usually import beans.xml.

通常,如果您在'pure'spring中创建Web应用程序(即没有spring MVC),那么您将添加ContextLoaderListener作为web.xml的过滤器。然后,当您通常导入beans.xml时,spring将查找applicationContext.xml。

In servletname-servlet.xml you define servlets. Servlets can refer other beans. So it's good practice to separate front (servlets) from backend (beans.xml).

在servletname-servlet.xml中定义servlet。 Servlet可以引用其他bean。因此,将前端(servlet)与后端(beans.xml)分开是一种很好的做法。

Also remember that beans declared in servletname-servlet.xml are overriding the definitions of any beans defined with the same name in the global scope.

还要记住,在servletname-servlet.xml中声明的bean会覆盖在全局范围内使用相同名称定义的任何bean的定义。

See also better answer at: ContextLoaderListener or not?

另请参阅更好的答案:ContextLoaderListener与否?