Spring 6.0官方文档示例(22): singleton类型的bean和prototype类型的bean协同工作的方法(一)

一、配置文件:

<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.domain.CommandManager" id="commandManager"  >
	</bean>

	<bean class="cn.edu.tju.domain.Command" id="command" scope="prototype" >
	</bean>
	
</beans>

二、实体类

package cn.edu.tju.domain;

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.domain;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

public class CommandManager implements ApplicationContextAware {
	private ApplicationContext applicationContext;
	public Object process(Map<String, Object> commandState) {
		// grab a new instance of the appropriate Command
		Command command = createCommand();
		// set the state on the (hopefully brand new) Command instance
		command.setState(commandState);
		return command.execute();
	}
	protected Command createCommand() {
		// notice the Spring API dependency!
		return this.applicationContext.getBean("command", Command.class);
	}
	public void setApplicationContext(
			ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}

三、主类:

package cn.edu.tju;

import cn.edu.tju.domain.CommandManager;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;

public class TestDiffScope {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test7.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<>());
	}
}
相关推荐
黄名富2 分钟前
Spring Cloud — 深入了解Eureka、Ribbon及Feign
分布式·spring·spring cloud·微服务·eureka·ribbon
m0_748248231 小时前
Spring Framework 中文官方文档
java·后端·spring
先睡1 小时前
Spring MVC的基本概念
java·spring·mvc
LUCIAZZZ1 小时前
SkyWalking快速入门
java·后端·spring·spring cloud·微服务·springboot·skywalking
m0_748245172 小时前
SpringCloud-使用FFmpeg对视频压缩处理
spring·spring cloud·ffmpeg
F20226974862 小时前
Spring MVC 对象转换器:初级开发者入门指南
java·spring·mvc
楠枬3 小时前
网页五子棋——对战后端
java·开发语言·spring boot·websocket·spring
攻城狮7号3 小时前
【第三节】C++设计模式(创建型模式)-单例模式
c++·单例模式·设计模式
黄名富4 小时前
Spring Cloud — Hystrix 服务隔离、请求缓存及合并
java·分布式·spring·spring cloud·hystrix·微服务
神马都会亿点点的毛毛张4 小时前
【SpringBoot教程】SpringBoot整合Caffeine本地缓存及Spring Cache注解的使用
java·spring boot·后端·spring·缓存·caffeine