我的 Spring

1、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" version="3.1">
  <display-name>SSM_1</display-name>
  <!-- 前端控制器-为了(读取springmvc的配置文件)拦截请求对请求进行处理-然后进行分发(handle) -->
  <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:springmvc-config.xml</param-value>
       </init-param>       
       <!-- 表示容器在启动时立即加载Servlet -->
       <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 编码过滤器-处理各页面跟服务器的编码问题(非必须) -->
  <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的配置文件-加载spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springContext.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
</web-app>

2、 springContext

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"> 
        <!-- 读取db.properties文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        
        <!-- 配置数据源-driud -->
        <bean id="dataScource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.name}"></property>
            <property name="password" value="${jdbc.pwd}"></property>
            <property name="minIdle" value="${jdbc.minIdle}"></property>
            <property name="maxWait" value="${jdbc.maxWait}"></property>
            <property name="maxActive" value="${jdbc.maxActive}"></property>
            <property name="initialSize" value="${jdbc.initialSize}"></property>
        </bean>
        
        <!-- 开启基于事务的驱动器 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
        <!-- 配置事务管理器(非必须) -->
        <bean id="transactionManager" class=
        "org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataScource"></property>
        </bean>
        
        <!-- 注册SQLSessionFactory -->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="dataSource" ref="dataScource"></property>
            <!-- 因为基于mapper代理开发原本是要把mapper文件与class文件写在一个包 -->
            <!-- 但是为了方便管理-所以就需要分开写 -->
            <!-- 分开写时应该通过mapperLocation属性指定mapper文件的位置 -->
            <property name="mapperLocations" value="classpath:com/neko/mapper/*.xml"></property>
        </bean>
        
        <!-- 配置Mapper代理 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.neko.dao"></property>
        </bean>
        
        <!-- 开启注解驱动-并且进行包扫描 -->
        <context:component-scan base-package="com.neko.service,com.neko.util"></context:component-scan>
</beans>

3、springmvc-config

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 开启注解驱动-并且扫描controller包 -->
    <context:component-scan base-package="com.neko.controller,com.neko.po"></context:component-scan>
   
    <!-- 基于mvc的注解驱动器 -->
    <mvc:annotation-driven></mvc:annotation-driven>
     
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 自定义类型转换器 (非必要)-->

    <!-- 前端控制器需要放行(静态资源) -->
    <mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
    <mvc:resources location="/upload/" mapping="/upload/**"></mvc:resources>
    
    <!-- 如果有拦截器-要进行拦截器的配置 (非必须)-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/static/js/**"/>
            <bean class="com.neko.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
        <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/static/js/**"/>
            <bean class="com.neko.interceptor.PowerInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    <!-- 如果有文件上传-需要配置文件的解析器 (非必须)-->
        <!-- 文件上传配置  -->
    <bean id="multipartResolver" class=
    "org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置请求编码格式:UTF-8 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 设置允许上传的文件的最大值(20M),单位为字节 -->
        <property name="maxUploadSize" value="20000000"></property>
    </bean>
</beans>

猜你喜欢

转载自blog.csdn.net/qq_57210034/article/details/125415549