在Spring框架中,applicationContext.xml和Spring -servlet.xml是相关的吗? 在applicationContext.xml中声明的属性文件对DispatcherServlet可用吗? 另外,为什么我需要一个*-servlet.xml呢?为什么单独的applicationContext.xml不够?


当前回答

Application contexts provide a means for resolving text messages, including support for i18n of those messages. Application contexts provide a generic way to load file resources, such as images. Application contexts can publish events to beans that are registered as listeners. Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. ResourceLoader support: Spring’s Resource interface us a flexible generic abstraction for handling low-level resources. An application context itself is a ResourceLoader, Hence provides an application with access to deployment-specific Resource instances. MessageSource support: The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable

其他回答

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

xml定义了“根webapp上下文”的bean,即与webapp相关的上下文。

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

spring-servlet.xml中的bean可以引用applicationContext.xml中的bean,反之则不行。

所有Spring MVC控制器都必须放在Spring -servlet.xml上下文中。

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

在spring-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>

在applicationcontext.xml中,我们为除控制器外的其余包添加了过滤器。

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

在Servlet技术中,如果你想将任何输入传递给特定的Servlet,那么你需要像下面的代码一样传入初始化参数。

 <servlet>
    <servlet-name>DBController</servlet-name>
    <servlet-class>com.test.controller.DBController</servlet-class>
    <init-param>
        <param-name>username</param-name>
        <param-value>John</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>DBController</servlet-name>
    <url-pattern>/DBController</url-pattern>
</servlet-mapping>

如果您想在put中传递一些对所有servlet都通用的参数,那么此时您需要配置上下文参数。例子

 <context-param>
    <param-name>email</param-name>
    <param-value>admin@example.com</param-value>
</context-param>

所以就像这样,当我们使用Spring MVC时,我们需要通过init参数向Spring提供的DispatcherServlet提供一些信息。 因此,配置是休耕的,这里我们将spring-servlet.xml作为初始参数提供给DispatcherServlet。

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Spring MVC App</display-name>

    <servlet>
        <servlet-name>SpringController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringController</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

同样,我们需要一些context参数。适用于整个应用。 我们可以提供根上下文applicationcontext。xml 配置如下:

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationcontext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
        <servlet-name>SpringController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringController</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

简单地说,

xml定义了在所有servlet之间共享的bean。如果您的应用程序有多个servlet,那么在applicationContext.xml中定义公共资源会更有意义。

xml定义了仅与该servlet相关的bean。这里是dispatcher servlet。所以,你的Spring MVC控制器必须在这个文件中定义。

如果在web应用程序中只运行一个servlet,那么在spring-servlet.xml中定义所有bean并没有什么错。

Application contexts provide a means for resolving text messages, including support for i18n of those messages. Application contexts provide a generic way to load file resources, such as images. Application contexts can publish events to beans that are registered as listeners. Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. ResourceLoader support: Spring’s Resource interface us a flexible generic abstraction for handling low-level resources. An application context itself is a ResourceLoader, Hence provides an application with access to deployment-specific Resource instances. MessageSource support: The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable