学习 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作用域,这篇博客就来简单介绍一下这七种作用域 ...
随机推荐
- kafka+storm结合存在的一些问题与解决方法
在配置kafka和storm的时候, 经常的会出现一些问题, 主要在以下几个: 1. 打jar包上去storm集群的时候会出现jar包冲突,类似于log4j或者sf4j的报错信息. 2. kafka ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简述及技术选型介绍
作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载. 萌芽阶段 很久之前就开始打算整理一下自己的技术博客了,由于各种原因(借口 ...
- 01 前言/基础设施 - DevOps之路
01 前言/基础设施 - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi 简介 基础架构采用DevOps设计思想, ...
- python 获取lazada菲律宾站地址库
import urllib3 import requests import ast import time # 因为lazada返回的数据是json类型,通过解码成字符串类型,为了方便数据操作,使用字 ...
- PM2用法简介
简介 PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单.引用 全局安装 sudo npm install pm2@lat ...
- Mysql多实例之mysql服务脚本
1. #init port=3306 mysql_user="root" mysql_pwd="cancer" CmdPath="/applicati ...
- mybatis入门 配置文件解释 及测试
这里介绍一下mybatis 根据mybatis的官网说明,mybatis是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置 ...
- koa generator
Koa (koajs) -- 基于 Node.js 平台的下一代 web 开发框架 | Koajs... Koa 框架教程 koa入门 如何评价 Node.js 的koa框架?
- 了解sso原理
- vue入门(一)
通过JS引用vue就不说了,重点说一下使用npm搭建vue脚手架. (以下是windows系统下的操作,win7+) npm是个命令行工具,在搭建vue脚手架之前首先要安装nodeJS,下面是node ...