Spring入门篇 学习笔记

配置项

  • Id: 整个 IoC 容器中的唯一标识
  • Class: 具体实例化的类(必须配置项)
  • Scope: 作用域
  • Constructor arguments: 构造器参数
  • Properties: 属性
  • Autowiring mode: 自动装配模式
  • lazy-initialization mode: 懒加载模式
  • Initialization/destruction method: 初始化/销毁 方法

作用域

  • singleton: 单例(默认模式),指一个 Bean 容器中只存在一份
  • prototype: 每次请求(每次使用创建新的实例),destory 方式不生效
  • request: 每次 http 请求创建一个实例且仅在当前 request 內有效
  • session: 同上,每次 http 请求创建,当前 session 内有效
  • global session: 基于 portlet 的 web 中有效(portlet 定义了 global session),如果是在 web 中同 session

作用域示例

添加 BeanScope:

public class BeanScope {

	public void say() {
System.out.println("BeanScope say : " + this.hashCode());
} }

singleton

添加配置文件 spring-beanscope-singleton.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id="beanScope" class="com.karonda.bean.BeanScope" scope="singleton"></bean> </beans>

添加测试 TestBeanScopeSingleton:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScopeSingleton extends UnitTestBase { public TestBeanScopeSingleton() {
super("classpath*:spring-beanscope-singleton.xml");
} @Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say(); BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
} }

prototype

添加配置文件 spring-beanscope-prototype.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id="beanScope" class="com.karonda.bean.BeanScope" scope="prototype"></bean> </beans>

添加测试 TestBeanScopePrototype:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScopePrototype extends UnitTestBase { public TestBeanScopePrototype() {
super("classpath*:spring-beanscope-prototype.xml");
} @Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say(); beanScope = super.getBean("beanScope");
beanScope.say();
} }

源码:learning-spring

学习 Spring (三) Bean 的配置项 & 作用域的更多相关文章

  1. Spring三 Bean的三种创建方式

    创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...

  2. Spring的Bean有哪些作用域?

    Spring的Bean有以下五种作用域: 1.singleton:SpringIOC容器只会创建该Bean的唯一实例: 2.prototype:每次请求都创建一个实例: 3.requset:每次HTT ...

  3. 学习Spring(三) 设置Bean作用域

    Spring中几种不同的作用域 Singleton     每个Spring IoC容器中创建一个Bean实例 Prototype    每次请求时创建一个新的Bean实例 Request     为 ...

  4. Spring学习记录(三)---bean自动装配autowire

    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...

  5. Spring(三)Bean继续入门

    一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...

  6. 学习 Spring (四) Bean 的生命周期

    Spring入门篇 学习笔记 定义 --> 初始化 --> 使用 --> 销毁 初始化 实现 org.springframework.beans.factory.Initializi ...

  7. JMeter学习(三)元件的作用域与执行顺序

    1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其它元件(conf ...

  8. JMeter学习(三)元件的作用域与执行顺序(转载)

    转载自 http://www.cnblogs.com/yangxia-test 1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它 ...

  9. 浅析Spring中bean的作用域

    一.前言   刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...

随机推荐

  1. Spring配置文件中条件判断标签

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Prop ...

  2. CF341D Iahub and Xors

    CF341D Iahub and Xors 给定一个 \(n\times n\) 的矩阵,平面异或,求平面异或和 \((n\leq10^3,\ m\leq10^5)\) 树状数组 这里主要是记录一下板 ...

  3. Generative Adversarial Nets[Vanilla]

    引言中已经较为详细的介绍了GAN的理论基础和模型本身的原理.这里主要是研读Goodfellow的第一篇GAN论文. 0. 对抗网络 如引言中所述,对抗网络其实就是一个零和游戏中的2人最小最大游戏,主要 ...

  4. [Spark][Flume]Flume 启动例子

    Flume 启动例子: flume-ng agent --conf /etc/flume-ng/conf --conf-file /etc/flume-ng/conf/flume.conf --nam ...

  5. git创建新分支

    1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义.   2.切换本地分支 git ch ...

  6. 吉特日化MES-日化生产称料基本步骤

    在日化行业称料是一个非常重要的环节,整个生产过程中称料所占据的时间也比较长,特别是遇到对料体精度高,量大的情况下称料都比较困难,汇总一下人工称料的基本过程: (1) 称量任务准备:根据生产工单或者生产 ...

  7. .NetCore SignalR 踩坑记

    背景 由于最近公司要做微信小程序聊天,所以.NetFramwork版本的SignalR版本的不能用了.因为小程序里没有windows对象,导致JQuery无法使用.而Signalr的 js客户端是依赖 ...

  8. 如何在C#中使用Dapper(译)

    前言: 对象关系映射(ORM)已经被使用了很长时间,以解决在编程过程中对象模型与数据模型在关系数据库中不匹配的问题. Dapper是由Stack OverFlow团队开发的开源的,轻量级的ORM.相比 ...

  9. vue2.0之axios使用详解

    axios 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 htt ...

  10. 软工+C(8): 提问与回复

    // 上一篇:野生程序员 // 下一篇:助教指南 在线上博客教学里引入了第三方助教,助教在每次作业期间尽力完成"消灭零点评"的目标.然而紧接而来的问题是:学生对博客作业点评的回复率 ...