2016-11-08 91 views
1

在我的spring spring控制器中,我写了一个@PostConstruct配置函数,但问题在于它运行project.Below是我的web.xml和servlet-context.xml文件。如何为spring创建servlet-context.xml和servlet-context-dispatcher.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文件sperate two.So我在web.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> 

改变了DispatcherServlet的下面,但我怎么能分割文件代码到Servlet的context.xml中和servlet的上下文dispatcher.xml?我应该在这两个文件中写什么?请帮助我。

回答

1

你通过的ContextLoaderListener加载的东西,应该是根WebApplicationContext的

了根WebApplicationContext应该包含所有的基础设施 豆应您的其他背景和Servlet实例 之间共享。这些继承的bean可以在 特定于servlet的作用域中被覆盖,并且可以为给定的Servlet实例定义新的作用域特定的bean 。

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

因此理想情况下,应用程序特定的bean如会话工厂,事务管理器和其他相关基础结构bean应该放在根web应用程序上下文中。豆类如控制器和视图解析器应该在你的servlet环境中运行

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