【问题标题】:How to create servlet-context.xml & servlet-context-dispatcher.xml files for spring如何为 spring 创建 servlet-context.xml 和 servlet-context-dispatcher.xml 文件
【发布时间】:2017-03-21 19:54:31
【问题描述】:

在我的spring spring控制器中我写了一个@PostConstruct配置函数,但问题是我运行项目时它被调用了2次。下面是我的web.xml和servlet-context.xml文件。

web.xml:

        <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>SaveMoneyOauth</display-name>

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


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

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

            <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>  
                /WEB-INF/servlet-context.xml,  
                /WEB-INF/spring-security.xml
            </param-value>
            </context-param>


            <!-- Spring Security -->

            <filter>
                    <filter-name>springSecurityFilterChain</filter-name>
                    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
            </filter>

            <filter-mapping>
                    <filter-name>springSecurityFilterChain</filter-name>
                    <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
                    <dispatcher>ERROR</dispatcher>
            </filter-mapping>
    </web-app>

servlet-context.xml:

        <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
            <context:annotation-config />
        <context:component-scan base-package="com.example.myproject" />
            <mvc:annotation-driven>
            <mvc:message-converters register-defaults="false">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            </mvc:message-converters>
            </mvc:annotation-driven>

            <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  

        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="1000000000" />
            </bean>  

            <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/SaveIt"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <!-- Hibernate Session Factory -->
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="packagesToScan">
        <array>
            <value>com.example.myproject</value>
        </array>
        </property>
        <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
        </value>
        </property>
    </bean>
    <!-- Hibernate Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <!-- Activates annotation based transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

我发现这个问题是因为我将 servlet-context.xml 配置为 ContextLoaderListener 和 DispatcherServlet。所以为了避免这个问题,我必须为这两个分开这个 xml 文件。所以我在 web.xml 中更改了 DispatcherServlet,如下所示

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

但是如何将文件代码拆分为 servlet-context.xml 和 servlet-context-dispatcher.xml?我应该在这两个文件中写什么?请帮助我。

【问题讨论】:

    标签: spring servletcontextlistener


    【解决方案1】:

    您通过 ContextLoaderListener 加载的内容应该是 root WebApplicationContext

    根 WebApplicationContext 应该包含所有的基础设施 应该在其他上下文和 Servlet 之间共享的 bean 实例。这些继承的 bean 可以在 特定于 servlet 的范围,并且您可以定义新的特定于范围的 bean 本地给定的 Servlet 实例。

    您通过 DispatcherServlet 加载的 Bean 是特定于应用程序的 servlet 上下文,它继承了您在根 Web 应用程序上下文中定义的所有 bean。

    因此,理想情况下,诸如会话工厂、事务管理器和其他相关基础设施 bean 之类的应用程序特定 bean 应该放在您的根 Web 应用程序上下文中。控制器和视图解析器等 Bean 应该放在您的 servlet 上下文中

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多