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<>());
	}
}
相关推荐
顾漂亮17 分钟前
Token快过期的三种续期方案
java·spring·状态模式
zl9798995 小时前
SpringBoot-数据访问之MyBatis与Redis
java·spring boot·spring
pcm1235677 小时前
java中的单例模式
java·开发语言·单例模式
杨DaB7 小时前
【SpringCloud】回顾微服务
spring·spring cloud·微服务
lang201509288 小时前
Spring Boot集成Spring Integration全解析
spring boot·后端·spring
大G的笔记本9 小时前
Spring IOC和AOP
java·后端·spring
hello 早上好10 小时前
解析 Prompt 工程中的四大核心角色
spring
兮动人14 小时前
Spring中@Configuration注解的proxyBeanMethods属性详解
java·后端·spring
Jing_jing_X15 小时前
Spring 自动注入是怎么实现的?从 @Component 到 @Autowired 的完整流程
java·后端·spring