`
jinnianshilongnian
  • 浏览: 21424161 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2403122
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:2996146
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5630387
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:257181
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1592644
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:248792
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5845630
Group-logo
跟我学Nginx+Lua开...
浏览量:697465
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:779839
社区版块
存档分类
最新评论

Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效

 
阅读更多

今天一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() { 

  } 

 

 

6
0
分享到:
评论
10 楼 haoxun 2015-05-12  
开涛*哥用了您说的方法1和2 同时用 还是解决不了问题。可以帮我看看吗?在线盼答复。
9 楼 fanlu 2014-08-14  
我现在是在方法上加了了@RequiresPermissions,而AuthorizationAttributeSourceAdvisor类isAuthzAnnotationPresent找不到相应的annotation,是什么原因呢?
8 楼 jinnianshilongnian 2013-07-18  
月沉海雾 写道
jinnianshilongnian 写道
月沉海雾 写道
月沉海雾 写道
jinnianshilongnian 写道
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理

<tx:annotation-driven transaction-manager="txManager" />加上proxy-target-class="true"吗

还是不行。


你看看 其他的如<aop:config>之类的;或者配置文件发我

配置文件发你QQ邮箱了。


1、DefaultAdvisorAutoProxyCreator 中设置 proxyTargetClass  属性 为 true 即可


2、更好的方式可参考我的github
https://github.com/zhangkaitao/es/tree/master/web/src/main/resources
7 楼 月沉海雾 2013-07-18  
jinnianshilongnian 写道
月沉海雾 写道
月沉海雾 写道
jinnianshilongnian 写道
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理

<tx:annotation-driven transaction-manager="txManager" />加上proxy-target-class="true"吗

还是不行。


你看看 其他的如<aop:config>之类的;或者配置文件发我

配置文件发你QQ邮箱了。
6 楼 jinnianshilongnian 2013-07-18  
月沉海雾 写道
月沉海雾 写道
jinnianshilongnian 写道
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理

<tx:annotation-driven transaction-manager="txManager" />加上proxy-target-class="true"吗

还是不行。


你看看 其他的如<aop:config>之类的;或者配置文件发我
5 楼 月沉海雾 2013-07-18  
月沉海雾 写道
jinnianshilongnian 写道
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理

<tx:annotation-driven transaction-manager="txManager" />加上proxy-target-class="true"吗

还是不行。
4 楼 jinnianshilongnian 2013-07-18  
月沉海雾 写道
jinnianshilongnian 写道
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理

<tx:annotation-driven transaction-manager="txManager" />加上proxy-target-class="true"吗

是的
3 楼 月沉海雾 2013-07-18  
jinnianshilongnian 写道
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理

<tx:annotation-driven transaction-manager="txManager" />加上proxy-target-class="true"吗
2 楼 jinnianshilongnian 2013-07-18  
月沉海雾 写道
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。


估计jdk动态代理的,请使用cglib代理
1 楼 月沉海雾 2013-07-18  
我在controller里面的这么写@RequiresRoles()@RequestMapping(),好像controller就失效了,去掉@RequiresRoles,@RequestMapping就正常了,我看网上好多教程都是这么用,不知道为什么,能指点下吗?好久没上你博客了,你竟然换头像了。

相关推荐

Global site tag (gtag.js) - Google Analytics