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<>());
	}
}
相关推荐
YDS82932 分钟前
SpringCloud —— Elasticsearch入门详解
spring·elasticsearch·spring cloud
xiaodaidai丶1 小时前
解决Sa-Token在 Spring MVC + WebFlux 混合架构中流式接口报错SaTokenContext 上下文尚未初始化的问题
spring·架构·mvc
QWQ___qwq1 小时前
Spring Security + MyBatis-Plus 实现自定义数据库用户认证
数据库·spring·mybatis
de_wizard1 小时前
【mybatis】基本操作:详解Spring通过注解和XML的方式来操作mybatis
xml·spring·mybatis
6+h2 小时前
【Spring】AOP核心之原始对象与代理对象
java·python·spring
Java基基3 小时前
Spring让Java慢了30倍,JIT、AOT等让Java比Python快13倍,比C慢17%
java·开发语言·后端·spring
future02103 小时前
Spring AOP核心机制:代理与拦截揭秘
java·开发语言·spring·面试·aop
零雲4 小时前
java面试:Spring是如何解决循环依赖问题的
java·spring·面试
·中年程序渣·6 小时前
Spring AI Alibaba入门学习(一)
人工智能·学习·spring
彭于晏Yan7 小时前
Redis缓存更新策略
spring boot·redis·spring·缓存