如何创建servlet-context.xml&;spring的servlet-context-dispatcher.xml文件

如何创建servlet-context.xml&;spring的servlet-context-dispatcher.xml文件,spring,servletcontextlistener,Spring,Servletcontextlistener,在我的spring控制器中,我编写了一个@PostConstruct配置函数,但问题是在我运行项目时,它被调用了两次。下面是我的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.

在我的spring控制器中,我编写了一个@PostConstruct配置函数,但问题是在我运行项目时,它被调用了两次。下面是我的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>

节省金钱
我的项目
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/servlet-context.xml
1.
我的项目
/
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/servlet-context.xml,
/WEB-INF/spring-security.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
要求
错误
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>

com.example.myproject
hibernate.dialogue=org.hibernate.dialogue.mysqldialogue
我发现这个问题是因为我已经将servlet-context.xml配置为ContextLoaderListener和DispatcherServlet。所以为了避免这个问题,我必须将这个xml文件拆分为这两个文件

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

节省金钱
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/servlet-context-dispatcher.xml
1.

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

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

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

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

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