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

这篇具有很好参考价值的文章主要介绍了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;
	}
}

三、主类:文章来源地址https://www.toymoban.com/news/detail-614792.html

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<>());
	}
}

到了这里,关于Spring 6.0官方文档示例(22): singleton类型的bean和prototype类型的bean协同工作的方法(一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?

    一、主类: 二、配置类: 三、singleton bean的创建流程 运行到context.refresh(); 进入refresh方法: 向下运行到红线位置时: 会实例化所有的singleton bean.进入finisheBeanFactoryInitialization方法: 向下拖动代码,可以看到beanFactory.preInstantiateSingletons(); 进入preInstantiateSingletons方法: 可以看

    2024年02月16日
    浏览(28)
  • Spring Security在6.0弃用WebSecurityConfigurationAdapter后该如何自定义配置介绍(新旧示例)

    在旧版的配置中,Security需要我们写一个类去继承他的WebSecurityConfigurerAdapter并把这个配置注入到容器中 在继承这个类后,我们可以在WebSecurityConfigurer里面去重写WebSecurityConfigurationAdapter类里面的一些方法来实现自定义过滤链等操作 实现自定义过滤链需要重写configure(HttpSecurit

    2024年02月08日
    浏览(32)
  • Spring Cloud Alibaba 官方中文文档

    Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿

    2024年02月14日
    浏览(43)
  • 【官方中文文档】Mybatis-Spring #简介

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession 并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException 。 最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。 Sp

    2024年02月11日
    浏览(30)
  • 【官方中文文档】Mybatis-Spring #搭配 Spring Boot

    请查看 MyBatis Spring-boot-starter 子项目获取更多信息。

    2024年02月11日
    浏览(30)
  • 【笔记】Spring Boot 历史官方文档学习(持续更新)

    Spring Boot 2014正式发布1.0版本,距今已经快10年了。看历史官方文档了解重点feature, 帮助自己建立知识网络。 与 Spring 5 官网历史文档学习 一样,尽量保证不误解文档作者的原意,不好翻译的会有原文摘录(包括一些专有名词),并辅以自己的理解。限于篇幅原因,只摘录工作

    2024年02月10日
    浏览(27)
  • 【官方中文文档】Mybatis-Spring #使用 SqlSession

    在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession 。 一旦你获得一个 session 之后,你可以使用它来执行映射了的语句,提交或回滚连接,最后,当不再需要它的时候,你可以关闭 session。 使用 MyBatis-Spring 之后,你不再需要直接使用 SqlSessionFactory 了,因为你的 bean 可以被

    2024年02月11日
    浏览(30)
  • 【官方中文文档】Mybatis-Spring #注入映射器

    与其在数据访问对象(DAO)中手工编写使用 SqlSessionDaoSupport 或 SqlSessionTemplate 的代码,还不如让 Mybatis-Spring 为你创建一个线程安全的映射器,这样你就可以直接注入到其它的 bean 中了: 注入完毕后,映射器就可以在你的应用逻辑代码中使用了: 注意代码中并没有任何的对

    2024年02月11日
    浏览(28)
  • Spring MVC官方文档学习笔记(二)之DispatcherServlet

    1.DispatcherServlet入门 (1) Spring MVC是以前端控制器模式(即围绕着一个中央的Servelt, DispatcherServlet)进行设计的,这个DispatcherServlet为请求的处理提供了一个共用的算法,即它都会将实际的请求处理工作委托给那些可配置的组件进行执行,说白了,DispatcherServlet的作用就是进行统一调度,并

    2024年02月07日
    浏览(71)
  • Spring AOP官方文档学习笔记(二)之基于注解的Spring AOP

    1.@Aspect注解 (1) @Aspect注解用于声明一个切面类,我们可在该类中来自定义切面,早在Spring之前,AspectJ框架中就已经存在了这么一个注解,而Spring为了提供统一的注解风格,因此采用了和AspectJ框架相同的注解方式,这便是@Aspect注解的由来,换句话说,在Spring想做AOP框架之前,

    2023年04月17日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包