Spring--基礎細節

來源:互聯網
上載者:User

標籤:enc   一個   lazy   str   ini   tin   cat   構造方法   建立對象   

 

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--基礎細節

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.