springmvc + shiro 配置(一、结构及配置文件)
2015-10-16 18:05
597 查看
springmvc + shiro 配置
最近自己做的项目框架升级之后,一直没有时间来整理一个完整的配置笔记,大都是一些小问题,这两天项目得空,刚好整理一下,顺便也修正一下配置文件中的冗余配置和不规范。整个框架涉及的部分分为以下几个部分:
SpringMVC配置
SpringMVC+Hibernate配置
SpringMVC+Ibatis配置
*SpringMVC+Shiro配置
为什么配置了两个持久层
SpringMVC的优缺点在此处就不再阐述,而持久层配置了两个的原因在于两者的优势和不足可以相互弥补。 **如此搭配也是初次尝试,在性能等方面的考虑和框架的冗余上必定考虑不足,如有明知的同学,请留言指出,万分感谢。** 在简单的增删改查上面,Hibernate做持久层无疑有着优势,利用IDE的功能可以快速的导出Entity和hbm.xml文件,而对这些简单的增删改查,hibernate所提供的find,save,update等方法,是十分便利的。然而,当处理复杂的表关系的时候,Hibernate的应用明显就要复杂很多,对于基础开发人员来说,具备足够的sql编写能力,但对于Hibernate的多表联查等复杂操作,及获取返回对象等就显得力有不逮,当团队里面需要快速开发的时候,就需要简化这些学习和摸索的时间,故用Ibatis来弥补这一缺点。 Ibatis在此场景里面最大的优势就是sql语句了,所有的操作都可以通过编写SQL语句来实现,这正好弥补Hibernate的不足。废话不多说,直接上代码。
spring的版本用的4.0+的,之前有文章记录
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: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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-lazy-init="true"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan"> <list> <!-- 可以加多个包 --> <value>com.web.app.wx.common.entity</value> </list> </property> <property name="mappingResources"> <list> ... </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="connection.characterEncoding">UTF-8</prop> <prop key="connection.useUnicode">true</prop> <prop key="hibernate.connection.url">${jdbc.url}</prop> <prop key="hibernate.connection.driver_class">${jdbc.driverClassName}</prop> <prop key="hibernate.connection.autocommit">true</prop> <prop key="hibernate.c3p0.min_size">5</prop> <!--最大连接数 --> <prop key="hibernate.c3p0.max_size">50</prop> <!--连接超时时间 --> <prop key="hibernate.c3p0.timeout">120</prop> <!--statemnets缓存大小 --> <prop key="hibernate.c3p0.max_statements">100</prop> <!--每隔多少秒检测连接是否可正常使用 --> <prop key="hibernate.c3p0.idle_test_period">120</prop> <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3 --> <prop key="hibernate.c3p0.acquire_increment">2</prop> <!-- 每次都验证连接是否可用 --> <prop key="hibernate.c3p0.validate">true</prop> </props> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${jdbc.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <property name="minPoolSize" value="10"/> <property name="maxPoolSize" value="100"/> <property name="maxIdleTime" value="1800"/> <property name="acquireIncrement" value="3"/> <property name="maxStatements" value="1000"/> <property name="initialPoolSize" value="10"/> <property name="idleConnectionTestPeriod" value="60"/> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="5"/> <property name="breakAfterAcquireFailure" value="true"/> <property name="testConnectionOnCheckout" value="false"/> <property name="autoCommitOnClose" value="true"/> </bean> <!-- 配置Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置事务异常封装 --> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" timeout="100"/> <tx:method name="save*" propagation="REQUIRED" timeout="100"/> <tx:method name="update*" propagation="REQUIRED" timeout="100"/> <tx:method name="delete*" propagation="REQUIRED" timeout="100"/> <tx:method name="get*" read-only="true" timeout="100"/> <tx:method name="*" timeout="100"/> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.web.app.wx.manager.service.*.*(..))"/> <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice --> <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/> </aop:config> <!--<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />--> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="fileEncoding" value="UTF-8"/> <property name="location" value="classpath:config.properties"/> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:sqlConfig.xml</value> </property> <property name="dataSource" ref="dataSource"> </property> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/server/**" value="myResourceHandler"/> </map> </property> <property name="order" value="100000"/> </bean> <bean id="myResourceHandler" name="myResourceHandler" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler"> <property name="locations" value="/server/portal"/> <property name="supportedMethods"> <list> <value>GET</value> <value>HEAD</value> <value>POST</value> </list> </property> </bean> </beans>
Servlet配置文件
<?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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="true"> <!-- 默认扫描的包路径 --> <context:component-scan base-package="com.web.app.wx" ></context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> <!-- JSON 数据信息传输转换配置:反序列化--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </list> </property> </bean> <!-- 定义跳转的文件的前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="exceptionHandler" class="com.web.app.wx.sys.handler.BizExceptionHandler"/> </beans>
Spring-Shiro配置文件
<?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" default-lazy-init="true"> <description>shiro 安全框架配置</description> <import resource="manager-servlet.xml"></import> <!-- Shiro Filter --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/admin/login" /> <!-- 不必设定successUrl,避免forward方式跳转,无法显示 数据,在Controller中直接redirect,可解决此问题--> <!--<property name="successUrl" value="/admin/base_info" />--> <property name="unauthorizedUrl" value="/err/unauthorizated"/> <property name="filterChainDefinitions"> <value> /admin/login = anon /admin/logout = anon /resources/** = anon /menu/** = roles["ROLE_SERVICE,ROLE_USER"] /clips/** = roles["ROLE_SERVICE,ROLE_USER"] /** = authc </value> </property> <property name="filters"> <map> <entry key="authc" value-ref="permissionFilter"></entry> </map> </property> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="weChatRealm"></property> </bean> <bean id="weChatRealm" class="com.web.app.wx.manager.realm.WeChatRealm"> <property name="adminService" ref="adminService"></property> </bean> <!-- 用户授权信息Cache, 采用EhCache --> <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache-shiro.xml"/> </bean> <bean id="permissionFilter" class="com.web.app.wx.manager.filter.PermissionsAuthorizationFilter"></bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> </beans>
文章篇幅有些长,我后续再写几篇来对此进行描述
相关文章推荐
- Apache Shiro 使用手册(一) Shiro架构介绍
- Apache Shiro 使用手册(二) Shiro 认证
- Apache Shiro 使用手册(五) Shiro 配置说明
- Apache Shiro 使用手册(四) Realm 实现
- 让Apache Shiro保护你的应用
- 基于Spring框架的Shiro配置方法
- 使用Shiro实现登录成功后跳转到之前的页面
- Spring+mybatis+shiro+freemarker+ehcache+ldap+mongo
- 第四章 INI配置
- 第五章 编码/加密
- 第六章 Realm及相关对象
- Shiro预览
- JFinal-Beetl-Shiro(JdbcRealm)-例子
- springrain技术详解
- Spring整合Dwr,Cannot locate BeanDefinitionParser for element [annotation-scan]
- S2SH整合Shiro之:SessionContext must be an HTTP compatible implementation
- S2SH整合Shiro之:java.lang.NoSuchMethodException: com.sun.proxy.$Proxy25
- spring3 MVC手工搭建Spring3项目demo
- 让Apache Shiro保护你的应用
- spring mvc 开发需要的jar包的用途详解