Action 中注入Spring 管理的Bean

ProductAction 是一个 Action,处理页面的请求,其中的save()方法使用到了业务层 ProductService对象,Spring 管理这个对象,所以涉及到 Action 中注入 Spring 管理的bean的问题。

记得导入spring整合structs2 的jar包 struts2-spring-plugin-2.3.31.jar
下面介绍两种注入方式

package com.jxust.ssh.action;
..
public class ProductAction extends ActionSupport implements ModelDriven<Product> {..private ProductService produceService;public void setProduceService(ProductService produceService) {this.produceService = produceService;}public String save(){//这里需要用到ProductService对象produceService.save(product);return "index";}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

注入方法一(Action由Structs管理)

struts.xml 文件中,Action 的 class 为该 Action 的类路径,而在 Spring配置文件中不需要添加 ProductAction 的bean配置。当我们使用Action类时,由于 produceService 已经配置了相关的bean,所以会自动装配。

structs2中的配置

structs2配置Action的文件

struts.xml

<action name="product_*" class="com.jxust.ssh.action.ProductAction" method="{1}"><result name="index">index.jsp</result></action > 
  • 1
  • 2
  • 3

类名使用完整路径class="com.jxust.ssh.action.ProductAction"

spring中的配置

spring的配置bean的文件

applicationContext.xml

    <!-- 配置业务层的类 --><bean id="produceService" class="com.jxust.ssh.service.ProductService">...</bean>
  • 1
  • 2
  • 3
  • 4

bean 的 id 值必须和 Action 中的变量名相同

private ProductService produceService;
//两者的名称要一一对应
<bean id="produceService".....
  • 1
  • 2
  • 3

注入方法二(Action由Spring管理)

struts.xml在配置 ProductAction 时,需要将class 名称和 Spring 配置文件中的相对应的ProductAction 的bean的 Id 的 名称保持一致,系统即可通过Spring来装配和管理Action。

建议使用这种方式,便于使用AOP管理

structs2中的配置

structs2配置Action的文件

struts.xml

    <action name="product_*" class="productActionBean" method="{1}"><result name="index">index.jsp</result></action > 
  • 1
  • 2
  • 3

class的路径设置成spring 配置的bean的名称class="productActionBean"

struts.xml里action的class应写成spring里对应bean的id 。只有这样spring容器才会自动的将papermanager注入

spring中的配置

spring的配置bean的文件

applicationContext.xml

    <!-- 配置Action 的类 --><bean id="productActionBean" class="com.jxust.ssh.action.ProductAction" scope="prototype"><property name="produceService" ref="productServiceBean"></property></bean><!-- 配置业务层的类 --><bean id="productServiceBean" class="com.jxust.ssh.service.ProductService">..</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

手动配置 ProductAction 中 ProductService 对象的注入,命名bean的名称为 productActionBean
spring中bean默认为单例的,要改为scope=”prototype”多例

参考

sun_zhicheng的博客http://blog.csdn.net/sun_zhicheng/article/details/24232129

strtus中action注入spring bean相关推荐

  1. java 从一个容器获取对象,Java 如何实现从spring容器中获取注入的bean对象

    Java 如何实现从spring容器中获取注入的bean对象 发布时间:2020-11-03 15:27:37 来源:亿速云 阅读:70 作者:Leah 这期内容当中小编将会给大家带来有关Java 如 ...

  2. 【SpringBoot】查看运行环境中所有的spring bean

    前言 spring boot : 2.0.0.RELEASE maven eclipse 在开发&调试过程中,提示某个Bean找不到.此时就需要查看运行环境中有没有这个bean,以便快速排除出 ...

  3. 为什么你写的拦截器中不能注入Java bean?

    一.如何实现拦截器 在Spring Boot项目中,拦截器经常被用来做登陆验证,日志记录等操作.拦截器是Spring提供的,所以可以将拦截器注成bean,由IOC容器来管理.实现拦截器的方式很简单,主 ...

  4. Struts2 Action 与Spring bean 作用域

    struts2 的action 是没有scope的,但通过引用spring bean 可以达到有scope功能. <action name="xxxAction" class ...

  5. 如何将Spring Bean注入到JSF Converter

    为什么80%的码农都做不了架构师?>>>    在项目中,因为想在自定义的JSF Converter中使用Spring Bean,经过搜索和测试,有两种方式可以达到目的 1)使用工具 ...

  6. Spring——Bean管理-xml方式进行属性注入

    目录 一.xml方式创建对象 二.xml方式注入属性 第①种方式注入:set方法注入 第②种方式注入:有参构造函数注入 constructor-arg:通过构造函数注入 用name标签属性: 不按照顺 ...

  7. 摆脱困境:将属性值注入配置Bean

    Spring Framework对将从属性文件中找到的属性值注入到bean或@Configuration类中提供了很好的支持. 但是,如果将单个属性值注入这些类中,则会遇到一些问题. 这篇博客文章指出 ...

  8. IOC操作Bean管理XML方式(注入内部 bean 和 级联赋值)

    目录 1.注入属性-内部 bean (1)一对多关系:部门和员工 (2)在实体类之间表示一对多关系 (3)在Spring 配置文件中进行配置 2.注入属性-级联赋值 (1)第一种写法类似外部bean注 ...

  9. Java反射生成对象注入spring(结合工厂模式)

    利用工厂模式进行代码扩展,而不用修改原来代码.在配置文件配置要生成的类,通过反射交由工厂生成对象,并将此对象交给spring管理,在项目中遇到此问题并予以解决,做一下记录. package cn.ea ...

最新文章

  1. matlab中prismastic,魅力值是什么意思
  2. 阿里云有史以来最大的一次优惠,爆款限时抢,至少省10000+
  3. Scrum项目1.0
  4. java8新特性-lambda表达式和stream API的简单使用
  5. 关于OPC自动化接口编程(OPCDAAuto.dll)几点注意问题
  6. 2021第一场 | 阿里云高校计划训练营全面升级!0成本体验云计算入门到进阶
  7. HappyLeetcode50:Rotate Array
  8. linux block设备,Linux I/O Block--块设备的表示
  9. 辐射避难所服务器维护,《辐射:避难所Online》6月2日停服维护更新公告
  10. PAT学习资料汇总(PAT甲级、PAT顶级、PAT考试经验)
  11. ubuntu之anaconda之编辑器
  12. python 在线培训费用-Python人工智能在线培训班学费多少钱?
  13. SNMP学习(2)——SNMP实战
  14. erdas几何校正_erdas图像几何校正操作步骤指南.doc
  15. DAVE笔记--Micrium uc-Probo Oscilloscope调试
  16. java画板_java 画板画图程序
  17. 7个黑科技十足的微信小程序,每一个都能让你念念不舍!
  18. html内边距的顺序,html中内边距和外边距之间的区别是什么? - 收获啦
  19. DRF实战5 - 商品管理
  20. 存量时代,汽车4S店要通过什么服务来吸引消费者

热门文章

  1. 80486/486/Intel486 架构/流水线及其优化
  2. 三维旋转四元数系列(4.四元数三维旋转表达)
  3. mysql udf提权_三分钟解析postgresql提权
  4. pcp pmda mysql_linux下的mysql的安装
  5. 使用UE4发布安卓平台游戏
  6. 室内温湿度监测系统解决方案
  7. 有关上次的字符串是否相等的比较
  8. python---内置模块
  9. zookeeper多种方式安装
  10. ecshop_商品描述远程图片自动本地化插件