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<>());
	}
}
相关推荐
trow5 分钟前
Spring 手写简易IOC容器
后端·spring
不会编程的阿成1 小时前
spring aop的概念与实战以及面试项目题
java·spring·面试
用户0595661192093 小时前
Java 21 与 Spring Boot 3.2 微服务开发从入门到精通实操指南
java·spring·编程语言
conkl3 小时前
Linux 并发编程:从线程池到单例模式的深度实践
linux·运维·服务器·阿里云·单例模式·云计算
都叫我大帅哥4 小时前
Spring Batch中的ItemProcessor:数据流水线的“魔法加工厂”🎩
java·spring
都叫我大帅哥5 小时前
Spring Batch:让大数据处理像流水线一样丝滑
java·spring
迢迢星万里灬6 小时前
Java求职者面试指南:Spring, Spring Boot, Spring MVC, MyBatis技术点深度解析
java·spring boot·spring·mybatis·面试指南
都叫我大帅哥8 小时前
Spring Batch中的ItemWriter:数据搬运工的“终极驿站” 📦
java·spring
杰_happy15 小时前
设计模式:原型模式(C++)
c++·设计模式·原型模式
CUIYD_198917 小时前
Spring MVC 处理静态资源请求 - ResourceHandler
java·spring·mvc