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<>());
	}
}
相关推荐
却话巴山夜雨时i31 分钟前
互联网大厂Java面试场景:从Spring到微服务的逐层提问
java·数据库·spring·微服务·日志·性能监控
小江的记录本31 分钟前
【Docker】Docker系统性知识体系与命令大全(镜像、容器、数据卷、网络、仓库)
java·网络·spring boot·spring·docker·容器·eureka
xxjj998a1 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
程序员榴莲1 小时前
设计模式之GoF设计模式(单例模式
单例模式·设计模式
Java成神之路-2 小时前
Spring 注解开发进阶实战:Bean 生命周期、 依赖注入及Properties配置(Spring系列4)
java·后端·spring
弹简特2 小时前
【JavaEE27-后端部分】Spring AOP 核心概念详解——把抽象变具象,让理论不再“飘”
java·spring·spring aop
弹简特2 小时前
【JavaEE29-后端部分】Spring AOP 切点表达式详解——精准定位,想切哪里切哪里
java·spring·spring aop
弹简特2 小时前
【JavaEE28-后端部分】Spring AOP 通知详解——五种“增强时机”,一网打尽
java·spring·spring aop
花千树-0102 小时前
Spring Boot 启动慢排查与优化实战指南
java·spring boot·后端·spring
云烟成雨TD3 小时前
Spring AI 1.x 系列【23】:工具配置详解(全局默认+运行时动态)
人工智能·python·spring