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

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

一、配置文件:

<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.domain.CommandManager" id="commandManager"  ></bean><bean class="cn.edu.tju.domain.Command" id="command" scope="prototype" ></bean></beans>

二、实体类

package cn.edu.tju.domain;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.domain;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;import java.util.Map;public class CommandManager implements ApplicationContextAware {private ApplicationContext applicationContext;public Object process(Map<String, Object> commandState) {// grab a new instance of the appropriate CommandCommand command = createCommand();// set the state on the (hopefully brand new) Command instancecommand.setState(commandState);return command.execute();}protected Command createCommand() {// notice the Spring API dependency!return this.applicationContext.getBean("command", Command.class);}public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}
}

三、主类:

package cn.edu.tju;import cn.edu.tju.domain.CommandManager;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.HashMap;public class TestDiffScope {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test7.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/99019.html

相关文章:

  • Android平台GB28181设备接入侧如何同时对外输出RTSP流?
  • el-Cascader 中div上绑定keyDown事件
  • elementUI 表格滚动分页加载请求数据
  • JAVA面试总结-Redis篇章(五)——持久化
  • 【数据结构】·顺序表函数实现·赶紧学起来呀
  • C++,类和对象-多态,制作饮品
  • 网站分析:学习如何分析目标网站的页面结构和URL规律,确定爬取目标和策略。
  • 《向量数据库指南》:向量数据库Pinecone如何集成数据湖
  • Vue3中使用pinia
  • Mysql中(@i:=@i+1)的介绍
  • Nexperia和KYOCERA AVX Components Salzburg 就车规氮化镓功率模块达成合作
  • 数据库应用:Redis安装部署
  • 7.Docker-compose
  • 多线程:管程法
  • 7.1 String StringBuffer 和 StringBuilder 的区别是什么? String 为什么是不可变的?
  • 【C++STL标准库】容器适配器
  • 2023深圳杯(东三省)数学建模ABC题思路及代码
  • Set集合类详解(附加思维导图)
  • 【vue3】vue3接收props以及emit的用法
  • 【Lua学习笔记】Lua入门
  • LLM Data Pipelines: 解析大语言模型训练数据集处理的复杂流程
  • 如何使用postman判断返回结果是否正确
  • A General framework for Prompt
  • 使用python将PDF转word
  • CMU 15-445 -- Logging Schemes - 17
  • 逻辑回归分析实战(根据鸢尾花的性质预测鸢尾花类别)
  • 【每日一题】2050. 并行课程 III
  • 【kubernetes系列】kubernetes之使用kubeadm搭建高可用集群
  • SpringBoot 快速实现 IP 地址解析
  • 【云原生】Docker镜像的创建,Dockerfile