今天一iteye网页在问答频道提问【Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效】,记录一下。
问题:
@ParentPackage("all") @Namespace("/project") public class ProjectAction extends BaseAction { public final static Logger logger = LoggerFactory .getLogger(ProjectAction.class); @Autowired(required=true) private ProjectService projectService;
如上代码@Autowired注入不了
分析:
1、首先从如上代码可以看出 走的是struts2注解,而且使用了struts2 convention 插件,这个插件会扫描如下配置的actionPackages 寻找action
<filter> <filter-name>struts2</filter-name> <filter-classfilter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>cn.javass</param-value> </init-param>
2、但此时并没有把action交给spring,
3、接下来,因为集成了spring(有struts2-spring-plugin),所以要使用StrutsSpringObjectFactory创建bean,代码分析
@Override public Object buildBean(String beanName, Map<String, Object> extraContext, boolean injectInternal) throws Exception { Object o; if (appContext.containsBean(beanName)) { o = appContext.getBean(beanName); //拿不到bean } else { Class beanClazz = getClassInstance(beanName); o = buildBean(beanClazz, extraContext); //所以创建了一个 } if (injectInternal) { injectInternalBeans(o); } return o; }
/** * @param clazz * @param extraContext * @throws Exception */ @Override public Object buildBean(Class clazz, Map<String, Object> extraContext) throws Exception { Object bean; try { // Decide to follow autowire strategy or use the legacy approach which mixes injection strategies if (alwaysRespectAutowireStrategy) {//默认false // Leave the creation up to Spring bean = autoWiringFactory.createBean(clazz, autowireStrategy, false); injectApplicationContext(bean); return injectInternalBeans(bean); } else { bean = autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); //只走构造器注入 bean = autoWiringFactory.applyBeanPostProcessorsBeforeInitialization(bean, bean.getClass().getName()); // We don't need to call the init-method since one won't be registered. bean = autoWiringFactory.applyBeanPostProcessorsAfterInitialization(bean, bean.getClass().getName()); return autoWireBean(bean, autoWiringFactory); } } catch (UnsatisfiedDependencyException e) { if (LOG.isErrorEnabled()) LOG.error("Error building bean", e); // Fall back return autoWireBean(super.buildBean(clazz, extraContext), autoWiringFactory); } }
我们在shiro里使用如下代码 去代理shiro的代理:
<bean id="proxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true"/> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean>
//StrutsSpringObjectFactory的如下代码将执行处理器的预处理
bean = autoWiringFactory.applyBeanPostProcessorsBeforeInitialization(bean, bean.getClass().getName());
//DefaultAdvisorAutoProxyCreator的postProcessBeforeInstantiation:将去完成代理bean 因此此时将返回代理Bean
//接着StrutsSpringObjectFactory的autoWireBean(bean, autoWiringFactory); 进行注入 所以此时注入到的是代理对象,因此如果字段注入 将注入不了。
解决方案
1、不使用actionPackages 而是 在类上加 @Controller @Scope 完全走spring
2、使用setter注入 而不是字段 如
private ProjectService projectService;
@Autowired(required=true)
public void setProjectService() {
}
相关推荐
关于shiro+struts2+hibernate+Spring+sitemash框架的毕业设计管理系统
shiro+struts2+mybatis+spring整合实例.
1.30 Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效 1.31 Spring事务不起作用 问题汇总 1.32 Spring3 Web MVC下的数据类型转换(第一篇)——《跟我学Spring3 Web MVC》抢先看 1.33 Spring 注入...
1,shiro+hibernate4+struts2+spring+easyui+fusioncharts部分bootstrap样式全注解零配置简单权限管理web项目 2,简单shiro权限管控 细度到按钮 3,安装方法按config.properties参数配置你的数据库 4,运行dbsql.sql 5,...
简单的权限管理搭建简单的权限管理搭建 shiro +hibernate4+struts2+spring+easyui+fusioncharts部分bootstrap样式(2)
shiro+spring+data+session+redis实现单点登录,这是一个不错的案例,学习和参考都是很不错的
Spring Boot+Apache Shiro+Spring MVC+MyBatis+Quartz+Druid DEMO
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
springMVC+mybatis+shiro+redis 项目整合demo。
本后台管理系统,采用流行的框架springMvc+spring+mybatis+shiro+redis+ehcache开发,实现了权限管理(菜单权限、数据权限),solr全文搜索引擎,activiti工作流程引擎,cas单点登陆等功能,完善的代码生成器 后期还...
基于 SpringBoot + Mybatis Plus + Shiro + mysql + redis构建的智慧云智能教育平台。架构上使用完全前后端分离。 支持多种题型:选择题、多选题、判断题、填空题、综合题以及数学公式。支持在线考试,教师在线批改...
Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理...
Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...
本系统(基于SpringBoot+MyBatis+Apache Shiro+Bootstrap+Thymeleaf) 可用于开发所有企业级WEB应用系统(如:各种后台管理系统、CRM、ERP、CMS、OA、博客、论坛等...)。响应式布局,支持大部分浏览器(如:IE9+...
shiro+Spring的demo,包含了数据库的表,本项目是基于maven。
1、基于springboot+shiro+jwt+vue+redis的后台管理系统源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料...
Maven项目构建: SpringMVC + Mybatis + SpringSecurity(权限控制到方法按钮) + Rest(服务) + Webservice(服务) + Quartz(定时调度)+ Lucene(搜索引擎) + HTML5 bootstrap 源码详细地址:...
基于SpringBoot2+Shiro+Thymeleaf的后台管理系统 基于SpringBoot2+Shiro+Thymeleaf的后台管理系统 基于SpringBoot2+Shiro+Thymeleaf的后台管理系统 基于SpringBoot2+Shiro+Thymeleaf的后台管理系统 基于SpringBoot2+...
● 实现方法: 使用MySQL 建立数据库。服务端使用SSM框架(Spring+SpringMVC+MyBatis)+shiro+Maven,使用Myeclipse进行开发。前端使用MUi和HUI框架和vue(与后台交互模板)和Html5+css3来实现移动端App的开发。
SpringMVC精品资源--Maven + Mysql + Shiro + SpringMVC + Spring