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. RMQ问题--范围最小值问题

    范围最小值问题(Range Minium Query,RMQ)---RMQ问题 一.一维问题 给出一个n个元素的数组A1,A2,...,An, 设计一个数据结构, 支持查询操作Query(L,R):计 ...

  2. Python脱产8期 Day11 2019/4/25

    一 字符串比较 1.字符串比较:字符串对应的ascii进行比较 2.多个字符的字符串进行比较:从前往后逐个字符进行比较,一旦哪个位置的字符出现了大小关系就结束比较. 二 形参与实参 1.参数介绍: 函 ...

  3. Python脱产8期 Day09 2019/4/23

    内存管理 一.引用计数:垃圾回收机制的依据 1.变量的值被引用,该值的引用计数 +12.变量的值被解绑,该值的引用计数 -13.引用计数为0时就会被垃圾回收机制回收 二.引用计数会出现循环引用问题:相 ...

  4. input表单提交完毕,返回重新填入有黄色背景,和 历史记录 清除

    <input autocomplete="value"> // 添加这个属性,可以解决然后添加一个css input:-webkit-autofill {box-sha ...

  5. 唯一正确的修改Jupyter Notebook默认路径的方法

    唯一正确修改Jupyter Notebook的默认路径 1.按照网上的方法,先修改了快捷方式的起始位置,发现并不能修改默认路径. 2.后来发现“目标”中后面有个参数%USERPROFILE%,它代表的 ...

  6. C#使用Json.Net遍历Json

    StringBuilder builder=new StringBuilder(); builder.AppendLine("{"); builder.AppendLine(&qu ...

  7. JDK命令行(jps、jstat、jinfo、jmap、jhat、jstack、jstatd、hprof)与JConsole

    很多资料在介绍JDK命令行工具时并不是在Java8环境下,因此还在使用过时的永久区系列的参数,给一些读者造成困难. Java8使用Metaspace(元空间)代替永久区,对于64位平台,为了压缩JVM ...

  8. Oracle记录表删除操作简单方法

    最近项目中Oracle库中一个表log_gpackage有数据丢失现象,但因为没有启用归档,所以CDC和Dataguard都无法使用.google一下,最简单的方法,增加触发器处理逻辑: ---创建触 ...

  9. 接口自动化框架(Pytest+request+Allure)

    前言: 接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱. 接口自动化包含2个部分,功能性的接口自动化测试和并发接口自动化测试. 本次文章着重介绍第一种, ...

  10. Mysql权限操作、用户管理、密码操作

    Mysql的权限 mysql中存在4个控制权限的表,分别为user表,db表,tables_priv表,columns_priv表. mysql权限表的验证过程为: 先从user表中的Host,Use ...