学习 Spring (三) Bean 的配置项 & 作用域
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 的配置项 & 作用域的更多相关文章
- Spring三 Bean的三种创建方式
创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...
- Spring的Bean有哪些作用域?
Spring的Bean有以下五种作用域: 1.singleton:SpringIOC容器只会创建该Bean的唯一实例: 2.prototype:每次请求都创建一个实例: 3.requset:每次HTT ...
- 学习Spring(三) 设置Bean作用域
Spring中几种不同的作用域 Singleton 每个Spring IoC容器中创建一个Bean实例 Prototype 每次请求时创建一个新的Bean实例 Request 为 ...
- Spring学习记录(三)---bean自动装配autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...
- Spring(三)Bean继续入门
一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...
- 学习 Spring (四) Bean 的生命周期
Spring入门篇 学习笔记 定义 --> 初始化 --> 使用 --> 销毁 初始化 实现 org.springframework.beans.factory.Initializi ...
- JMeter学习(三)元件的作用域与执行顺序
1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其它元件(conf ...
- JMeter学习(三)元件的作用域与执行顺序(转载)
转载自 http://www.cnblogs.com/yangxia-test 1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它 ...
- 浅析Spring中bean的作用域
一.前言 刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...
随机推荐
- 【转】打包 压缩 命令tar zip
https://www.cnblogs.com/centos2017/p/7896807.html tar语法 #压缩tar -czvf ***.tar.gztar -cjvf ***.tar.bz2 ...
- redis学习(一)——redis介绍及安装
一.redis简介 redis是一个高性能的key-value非关系数据库,它可以存键(key)与5种不同类型的值(value)之间的映射(mapping),支持存储的value类型包括:String ...
- mac nginx 启动 自启动
MacBook-Pro:local shihw$ brew services start nginx ==> Tapping homebrew/services Cloning into '/u ...
- Angularjs 过滤器使用
Filter:格式化数据 // HTML表达式: {{ filter_expression | filter : expression : comparator}} // JS表达式: $filt ...
- arm汇编之 bne与beq
在网上看了一些bne和beq的区别,但是对于初学者来说,容易越看越糊涂,因此简单介绍下: 我们先分析CPSR寄存器的Z标识位: cmp指令可以直接影响CPSR寄存器的Z标识位(条件位),从图中可以看出 ...
- mysql查询表结果排序
要求:将数据表tab1中DeviceName为‘XI003’,'DO107'的数据放在查询结果的前面,同时对其他查询结果进行降序排序 sql语句: SELECT DISTINCT DeviceName ...
- SpringMVC处理跨域请求时的一个注意事项
由于公司对SpingMVC框架里面的东西进行了扩展,在配置SpringMVC时没有使用<mvc:annotation-driven>这个标签.而且是自己手动来配置HandlerMa ...
- Objective-C Programming The Big Nerd Ranch Guide 笔记 19-37
Properties are either atomic or nonatomic, The difference has to do with multithreading. atomic is t ...
- 缓存子系统如何设计(Cachable tag, Memcache/redis support, xml config support, LRU/LFU/本地缓存命中率)
大家对这段代码肯定很熟悉吧: public List<UserInfo> SearchUsers(string userName) { string cacheKey=string.For ...
- disconf原理 “入坑”指南
之前有了解过disconf,也知道它是基于zookeeper来做的,但是对于其运行原理不太了解,趁着周末,debug下源码,也算是不枉费周末大好时光哈 :) .关于这篇文章,笔者主要是参考discon ...