我是靠谱客的博主 勤奋玉米,这篇文章主要介绍Struts2 Action中使用注解注入调用service,现在分享给大家,希望可以做个参考。

//struts2中Action如果需要使用spring的注解注入,则需要将Action交给spring管理。
1.需要引入struts2-spring-plugin-2.3.15.1.jar(不同struts版本对应不同的包),jar包中有xml配置文件
其中设置了ObjectFactory为spring 这个struts会自动加载不用设置

2.需要在Action类中增加注释
  @Component(" TestAction ") 
  @Scope("prototype")
  public class TestActionextends ActionSupport {}
 告诉spring这个action需要管理为bean,然后这个类就可以使用spring的 @Resource
 来注入serviceBean实例


3.修改struts.xml

<action name="test" class="com.action.TestAction">
改为
<action name="test" class=" TestAction"> class和上面 @Component(" TestAction ") 定义的bean id对应

此时应该不会出现在Action中@Resource service为null的情况了。

最后

以上就是勤奋玉米最近收集整理的关于Struts2 Action中使用注解注入调用service的全部内容,更多相关Struts2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部