课程链接:

本节主要讲了四大块

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的配置项及作用域的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  4. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  5. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  6. spring boot(一):入门篇

    构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  7. Spring Boot(一):入门篇+前端访问后端

    转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...

  8. (转)Spring boot(一):入门篇

    https://www.cnblogs.com/ityouknow/p/5662753.html#!comments 构建微服务:Spring boot 入门篇 什么是Spring Boot Spri ...

  9. Spring Boot(一):入门篇

    Spring Boot(一):入门篇 一.Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. 该框架 ...

  10. Spring Cloud Alibaba(1)---入门篇

    Spring Cloud Alibaba入门篇 有关微服务的一些概念的东西我这里就不再阐述了,因为之前在写Spring Cloud系列的时候都有详细写过. 具体地址: Spring Cloud系列博客 ...

随机推荐

  1. 浅识J2EE十三个规范

    前言 没有规矩不成方圆,学习J2EE,先来明白都有什么规范. 内容 1.JDBC(Java Database Connectivity)java数据库连接 a)为java开发人员提供了一个行业标准AP ...

  2. 数据结构19: BF算法(普通模式匹配算法)

    判断两个串之间是否存在主串与子串的关系,这个过程称为串的模式匹配. 在串的模式匹配过程,子串 T 通常被叫做“模式串”. 普通的模式匹配(“BF”算法) 判断两个串是否存在子串与主串的关系,最直接的算 ...

  3. AtCoder - 2581 树状数组

    You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K. a has N(N+1)⁄2 non ...

  4. PHP中SESSION无法获取问题

    近期在看公司老项目,前台可以正常访问,但是后台却无法登录,一直报请求超时,请重新登录!进入服务后发现是有一处SESSION的值无法获取,这就让人很郁闷了,通常SESSION无法使用都是因为没有使用se ...

  5. 【Groovy】Spock with Maven

    已经在项目里使用Groovy/Spock做测试框架了,感觉和Maven结合在一起还是挺好用的. 在Maven的pom.xml里引入他们还是挺方便的,第一先要在dependency 里引入 <de ...

  6. xampp 在 windows下 配置 fcgi... 和 opcache 等 优化操作

    首先 修改 xampp 的 httpd.conf:(底部加入.方便修改) #fast-cgi 模式 LoadModule fcgid_module modules/mod_fcgid.so <I ...

  7. NowCoder数列(矩阵快速幂变式)

    时间限制 3000 ms 内存限制 32768 KB 代码长度限制 100 KB 题目描述 NowCoder最近在研究一个数列: * F(0) = 7 * F(1) = 11 * F(n) = F(n ...

  8. Dijkstra算法图文详解

    Dijkstra算法 Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不 ...

  9. POJ 3067 Japan (树状数组 && 控制变量)

    题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...

  10. codeforces 620D Professor GukiZ and Two Arrays

    #include <bits/stdc++.h> using namespace std; + ; const long long inf = 1e18; int n, m; long l ...