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 interface
		Command command = createCommand();
		System.out.println("hash: " + command.hashCode());
		// set the state on the (hopefully brand new) Command instance
		command.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/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://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<>());
	}
}
相关推荐
麦兜*1 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
CHENWENFEIc4 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试
高兴达4 小时前
RPC--Netty客户端实现
java·spring·rpc
要开心吖ZSH8 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring
考虑考虑8 小时前
Springboot3.4.x中的@Bean使用
spring boot·后端·spring
萧曵 丶11 小时前
Spring @TransactionalEventListener
java·数据库·spring·事务·transactional·异步
N_NAN_N12 小时前
类图+案例+代码详解:软件设计模式----原型模式
java·设计模式·原型模式
默默coding的程序猿13 小时前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
Kyrie_Li13 小时前
(十五)Spring Test
java·后端·spring
布兰妮甜14 小时前
单例模式在前端(JavaScript)中的实现与应用
前端·javascript·单例模式