当前位置: 首页 > news >正文

Spring 6.0官方文档示例(23): singleton类型的bean和prototype类型的bean协同工作的方法(二)

使用lookup-method:
一、实体类:

package cn.edu.tju.domain2;import java.time.LocalDateTime;
import java.util.Map;public class Command {private Map<String, Object> state;public Map<String, Object> getState() {return state;}public void setState(Map<String, Object> state) {this.state = state;}public Object execute(){System.out.println(LocalDateTime.now().toLocalTime());return null;}}
package cn.edu.tju.domain2;import java.util.HashMap;public abstract class CommandManager {public Object process(Object commandState) {// grab a new instance of the appropriate Command interfaceCommand command = createCommand();System.out.println("hash: " + command.hashCode());// set the state on the (hopefully brand new) Command instancecommand.setState(new HashMap<String, Object>());return command.execute();}// okay... but where is the implementation of this method?protected abstract Command createCommand();}

二、配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean class="cn.edu.tju.domain2.CommandManager" id="commandManager"  ><lookup-method bean="command" name="createCommand"/></bean><bean class="cn.edu.tju.domain2.Command" id="command" scope="prototype" ></bean></beans>

三、主类:

package cn.edu.tju;import cn.edu.tju.domain2.CommandManager;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.HashMap;public class TestDiffScope2 {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test8.xml","test2.xml");CommandManager commandManager = context.getBean("commandManager", CommandManager.class);commandManager.process(new HashMap<String, Object>());CommandManager commandManager2 = context.getBean("commandManager", CommandManager.class);commandManager2.process(new HashMap<>());}
}
http://www.lryc.cn/news/96348.html

相关文章:

  • Docker Compose 容器编排
  • while循环
  • 从JVM指令看String对象的比较
  • python与深度学习(六):CNN和手写数字识别二
  • Linux使用教程
  • 项目名称:智能家居边缘网关项目
  • SciencePub学术 | 物联网类重点SCIEEI征稿中
  • EtherNet/IP转Modbus网关以连接AB PLC
  • mysql用户添加
  • 628. 三个数的最大乘积
  • linux驱动开发入门(学习记录)
  • SpringCloud-Alibaba之Sentinel熔断与限流
  • 深“扒”云原生高性能分布式文件系统JuiceFS
  • opencv-18 什么是色彩空间?
  • RedHat离线安装工具yum+gcc+pcre+zlib+openssl+openssh
  • Redis概述及安装、使用和管理
  • 【算法第十一天7.25】二叉树前、中、后递归、非递归遍历
  • Linux搭建Promtail + Loki + Grafana 轻量日志监控系统
  • [PyTorch][chapter 44][RNN]
  • 20230726----重返学习-vue3项目实战-知乎日报第3天-TS-简历
  • TypeScript 在前端开发中的应用实践
  • 商业密码应用安全性评估量化评估规则2023版更新点
  • 【软件测试】单元测试工具---Junit详解
  • 【算法基础:搜索与图论】3.4 求最短路算法(Dijkstrabellman-fordspfaFloyd)
  • 【Matlab】基于卷积神经网络的数据分类预测(Excel可直接替换数据)
  • 【C++ 重要知识点总结】自定义类型-枚举和联合
  • Centos MySql安装,手动安装保姆级教程
  • 电脑C盘空间大小调整 --- 扩容(扩大/缩小)--磁盘分区大小调整/移动
  • centos7设置网桥网卡
  • TCP模型和工作沟通方式