在SSM框架中往往要进行很多xml的配置,才能达到我们想要的效果。
(Spring、Spring MVC、Mybatis、Shiro、Freemarker)
*以下配置各有关联
一、在web.xml
配置前端控制器、字符编码过滤器、shiro代理过滤器
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--shiro-->
<!--代理过滤器,该代理过滤器会从spring容器中找shiroFilter-->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
二、在mvc.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入其他配置文件-->
<import resource="classpath:applicationContext.xml"/>
<!--引入shiro的配置文件-->
<import resource="classpath:myShiro.xml"/>
<context:component-scan base-package="cn.xbao.pet.web,cn.xbao.pet.util,cn.xbao.pet.security"/>
<!--配置MVC注解驱动器-->
<mvc:annotation-driven/>
<!-- 处理静态资源 -->
<mvc:default-servlet-handler/>
<!--配置freeMarker的模板路径,配置自定义的配置类,
将MyFreeMarkerConfig设置成当前环境中使用的配置对象,
让FreeMarker去解析shiro的标签以控制页面显示(在前端页面上,根据用户拥有的权限来显示具体的页面)
-->
<bean class="cn.xbao.pet.util.MyFreeMarkerConfig">
<!-- 配置freemarker的文件编码 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- 配置freemarker寻找模板的路径 -->
<property name="templateLoaderPath" value="/WEB-INF/views/" />
</bean>
<!--freemarker视图解析器 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<!-- 是否在model自动把session中的attribute导入进去; -->
<property name="exposeSessionAttributes" value="true" />
<!-- 配置逻辑视图自动添加的后缀名 -->
<property name="suffix" value=".ftl" />
<!-- 配置视图的输出HTML的contentType -->
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<!--统一异常处理器方式一-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->
<!--异常出错,默认跳转至错误视图-->
<property name="defaultErrorView" value="common/error"/>
<!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
<property name="exceptionAttribute" value="ex"/>
<!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->
<property name="exceptionMappings">
<value>
org.apache.shiro.authz.UnauthorizedException=common/nopermission
<!-- 这里还可以继续扩展不同异常类型的异常处理 -->
</value>
</property>
</bean>
</beans>
三、在applicationContext.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:tx="http://www.springframework.org/schema/tx"
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/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">
<!--组件扫描器-->
<context:component-scan base-package="cn.xbao.pet"/>
<!-- db.properties通过属性展位符交给Spring管理 -->
<!-- 使用属性占位符,读取属性文件 -->
<context:property-placeholder location="classpath:db.properties"
system-properties-mode="NEVER"/>
<!-- 配置这个表示环境变量不被别的覆盖 -->
<!-- 把德鲁伊连接池交给Spring管理 -->
<!-- 连接池要配初始化方法和销毁方法 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 设置数据库4要素 -->
<!--在德鲁伊连接池里面,第一个属性驱动类名可以不配,因为它可以根据url属性来自动识别是哪个数据库,
这里可以识别到器数据库是mysql,则其驱动类名就是com.mysql.jdbc.Drive-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置业务层组件:将业务层组件Service的实现类交给Spring管理 ,注:id默认的,可写可不写-->
<!-- 1:把SqlSessionFactory交给Spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--1:连接池 -->
<property name="dataSource" ref="dataSource"/>
<!--2:管理的mapper.xml文件 -->
<!--这里不直接用EmployeeMapper.xml而用*Mapper.xml是因为不想把它写死! -->
<!--<property name="mapperLocations" value="classpath:cn/xbao/pet/mapper/*Mapper.xml"/>-->
<!--注意:1和2都是必配的,,以下3和4时选配的(根据需求,甚至可以不配) -->
<!--3:包的别名扫描,比如要写类的简单名称,那么就要配一个别名,如果没配这个别名,就不可以写别名,它默认是全限定名 -->
<property name="typeAliasesPackage" value="cn.xbao.pet.domain"/>
<!--4:框架的个性配置:比如Mybatis个性配置:懒加载 -->
<!--引入mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- 2:配置mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描哪个包中的接口 -->
<property name="basePackage" value="cn.xbao.pet.mapper"/>
</bean>
<!--声明式事务管理-->
<!-- AOP:3W -->
<!-- what:做什么增强 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 开启事务: conn.setAutoCommit(false);要执行这个代码就要有Connection对象,而该对象是从连接池来的-->
<!-- 管理哪个连接池的事务 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<!-- where:在哪里做增强 -->
<aop:pointcut id="pc" expression="execution(* cn.xbao.pet.service.impl.*ServiceImpl.*(..))"/>
<!--三剑客连接:where和when连接在一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
<!--
when:在什么时机做增强(底层是自定义增强)
由tx:advice标签底层封装好了,是环绕增强
并且when和what连接在一起
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 对方法做个性事务配置 -->
<!--DQL事务管理-->
<tx:method name="get*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<!--DML事务管理-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置Mapper接口的代理对象 -->
<!-- <bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
配置SqlSessionFactory来源
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
代理哪个接口
<property name="mapperInterface" value="cn.xbao.mybatis.mapper.AccountMapper" />
</bean> -->
</beans>
四、mybatis.xml
<?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>
<!--个性配置-->
<settings>
<!--配置允许懒加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--配置积极懒加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--调用哪些方法触发懒加载-->
<setting name="lazyLoadTriggerMethods" value="clone"/>
</settings>
<!--分页插件,配置在Spring中-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--当pageNum <= 0 时,将pageNum设置为1-->
<!--当pageNum > pages时,将pageNum设置为pages-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
</configuration>
五、myShiro.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--shiro过滤器-->
<bean id="shiroFilter"
class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--引用指定的安全管理器-->
<property name="securityManager" ref="securityManager"/>
<!--登录表单的地址-->
<property name="loginUrl" value="/login.html"/>
<!--过滤规则-->
<property name="filterChainDefinitions">
<value>
<!--匿名访问,不需要登陆即可访问-->
/js/**=anon
/images/**=anon
/css/**=anon
/logout=logout
<!--/register=anon
/register.html=anon-->
<!--认证访问,需要认证(登陆)才能使用-->
/**=authc
</value>
</property>
<property name="filters">
<map>
<!--设置当前使用的认证过滤器-->
<entry key="authc" value-ref="authenticationFilter"/>
</map>
</property>
</bean>
<!--shiro安全管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="petRealm"/>
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!--基于shiro的权限加载:开启shiro注解扫描,并 注入安全管理器-->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!--指定当前需要使用的凭证匹配器-->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!--指定加密算法-->
<property name="hashAlgorithmName" value="MD5"/>
</bean>
<!-- 缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" >
<!-- 设置配置文件 -->
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>
</beans>
六、shiro-ehcache.xml
<ehcache name="cacheManager">
<!--
maxElementsInMemory:缓存最大个数
eternal:是否永久缓存
timeToIdleSeconds:最大空闲时间
timeToLiveSeconds:最大存活时间
memoryStoreEvictionPolicy:缓存淘汰策略,最近最少使用
-->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/117719.html