bean工廠和ApplicationContext

來源:互聯網
上載者:User

標籤:org   getbean   value   private   pos   bsp   white   cto   work   

擷取bean的兩種方法1. 從applicationcontext 應用上下文容器中擷取2. 從bean 工廠擷取 bean 的區別 使用ApplicationContext 擷取bean的例子 我們定義一個Student 類,然後讓spring去調用它Student.javapackage com.getBean; /*** Created by admin on 2017/12/9.*/public class Student {private String name;private int age;private int id; public String getName() {return name;} public void setName(String name) {this.name = name;System.out.print(name);} public int getAge() {return age;} public void setAge(int age) {this.age = age;} public int getId() {return id;} public void setId(int id) {this.id = id;}} // 把Student 類加入到spring設定檔,<bean id="student" class="com.getBean.Student"><property name="name"><value>胖子喲</value></property><property name="age"><value>1</value></property><property name="id"><value>1</value></property></bean> // 通過ApplicationContext 調用beanpackage com.getBean;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.beans.factory.BeanFactory;import org.springframework.core.io.ClassPathResource; /*** Created by admin on 2017/12/9.* 介紹兩種擷取bean的方式 與其他們的不同* 1. 從ApplicationContext 容器上下文擷取bean* 2. 從BeanFactory 擷取bean*/public class runGetBean {public static void main(String[] args){// 1、通過ApplicationContext擷取bean// 當我們調用 new ClassPathXmlApplicationContext 則spring設定檔中的bean 就會被執行個體化 該bean的scope 是singleton 單一實例的ApplicationContext app = new ClassPathXmlApplicationContext("com/getBean/beans.xml");app.getBean("student"); }} 這種方式擷取bean 則在建立 ApplicationContext 執行個體時會自動執行個體化設定檔中的所有bean 2. 使用bean 工廠來擷取bean package com.getBean;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.beans.factory.BeanFactory;import org.springframework.core.io.ClassPathResource; /*** Created by admin on 2017/12/9.* 介紹兩種擷取bean的方式 與其他們的不同* 1. 從ApplicationContext 容器上下文擷取bean* 2. 從BeanFactory 擷取bean*/public class runGetBean {public static void main(String[] args){// 1、通過ApplicationContext擷取bean// 當我們調用 new ClassPathXmlApplicationContext 則spring設定檔中的bean 就會被執行個體化 該bean的scope 是single 單一實例的// ApplicationContext app = new ClassPathXmlApplicationContext("com/getBean/beans.xml");// app.getBean("student");// 通過bean 工廠來擷取beanBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/getBean/beans.xml"));factory.getBean("student");}} 使用bean工廠時則是只有你調用了getBean 方法時,bean執行個體才會被建立 在大部分的項目開發中都是使用ApplicationContext方式擷取bean, 因為它可以提前載入,速度較快而在行動裝置中則多數使用bean工廠,因為其節約記憶體 總結: 如果使用ApplicationContext 則配置的bean 如果是singleton 不管你是否使用,都會被執行個體化,好處是可以積極式載入,壞處是浪費記憶體如果是使用BeanFactory 則配置的bean 不會被馬上執行個體化,當你使用的時候才會被執行個體化,好處是節約記憶體一般如果沒有特殊要求,則應該使用ApplicationContext    

bean工廠和ApplicationContext

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.