SSM配置文件简述

web.xml:

1.配置Spring环境

2.配置post的编码过滤器

3.配置前端控制器

4.配置shiro框架的ShiroFilter

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  
  <welcome-file-list>
    <welcome-file>/application/indexHelp.jsp</welcome-file>
  </welcome-file-list>

  <!-- 配置Spring环境 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置post的编码过滤器-->
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--配置前端控制器-->
  <servlet>
    <servlet-name>oa</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>oa</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--配一个ShiroFilter-->
  <!-- Shiro Filter is defined in the spring application context: -->
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <!-- 默认是值是false,true代表由spring来管理bean的生命周期-->
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

applicationContext-dao.xml:

1.引入连接池的配置文件

2.配置连接池

3.管理sessionFactory

3.1配置在哪加载mybatis主配置文件

3.2加载数据源

3.3加载映射文件

3.4别名

4.创建Mapper对象

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

    <context:property-placeholder location="classpath:properties/jdbc.properties" />
    <!-- 配置druid -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="url" value="${url}" />
        <property name="username" value="${myusername}" />
        <property name="password" value="${password}" />
        <property name="filters" value="stat" />
        <property name="maxActive" value="20" />
        <property name="initialSize" value="10" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="5" />
        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="100" />
    </bean>

    <!-- 2.管理sessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--在哪加载mybatis主配置文件-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <!--加载数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载映射文件-->
        <property name="mapperLocations">
            <array>
                <value>classpath:com/shopfs/mapper/*.xml</value>
            </array>
        </property>
        <!--别名-->
        <property name="typeAliasesPackage" value="com.shopfs.entity"/>
    </bean>

    <!-- 3.创建Mapper对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.shopfs.mapper"/>
    </bean>

</beans>

applicationContext-service.xml:

1.开启扫描 扫描service层

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


    <!-- 开启扫描 扫描service层-->
    <context:component-scan base-package="com.shopfs.service" />
</beans>

applicationContext-tx.xml:

1.配置事务管理器

2.配置事务增强策略

3.配置哪些切点需要应用该事务增强策略

<?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:tx="http://www.springframework.org/schema/tx"
       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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 事务的配置 -->
    <!-- 1.配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源对象-->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 2.配置事务增强策略 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--哪些方法需要进行事务管理-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 3.配置哪些切点需要应用该事务增强策略 -->
    <!--切点-->
    <aop:config>
        <aop:pointcut id="service"
                      expression="execution(* com.shopfs.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
    </aop:config>
</beans>

applicationContext-shiro.xml:

1.配置自定义realm

2.securityManager

2.1配置自定义的realm(后面有需要也可以配置缓存管理器、记住我管理器等等)

3.配置shiroFilter,配置放行的静态资源,拦截的页面

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied.  See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- 缓存管理器 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <!-- cache配置文件 -->
        <property name="cacheManagerConfigFile" value="classpath:spring/ehcache.xml" />
    </bean>

    <!--记住我cookie-->
    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg value="rememberMe"/>
        <property name="httpOnly" value="true"/>
        <property name="maxAge" value="2592000"/><!-- 30天 -->
    </bean>

    <!-- rememberMe管理器 -->
    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
        <property name="cookie" ref="rememberMeCookie"/>
    </bean>


    <!--配置自定义realm-->
    <bean id="myRealm" class="com.shopfs.realm.MyRealm">
        <!--会把前端用户输入的密码自动进行MD5加密-->
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <!-- 加密的算法 -->
                <property name="hashAlgorithmName" value="MD5"></property>
                <!--加密次数 -->
                <property name="hashIterations" value="1024"></property>
            </bean>
        </property>

    </bean>


    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--配置自定义的realm-->
        <property name="realm" ref="myRealm"/>
        <!-- 加入rememberMe管理 -->
        <property name="rememberMeManager" ref="rememberMeManager" />
        <!--配置缓存的管理器-->
        <property name="cacheManager" ref="cacheManager"/>
    </bean>




    <!-- =========================================================
         Shiro Spring-specific integration
         ========================================================= -->
    <!-- Post processor that automatically invokes init() and destroy() methods
         for Spring-configured Shiro objects so you don't have to
         1) specify an init-method and destroy-method attributes for every bean
            definition and
         2) even know which Shiro objects require these methods to be
            called. -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- Enable Shiro Annotations for Spring-configured beans.  Only run after
         the lifecycleBeanProcessor has run: -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations can be associated
         with a Subject for security checks. -->
    <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <!-- Define the Shiro Filter here (as a FactoryBean) instead of directly in web.xml -
         web.xml uses the DelegatingFilterProxy to access this bean.  This allows us
         to wire things with more control as well utilize nice Spring things such as
         PropertiesPlaceholderConfigurer and abstract beans or anything else we might need: -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="loginUrl" value="/goods/getOnSaleGoods"></property>
        <property name="unauthorizedUrl" value="/WEB-INF/jsp/NoAuthority.jsp" />
        <property name="securityManager" ref="securityManager"/>

        <!--
        /favicon.ico = anon
        左边:表示要访问的资源地址
        右边:拦截器
        anon:不需要进行登录认证就可以访问的资源
        authc:必须进行登录认证才可以访问的资源
        -->
        <property name="filterChainDefinitions">
            <value>
                /AmazeUI-2.4.2/**=anon
                /css/**=anon
                /images/**=anon
                /include/**=anon
                /js/**=anon
                /lib/**=anon
                /media/**=anon
                /scripts/**=anon
                /skin/**=anon
                /uploadfiles/**=anon

                /user/checkLogin=anon
                /user/check=anon
                /goods/**=anon
                /application/register.jsp=anon
                /user/register=anon
                /user/logout=logout

                /application/back/**=user
                <!--roles['admin']-->
            </value>
        </property>
    </bean>

</beans>

spring-mvc.xml:

1.加载注解驱动

2.扫描controller

3.配置视图解析器

4.配置静态资源放行

5.启动shiro注解

6.对上传文件的配置

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 1.扫描controller -->
    <context:component-scan base-package="com.shopfs.controller,com.shopfs.exception" />

    <!-- 2.配置视图解析器 -->
    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <!-- 将视图名 渲染后视图的前缀 -->
        <property name="prefix" value="/application/" />
        <!-- 渲染后视图的后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 配置静态资源 -->
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    <mvc:resources location="/lib/" mapping="/lib/**"></mvc:resources>
    <mvc:resources location="/skin/" mapping="/skin/**"></mvc:resources>
    <mvc:resources location="/media/" mapping="/media/**"></mvc:resources>
    <mvc:resources location="/include/" mapping="/include/**"></mvc:resources>
    <mvc:resources location="/scripts/" mapping="/scripts/**"></mvc:resources>
    <mvc:resources location="/uploadfiles/" mapping="/uploadfiles/**"></mvc:resources>
    <mvc:resources location="/AmazeUI-2.4.2/" mapping="/AmazeUI-2.4.2/**"></mvc:resources>

    <!-- 启动shiro注解 -->
    <aop:config proxy-target-class="true"></aop:config>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager" />
    </bean>

    <!-- 对上传文件的配置 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置编码格式 -->
        <property name="defaultEncoding" value="utf-8"></property>
        <!-- 设置上传文件总大小,单位字节 -->
        <property name="maxUploadSize" value="20971520"></property>
    </bean>
</beans>

mybatis-config.xml:

1.配置添加分页插件(也可以加在sessionFactory)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--需要配置的东西都在sqlSessionFactory中配置,只需要添加分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值