1,除了需要配置pom.xml需要引用的jar包以外,还需要配置其他的配置文件;如web.xml,spring-mvc.xml,applicationContext.xml等。

    现在一一详细解说

    web.xml



    <!-- spring初始化时,容器调用监听 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
     <!--  spring初始化时,引用文件工程目录下所有以applicationContext_开头的.xml,包括jar包中也会搜索进来。
        	classpath:applicationContext.xml 仅指工程classes目录下的applicationContext.xml;
        	classpath:applicationContext_*.xml 仅指工程classes目录下的applicationContext_开头的.xml文件;
        	classpath*:applicationContext_.xml 指工程classes目录下包括lib包中,包含有application_开头的.xml文件。	
        	 文件以及全局参数,此处默认了一个参数product来控制是否使用生产联接串或开发联接串-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:applicationContext.xml
            </param-value>
        </context-param>
        <context-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>product</param-value>
        </context-param>
      <!-- 不多说了,编码过滤器 -->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
     <!-- spring mvc的初始化,文件为classes目录下的 spring-webmvc.xml web请求处理参数,以及 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-webmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
      <!--MySQL数据库JNDI数据  可在程序手动lookup 到数据源-->
        <resource-ref>
           <description>xuahua</description>
           <res-ref-name>jdbc/xuahua</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
       </resource-ref>
     <!-- druid web监控使用,如果不需要监控,可不用 -->
       <servlet>  
            <servlet-name>DruidStatView</servlet-name>  
            <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>  
        </servlet>    
        <servlet-mapping>  
            <servlet-name>DruidStatView</servlet-name>  
            <url-pattern>/druid/*</url-pattern>  
        </servlet-mapping> 
            
    以上为一般工程所需要的基本文件.


    其中文件提到了  applicationContext.xml  spring-webmvc.xml

    applicationContext.xml 内容如下

    <!-- 加载属性文件,如果没有,可不使用 -->
    	<bean id="propertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location">
    			<value>classpath:parameter.properties</value>
    		</property>
    	</bean>


    <!-- Hibernate 联接配置 -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		
    		<!-- 指定数据源 -->
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		
    		<!-- 指定实体映射文件 -->
    		<property name="packagesToScan">
    			<list>
    				<value>com.xuahua.model</value>
    			</list>
    		</property>
    		
    		<!-- 属性文件 -->
    		<property name="hibernateProperties">
    			<value>
    				hibernate.dialect=org.hibernate.dialect.MySQLDialect
    				hibernate.show_sql=true
    				hibernate.format_sql=true
    				hibernate.cache.use_second_level_cache=false
    				hibernate.use_sql_comments=false
    				hibernate.jdbc.batch_size=30
    				hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
    			</value>
    		</property>
    	</bean>
     <!-- hibernate事务管理 -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"></property>
    	</bean>
    <!-- 事务切入点 -->
    	<aop:config>
    		<aop:pointcut id="crudMethodsCoreMaster"
    			expression="execution(* com.xuahua.service..*.*(..))" />
    		<aop:advisor advice-ref="txAdviceCoreMaster" pointcut-ref="crudMethodsCoreMaster" />
    	</aop:config>
    	
    <!-- hibernate 代理事务,定义哪些service 开头的应该需要事务。哪些只需要只读事务 -->
    	<tx:advice id="txAdviceCoreMaster" transaction-manager="transactionManager">
    		<tx:attributes>
    			<tx:method name="mng*" propagation="REQUIRED" rollback-for="Exception" />
    			<tx:method name="create*" propagation="REQUIRED"
    				rollback-for="Exception" />
    			<tx:method name="save*" propagation="REQUIRED"
    				rollback-for="Exception" />
    			<tx:method name="update*" propagation="REQUIRED"
    				rollback-for="Exception" />
    			<tx:method name="insert*" propagation="REQUIRED"
    				rollback-for="Exception" />
    			<tx:method name="delete*" propagation="REQUIRED"
    				rollback-for="Exception" />
    			<tx:method name="do*" propagation="REQUIRED" rollback-for="Exception" />
    			<tx:method name="load*" read-only="true" />
    			<tx:method name="get*" read-only="true" />
    			<tx:method name="list*" read-only="true" />
    			<tx:method name="find*" read-only="true" />
    			<tx:method name="view*" read-only="true" />
    			<tx:method name="show*" read-only="true" />
    			<tx:method name="valid*" read-only="true" />
    			<tx:method name="query*" read-only="true" />
    		</tx:attributes>
    	</tx:advice>
    
    
    		<!-- 生产数据源(JNDI)(生产环境、UAT环境、测试环境用) -->
    	<beans profile="product">
    		<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    			<property name="jndiName">
    				<value>${jndiName}</value>
    			</property>
    		</bean>
    	</beans>
    
    <!-- druid web监控使用。 -->
    	<beans>
    		<bean id="druid-stat-interceptor"
    			class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    		</bean>
    	
    	
    		<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
    			scope="prototype">
    			<property name="patterns">
    				<list>
    					<value>com.xuahua.service.*</value>
    				</list>
    			</property>
    		</bean>
    	
    		<aop:config>
    			<aop:advisor advice-ref="druid-stat-interceptor"
    				pointcut-ref="druid-stat-pointcut" />
    		</aop:config>
     	</beans>


    spring-mvc.xml文件内容如下

    <!-- 定义自动代理 -->
    	<aop:aspectj-autoproxy proxy-target-class="true" />
    	
    	<!-- spring 定义扫描的包,对应工程路径.com.xuahua.controller;dao;service,意味着这三个包下面的所有类,spring会把注解的类名,方法名都会扫描进来 -->
    	<context:component-scan base-package="com.xuahua">
    		<context:include-filter type="regex"
    			expression=".controller..*" />
    		<context:include-filter type="regex"
    			expression=".dao..*" />
    		<context:include-filter type="regex"
    			expression=".service..*" />
    	</context:component-scan>
        
    	<!-- 启动注解的方式 -->
    	<context:annotation-config />  
    	
     <!-- spring mvc 请求映射处理默认-->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
        
        <!-- spring mvc Adapter 请求适配 转换json使用-->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <!-- 处理JSON数据转换的-->
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="supportedMediaTypes">
                            <list>
                                <value>text/html;charset=UTF-8</value>
                            </list>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
    
    	<!-- 使用默认的Servlet来响应静态文件-->
    	<mvc:default-servlet-handler />
    
    <!-- 视图解释类,定义文件路径,以及文件后缀。 -->
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/views/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    	<!-- 上传文件支持 -->
    	<bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    完整文件下载地址  web.xml http://file.xuahua.com/images/blog/2016/0728/web.xml

                              applicationContext.xml  http://file.xuahua.com/images/blog/2016/0728/applicationContext.xml

                             spring-webmvc.xml   http://file.xuahua.com/images/blog/2016/0728/spring-webmvc.xml


    联系QQ:343886028 --加QQ群 145315854