0
点赞
收藏
分享

微信扫一扫

Spring - Bean管理之配置(XML)


Spring - Bean管理之配置(XML)_配置

一、作用域

Spring - Bean管理之配置(XML)_配置_02

Ps:在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。

package com.imooc.ioc.demo3;

/**
* Created by jt on 2017/10/9.
*/
public class Person {

}
<!-- <bean id="person" class="com.imooc.ioc.demo3.Person" /> -->

<bean id="person" class="com.imooc.ioc.demo3.Person" scope="prototype"/>
package com.imooc.ioc.demo3;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Bean的作用范围的测试
*/
public class SpringDemo3 {
@Test
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person persion1 = (Person)applicationContext.getBean("person");
Person persion2 = (Person)applicationContext.getBean("person");

System.out.println(persion1);
System.out.println(persion2);
}
}

Ps:注释版:输出的hashCode是一样的;未注释版:输出的hashCode是不一样的。


举报

相关推荐

0 条评论