首页 技术 正文
技术 2022年11月21日
0 收藏 840 点赞 2,668 浏览 1453 个字

  注解形势:通过@Scope注解控制作用域,默认使用单实例模式,可修改为多实例模式

 /**
* Specifies the name of the scope to use for the annotated component/bean.
* <p>Defaults to an empty string ({@code ""}) which implies
* {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SINGLETON}.
* @since 4.2
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE prototype
* @see ConfigurableBeanFactory#SCOPE_SINGLETON singleton
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION session
* @see #value
*
* prototype:多实例的
* singleton:单实例的(默认)单实例启动在ioc容器启动会调用方法创建对象放入到ioc容器中,以后每次获取直接从ioc容器中拿,都是之前创建好的对象
* request:同一次请求创建一个实例 多实例情况下 ,ioc启动时并不会调用方法创建对象放到容器中,而是没次获取对象时创建对象,每次获取都是不同的实例
* session:同一个session创建一个实例
*/
@Scope(value="prototype") //控制作用域
@Bean("persion")
public Persion persion() {
return new Persion("zhangsan",20);
}

同理xml使用:

 <bean id="persion" class="com.test.bean.Persion" scope="singleton">
<property name="age" value="18"></property>
<property name="name" value="zhangsan"></property>
</bean>

测试类:bean==bean2 相等说明是同一个示例,不相等 说明是多个示例

 @Test
public void test02() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
MainConfig2.class); String[] definitionNames = applicationContext.getBeanDefinitionNames();// 获取spring装配的bean for (String name : definitionNames) {
System.out.println(name);
}
System.out.println("ioc容器创建完成。。。");
Object bean=applicationContext.getBean("persion");
Object bean2=applicationContext.getBean("persion");
System.out.println(bean==bean2); }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918