Spring课程 Spring入门篇 3-1 Spring bean装配(上)之bean的配置项及作用域
本节主要讲了四大块
1 bean的作用域
2 bean作用域代码演练
3 单例 多例应用场景
4 bean的配置项(不重要)
1 bean的作用域
1.1 singleton :单例
1.2 prototype :多例
不重要:
1.3 request :每次http请求创建一个实例且仅在当前request有效
1.4 session :每次http请求创建,当前session内有效
1.5 global session:基于portlet的web有效(portlet中定义了global session),如果在web中,同session
2 bean作用域代码演练
2.1 singleton作用域实例
实体类:
package com.imooc.bean;
public class BeanScope {
/**
* say方法测试 哈希码值,判断用的是不是同一块 内存
*/
public void say(){
System.out.println("该对象的哈希码为"+this.hashCode());
}
}
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"
default-init-method="init" default-destroy-method="destroy"> <bean id="beanScope" class="com.imooc.bean.BeanScope" scope="singleton"></bean>
<!--<bean id="beanScope" class="com.imooc.bean.BeanScope" scope="prototype"></bean> --> </beans>
测试类:
package com.imooc.test.ioc.interfaces; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.cglib.core.Block; import com.imooc.bean.BeanScope;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase{ public TestBeanScope() {
super("classpath*:spring-beanScope.xml");
} @Test
/**
* 测试单例模式 Signton
*/
public void testSay(){
try {
BeanScope bScope = super.getbean("beanScope");
bScope.say(); BeanScope bScope2 = super.getbean("beanScope");
bScope2.say();
} catch (Exception e) {
// TODO: handle exception
}
} }
2.2 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"
default-init-method="init" default-destroy-method="destroy"> <!-- <bean id="beanScope" class="com.imooc.bean.BeanScope" scope="singleton"></bean> -->
<bean id="beanScope" class="com.imooc.bean.BeanScope" scope="prototype"></bean> </beans>
测试类(同上)
3 单例 多例应用场景
经上边应用可知:
作用域为singleton ,两次打印哈希码值相同,即,只在初始化的时候创建一个实例(即加载上下文context.xml的时候);
作用域为prototype,两次打印哈希码值不同,则每次访问都会创建一个实例。
应用场景:
如果需要回收重要资源(如数据库连接等)应该为配置为singleton,因为单个库的数据库连接只有一个。
如果是有状态的bean应配置为prototype,如生效和失效的客户。
4 bean的配置项(不重要)
4.1 Id
4.2 Class
4.3 Scope
4.4 Properties
4.5 Constructor arguments
4.6 lazy-initialization mode
Spring课程 Spring入门篇 3-1 Spring bean装配(上)之bean的配置项及作用域的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring Cloud Alibaba入门篇
学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot(一):入门篇+前端访问后端
转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...
- (转)Spring boot(一):入门篇
https://www.cnblogs.com/ityouknow/p/5662753.html#!comments 构建微服务:Spring boot 入门篇 什么是Spring Boot Spri ...
- Spring Boot(一):入门篇
Spring Boot(一):入门篇 一.Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. 该框架 ...
- Spring Cloud Alibaba(1)---入门篇
Spring Cloud Alibaba入门篇 有关微服务的一些概念的东西我这里就不再阐述了,因为之前在写Spring Cloud系列的时候都有详细写过. 具体地址: Spring Cloud系列博客 ...
随机推荐
- 从零开始安装 Ambari (4) -- 通过 Ambari 部署 hadoop 集群
1. 打开 http://192.168.242.181:8080 登陆的用户名/密码是 : admin/admin 2. 点击 “LAUNCH INSTALL WIZARD”,开始创建一个集群 3 ...
- c语言参考书籍
很惭愧没能把c++学的很好,毕竟离开始工作只有2年时间,对自己要求不要过高,慢慢来吧.话说知道自己的不足,以后要更加抓紧了!fighting~ 现在计划着把c语言给学习一下了,当然这次指的是深入地学习 ...
- Cardinality (基数)
名词 Cardinality: 优化器在计算成本的时候,需要从统计信息中取得数据,然后去估计每一步操作所涉及的行数,叫做Cardinality. 比如,一张表T有1000行数据,列COL1 ...
- Div的移动
//JQuery 拖拽本体DIV,把以下代码全部复制即可 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- P3379 【模板】最近公共祖先(LCA)(LCT)
\(\color{#0066ff}{ 题目描述 }\) 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. \(\color{#0066ff}{输入格式}\) 第一行包含三个正整数N.M. ...
- UVa 11292 勇者斗恶龙(The Dragon of Loowater)
首先先看一下这道题的英文原版... 好吧,没看懂... 大体意思就是: 有一条n个头的恶龙,现在有m个骑士可以雇佣去杀死他,一个能力值为x的勇士可以砍掉直径不超过x的头,而且需要支付x个金币.如何雇佣 ...
- xshell一直连接中断 守护进程
last指令 重新登录使用last指令查看登录情况 pts的理解 who:查看目前有谁在线 pts是所谓的伪终端或虚拟终端,具体表现就是你打开一个终端,这个终端就叫pts/0,如果你再打开一个终端,这 ...
- 3. mybatis # 与 $ 的区别
mybatis # 与 $ 的区别 1.# % 号必须写在test中 应用场景:模糊查询 配置文档mapper.xml <select id="selectBlogByTitle&qu ...
- pytorch 加载mnist数据集报错not gzip file
利用pytorch加载mnist数据集的代码如下 import torchvision import torchvision.transforms as transforms from torch.u ...
- POJ1052 Plato's Blocks
题目来源:http://poj.org/problem?id=1052 题目大意: 把1*1*1的小立方体通过粘接相邻面组成大的立方体的形状.如下图所示: 一层一层地堆叠,立方体从三个方向的投影会分别 ...