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. Yii框架下不同contoller之间的方法调用

    一个contoller CustomerController里怎么调用另一个controller里的action,Acontoller调用SiteContoller的actionShow($id), ...

  2. Ganglia监控搭建

    一.Ganglia介绍: Ganglia是一个监控服务器.集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标.Ga ...

  3. lightoj 1011 最大权重匹配或最大费用流

    由于暂时不会KM算法,只能用最大费用流来做了. 题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 #include <cst ...

  4. L - Subway - POJ 2502

    题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到 ...

  5. React Native专题

    转载注明出处:地址:http://www.lcode.org本文出自:[江清清的技术专栏]本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶 ...

  6. PHP面试题(二)

    前言 从网上找了一套号称是百度的php面试题目,这里记录一下 PHP的gc机制 php的垃圾回收机制注意以下几点即可: 引用计数refcount和is_ref,也就是php不会随意的malloc内存空 ...

  7. Linux用户与用户组的详解

    1,用户和用户组文件   在linux中,用户帐号,用户密码,用户组信息和用户组密码均是存放在不同的配置文件中的.   在linux系统中,所创建的用户帐号和其相关信息(密码除外)均是存放在/etc/ ...

  8. NOI2015 程序自动分析 prog

    何等水题 某神犇仿关押罪犯的写法 却写挂了  然而实际上并不需要补集之类的 #include<iostream> #include<cstring> #include<c ...

  9. OpengGL ES2.0 Using NDK

    使用C语言在Android Studio中编写OpenGL ES,首要的任务就是配置编程环境. 在最新的Android Studio中,可以直接编译C/C++源代码.本人的版本是Android Stu ...

  10. angular ng-class使用笔记

    在前面Angularjs开发一些经验总结中说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJO( ...