且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring Framework中applicationContext.xml和spring-servlet.xml之间的区别

更新时间:2023-12-04 10:56:28

Spring允许您在父子层次结构中定义多个上下文。

Spring lets you define multiple contexts in a parent-child hierarchy.

applicationContext.xml 定义root webapp context的bean,即与webapp关联的上下文。

The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.

spring-servlet.xml (或者你称之为的其他任何东西)定义了一个servlet应用程序上下文的bean。在webapp中可以有很多这样的,每个Spring servlet一个(例如 spring1-servlet.xml for servlet spring1 spring2-servlet.xml for servlet spring2 )。

The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).

中的bean spring-servlet.xml 可以引用 applicationContext.xml 中的bean,但反之亦然。

Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.

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

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

在大多数情况下, applicationContext.xml 上下文是不必要的。它通常用于包含在webapp中的所有servlet之间共享的bean。如果你只有一个servlet,那么除非你有特定用途,否则没什么意义。

In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.