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 ...
随机推荐
- 怎么退出jQuery的each函数
返回 'false' 将停止循环 (就像在普通的循环中使用 'break').返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue'). 以下举例如何退出 each 函数和退出 ...
- elasticsearch 5.x 系列之二 线程池的设置
1,概述 每个Elasticsearch节点内部都维护着多个线程池,如index.search.get.bulk等,用户可以修改线程池的类型和大小,以及其他的比如reflesh, flush,warm ...
- [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)
Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...
- [回文树][BZOJ2160][国家集训队]拉拉队排练
题面 Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮 ...
- scala高级特性-01
目标一:深入理解高阶函数 高阶函数 1.1概念 Scala混合了面向对象和函数式的特性, 我们通常将可以做为参数传递到方法中的表达式叫做函数. 在函数式编程语言中,函数是“头等公民”, 高阶函数包含: ...
- PHP.16-PDO
PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库的特性作为标准扩展功能. 注意利用 PDO 扩展自身并不能实现任何数 ...
- error LNK2001: unresolved external symbol @__security_check_cookie
Q:VS2005编译的静态库, 在vc++6.0中连接出现错误 error LNK2001: unresolved external symbol @__security_check_cookie@l ...
- centos使用--supervisor使用
目录 1 下载程序并安装 2 编辑配置文件 3 supervisor的使用 4 配置文件详细解析 参考资料 supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变 ...
- react基本知识点合集
妹子UI里面有React的相关组件与用法:http://amazeui.org/react/components React官方网站:https://facebook.github.io/react/ ...
- Python学习4,字符串
字符串这个东西主要靠记,多打打就好了. _author_ = "Happyboy" name = "my \tname is happyboy and i am 66 y ...