问题

  • 无论如何,在Spring Framework中applicationContext.xml和spring-servlet.xml是否相关?
  • 在applicationContext.xml中声明的属性文件是否可用于DispatcherServlet?
  • 在相关的说明中,为什么我需要一个* -servlet.xml?为什么单独的applicationContext.xml不足?

#1 热门回答(389 赞)

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

TheapplicationContext.xml定义了"根webapp上下文"的bean,即与webapp相关联的上下文。

Thespring-servlet.xml(或其他任何你称之为)的bean定义了一个servlet应用程序上下文的bean。在webapp中可以有许多这样的,每个Spring servlet一个(例如,对于servletspring2,servletspring1,spring2-servlet.xml为22514135)。

spring-servlet.xml中的豆类可以参考3885941018中的豆类,但反之亦然。

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

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


#2 热门回答(94 赞)

#场景1

在客户端应用程序中(应用程序不是Web应用程序,例如可能是swing app)

private static ApplicationContext context = new  ClassPathXmlApplicationContext("test-client.xml");

context.getBean(name);

不需要web.xml。 ApplicationContext作为获取bean服务的容器。不需要Web服务器容器。 Intest-client.xml可以是Simple bean,没有远程处理,bean有远程处理。

结论:在场景1中,applicationContext和DispatcherServlet没有关系。

#场景2

在服务器应用程序(部署在服务器中的应用程序,例如Tomcat)。从客户端程序远程访问服务(例如Swing应用程序)

定义侦听器inweb.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在服务器启动时,ContextLoaderListener实例化了inapplicationContext.xml中定义的bean。

假设你已经定义了以下inapplicationContext.xml:

<import resource="test1.xml" />
<import resource="test2.xml" />
<import resource="test3.xml" />
<import resource="test4.xml" />

bean从所有四个配置文件test1.xml,test2.xml,test3.xml,test4.xml中实例化。

结论:在场景2中,applicationContext和DispatcherServlet无关。

#场景3

在带有spring MVC的Web应用程序中。

Inweb.xmldefine:

<servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
</servlet>

<servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

当Tomcat启动时,bean定义了inspringweb-servlet.xmlare,实例化为DispatcherServletextendsFrameworkServlet。 InFrameworkServlet Bean实例化发生在springweb上。在我们的casepringwebis FrameworkServlet中。

结论:在场景3中,applicationContext和DispatcherServlet没有关系。

#场景4

在带有spring MVC.springweb-servlet.xml for servlet和applicationContext.xml的Web应用程序中,用于访问服务器程序中的业务服务或访问另一个服务器程序中的DB服务。

Inweb.xml中定义了以下内容:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

在服务器启动时,ContextLoaderListener实例化在inapplicationContext.xml中定义的bean;假设你在此声明:

<import resource="test1.xml" />
<import resource="test2.xml" />
<import resource="test3.xml" />
<import resource="test4.xml" />

bean都是从所有fourtest1.xml,test2.xml,test3.xml,test4.xml中实例化的。完成bean实例化后定义inapplicationContext.xml,然后bean定义了inspringweb-servlet.xmlare实例化。

所以实例化顺序是root是应用程序上下文,然后是FrameworkServlet。

现在它清楚地说明了为什么它们在哪种情况下很重要。


#3 热门回答(52 赞)

还有一点我想补充一点。 Inspring-servlet.xml我们包括Controller包的组件扫描。在下面的示例中,我们包含控制器包的过滤器注释。

<!-- Scans for annotated @Controllers in the classpath -->
<context:component-scan base-package="org.test.web" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

Inapplicationcontext.xml我们为除控制器之外的剩余包添加过滤器。

<context:component-scan base-package="org.test">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

原文链接