Spring 版本:3.2.x,  4.0.x

【问题说明】

首先介绍下配置出错情况:

(1)项目中,Spring3 and Spring4 的 applicationContext.xml aop 配置如下:

... ...
<aop:aspectj-autoproxy expose-proxy="true"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> <aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.app..service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
... ...

未加入 Shiro 之前,所有 applicationContext 的 getBean(...) 方法能正常获取到相应对象实例。 调用 getBeanDefinitionNames() 输出的 Beans 如下:

Bean Name Bean Class
userDaoImpl class com.app.dao.impl.UserDaoImpl
resourceServiceImpl class com.app.service.impl.ResourceServiceImpl$$EnhancerBySpringCGLIB$$4ddf6901
roleServiceImpl class com.app.service.impl.RoleServiceImpl
userServiceImpl class com.app.service.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$e7bcd944

以上 bean 都采用 @Repository 或 @Service 的注解方式。

注意标红Class名称,这是因为被 CGLIB 动态代理。

(2)根据 Shiro 示例,新增配置文件 applicationContext-security.xml,然后配置 Shiro 如下:

... ...

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
</bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
</bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean> <bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"/> ... ...

加入 Shiro 之后,发现有些地方 getBean(...) 出错,调用 getBeanDefinitionNames() 输出的 Beans 如下:

Bean Name Bean Class
userDaoImpl class com.app.dao.impl.UserDaoImpl
resourceServiceImpl class $Proxy38
roleServiceImpl class com.app.service.impl.RoleServiceImpl
userServiceImpl class $Proxy32

可以注意到标红Class名称,这是由于配置 DefaultAdvisorAutoProxyCreator,又被 JDK 动态二次代理。

【解决办法】

两次都以 CGLIB 动态代理。在 AOP 配置加上 proxy-target-class 属性,值为 true。具体如下:

(1)在 applicationContext.xml 中配置

... ...
<aop:config expose-proxy="true" proxy-target-class="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.app..service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
... ...

(2)在 applicationContext-security.xml 配置 Shiro

... ...
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>
... ...

修改完成后,重新运行程序,getBean 恢复正常。

【参考资料】

spring的二次代理原因及如何排查  http://jinnianshilongnian.iteye.com/blog/1894465

Spring mvc 中有关 Shiro 1.2.3 配置问题的更多相关文章

  1. 细说shiro之五:在spring框架中集成shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

  2. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  3. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  4. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  5. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  6. Spring MVC中处理静态资源的多种方法

    处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...

  7. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  8. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  9. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

随机推荐

  1. c# 集合ArrayList;特殊集合Stack、Queue

    一)  ArrayList 1.foreach遍历数组中各个元素,执行内部语句 2.  3. 4.  myarry.Clear();//将集合清空 bool b = myarry.Contains(3 ...

  2. 在mac中用终端来运行.c文件

    第一步:打开终端,位置在lauchpad中去找搜索. 第二步:建一个.c文件. 第三步: 在终端输入.c路径.用cd命令 第五步:cc -c +tab键.生成.O文件 第六步:cc +tab键.生成. ...

  3. 我的学习笔记之node----node.js+socket.io实时聊天(1) (谨此纪念博客开篇)

    本想着从hello word开篇,也确实写了相关学习笔记.各种原因吧,现在又着急写出作品,便作罢. 这里将记录一个node.js+socket.io的实时聊天程序.(当然我也是跟着网上各种教程资料学习 ...

  4. jsp中的contentType与pageEncoding的区别和作用

    jsp中的contentType与pageEncoding的区别和作用   <%@ page contentType="text/html; charset=utf-8" p ...

  5. SRM 600(1-250pt,500pt)

    DIV1 250pt 题意:给定一个vector<int>A,若能从里面选出一些数,使得他们按位或的值为x,则称x为吉利数.给定k,问最少要从A里面去掉多少个数,才能使k变为不吉利数. 解 ...

  6. The Minimum Length - HUST 1010(求最小循环节)

    题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度.   分析:其实就是求最小循环节.......串的长度 - 最大 ...

  7. K - Candies(最短路+差分约束)

    题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...

  8. Dijkstra算法为什么权值不能为负

    Dijkstra算法当中将节点分为已求得最短路径的集合(记为S)和未确定最短路径的个集合(记为U),归入S集合的节点的最短路径及其长度不再变更,如果边上的权值允许为负值,那么有可能出现当与S内某点(记 ...

  9. AS3 Graphics 多次绘制

    AS3中 Sprite和Shape类都持有一个Graphics对象,利用Graphics对象可以方便的利用内置的绘图方法绘制一些简单的图形. 之前在游戏中做新手引导的时候利用显示对象的BlendMod ...

  10. 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...