spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。
spring 中scope的值有四个:分别是:singleton、prototype、session、request。其中session和request是在web应用中的。
下面证明当scope为prototype时每次创建的对象是不同的。
示例代码如下:
package com.advancedWiring.ambiguityIniAutowiring2; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Student implements BeanNameAware{
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public void setBeanName(String name) {
System.out.println("the Student bean name is :"+name);
}
}
package com.advancedWiring.ambiguityIniAutowiring2; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Configuration
@ComponentScan
public class StudentConfig { }
package com.advancedWiring.ambiguityIniAutowiring2; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by ${秦林森} on 2017/6/9.
*/
public class Test {
public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(StudentConfig.class);
Student student = ac.getBean(Student.class);
Student student2 = ac.getBean(Student.class);
System.out.println(student==student2);
}
}/**output:
false/
上面的测试类的运行结果是false,就说明了在scope为prototype时,spring每次创建的bean都是一个全新的对象。
spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。的更多相关文章
- spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。
在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...
- spring in action 学习笔记十四:用纯注解的方式实现spring mvc
在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- Spring in Action 学习笔记二-DI
装配bean 2015年10月9日 9:49 Sprng中,对象无需自己负责查找或创建其关联的其他对象.相关,容器负责吧需要相互协作的对象引用赋予各个对象. 创建应用对象之间协 ...
- Spring in Action学习笔记(1)
Spring基础 IoC 控制反转, 也称为DI-依赖注入 一.装配bean 推荐顺序:自动装配 -> JavaConfig装配 -> XML装配 1. 自动装配 @Component 注 ...
- Spring in Action 学习笔记三-AOP
面向切面的Spring 2015年10月9日 11:30 屏幕剪辑的捕获时间: 2015-10-9 14:30 屏幕剪辑的捕获时间: 2015-10-9 ...
- Spring in Action 学习笔记一
Spring 核心 Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...
- spring in action学习笔记十六:配置数据源的几种方式
第一种方式:JNDI的方式. 用xml配置的方式的代码如下: 1 <jee:jndi-lookup jndi-name="/jdbc/spittrDS" resource-r ...
- spring in action学习笔记七:@Conditional注解的用法
@Profile注解是@Conditional注解的一个例子.即@Profile也是用@Conditional注解来实现的. 必须让条件实现Condition这个接口. 下面的案例讲如果环境中有mag ...
随机推荐
- 边缘检测 opencv
本次实验使用了两种方法进行了边缘检测,分别使用到了opencv中的两个API函数为Canny()和Sobel()函数.实验后加了Scharr滤波器,它其实是基于Sobel()函数的. 这三个API中的 ...
- 从Wireshark看TCP连接的建立与关闭
TCP是一种面向连接.可靠的协议.TCP连接的建立与断开,都是需要经过通信双方的协商.用一句话概括就是:三次握手say hello(建立连接):四次握手say goodbye(断开连接).要了解TCP ...
- P2680 运输计划(二分+树上差分)
P2680 运输计划 链接 分析: 二分+树上差分. 首先可以二分一个答案,那么所有比这个答案大的路径,都需要减去些东西才可以满足这个答案. 那么减去的这条边一定在所有的路径的交集上. 那么如果求快速 ...
- 网页图片很多时,加载完后再加载图片(defer:延迟加载)
图片影响页面加载速度,可以先加载完页面,再去加载图片. defer:告诉浏览器,这里面的js代码不影响网页脚本解析,可以解析完html脚本再执行这段js代码(个人理解). 网页代码:<img s ...
- 【Lowest Common Ancestor of a Binary Tree】cpp
题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...
- java中继承的概念
继承是类的三大特性之一,是java中实现代码重用的重要手段之一. java中只支持单继承,即每个类只能有一个父类. 继承表达的是is a的关系,或者说一种特殊和一般的关系. ...
- 自动化测试(三)如何用python写个双色球
写一个程序,输入N就产生N条双色球号码 红球 6 01-33 蓝球 1 01-16 产生的双色球号码不能重复,写到一个文件里面,每一行是一条 红球: 01 03 05 07 08 ...
- Flask 教程精简版之一(系列片)
Flask 教程精简版之一(系列片) 现在连教程都有精简版 准备 1.要学会 Flask 之前必须掌握 Python 基本使用. 2.会使用简单的 HTML 效果更加 3.若想练气功必须先自暴自弃 简 ...
- 1、shader简介、渲染管线
vs对于shader的插件:http://blog.shuiguzi.com/shaderlabvs-release-page.html 计算机有一块重要的组成部分,就是“显卡”,大家玩游戏的话,肯定 ...
- [转]LVS+Keepalived负载均衡配置
简介 来源:https://www.cnblogs.com/MacoLee/p/5858995.html lvs一般是和keepalived一起组合使用的,虽然也可以单独使用lvs,但配置比较繁琐,且 ...