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<>());
	}
}
相关推荐
爱干饭的boy43 分钟前
手写Spring底层机制的实现【初始化IOC容器+依赖注入+BeanPostProcesson机制+AOP】
java·数据结构·后端·算法·spring
AD钙奶-lalala2 小时前
Spring Initializr(或者 IDEA 里新建 Spring Boot 项目)时 Dependencies 的选择
spring boot·spring·intellij-idea
风槐啊4 小时前
邪修实战系列(3)
java·ide·spring boot·spring·tomcat
波波烤鸭8 小时前
深入理解 Gateway 网关:原理、源码解析与最佳实践
java·spring·gateway
心之伊始9 小时前
Spring Cloud Gateway:构建智能API网关的终极指南
java·spring
白玉cfc9 小时前
【OC】单例模式
开发语言·ios·单例模式·objective-c
心月狐的流火号9 小时前
Spring Bean 生命周期详解——简单、清晰、全面、实用
java·spring
acaad10 小时前
Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题
spring·apache·excel
王嘉俊92511 小时前
SpringBoot应用开发指南:从入门到高级配置与自动装配原理
java·spring boot·后端·spring·ssm
choice of12 小时前
SpringMVC通过注解实现全局异常处理
java·后端·spring