首页 > 编程语言 > 详细

Spring--基础细节

时间:2017-09-22 20:49:05      阅读:216      评论:0      收藏:0      [点我收藏+]

 

1 id 和 name 的区别

   id:不可重复,不可包含特殊字符

  name:可以重复,可以包含特殊字符

 

2 scope

  singleton:配置单例模式(默认),在容器启动时创建对象,而且只创建一个

  prototype:配置多例模式,在容器启动时不创建对象,当获取对象时才创建

 

3 lazy-init

  true:延迟创建对象,容器启动时不创建,获取时再创建,只适用于单例模式

  false:(默认)

 4 init-method 和 destroy-mothod

  init-method:初始化调用方法

  destroy-mothod:销毁时调用方法

config:

<bean name="p1" id="p2" scope="singleton" lazy-init="true" init-method="init" destroy-method="destroy" class="com.roxy.spring.pojo.Person"></bean>

test:
   
   @Test
    public void testIdAndName() {
        
        //创建容器
        AbstractApplicationContext  context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //查找对象
        Person p1 = (Person)context.getBean("p1");
        Person p2 = (Person)context.getBean("p1");
        
        context.destroy();//执行销毁
//        context.close();//触发销毁
        
    }
   
console:
  
    Creating shared instance of singleton bean ‘p2‘
    Creating instance of bean ‘p2‘
    构造方法被调用
    Eagerly caching bean ‘p2‘ to allow for resolving potential circular references
    Invoking init method  ‘init‘ on bean with name ‘p2
    Person被初始化!
    Invoking destroy method ‘destroy‘ on bean with name ‘p2‘
    Person被销毁!
   

 

Spring--基础细节

原文:http://www.cnblogs.com/roxy/p/7576553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!