欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Bean實(shí)例化實(shí)現(xiàn)過程解析

 更新時(shí)間:2020年02月07日 09:19:46   作者:WODioe  
這篇文章主要介紹了Spring Bean實(shí)例化實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring Bean實(shí)例化實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Bean的實(shí)例化

1.構(gòu)造器實(shí)例化:Spring容器通過Bean對應(yīng)類中默認(rèn)的無參構(gòu)造方法來實(shí)例化Bean

package com.itheima.instance.constructor;
 
public class Bean1 {
 
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean1" class="com.itheima.instance.constructor.Bean1"></bean>
</beans>

在beans1.xml文件中,定義了一個(gè)id為bean1的Bean,并通過class屬性指定其對應(yīng)的實(shí)現(xiàn)類Bean1

package com.itheima.instance.constructor;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceTest1 {
  public static void main(String[] args) {
    //定義配置文件路徑
    String xmlPath = "com/itheima/instance/constructor/beans1.xml";
    //ApplicationContext在加載配置文件時(shí),對Bean進(jìn)行實(shí)例化
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    Bean1 bean = (Bean1) applicationContext.getBean("bean1");
    System.out.println(bean);
  }
}

在InstanceTest1類中,首先定義了配置文件的路徑,然后Spring容器ApplicationContext會加載配置文件。在加載時(shí),Spring容器會通過id為bean1的實(shí)現(xiàn)類Bean1中默認(rèn)的無參構(gòu)造方法對Bean進(jìn)行實(shí)例化。

2. 靜態(tài)工廠方法實(shí)例化

package com.itheima.instance.static_factory;
 
public class Bean2 {
 
}
package com.itheima.instance.static_factory;
 
public class MyBean2Factory {
  //使用自己的方法創(chuàng)建Bean2實(shí)例
  public static Bean2 createBean(){
    return new Bean2();
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean"></bean>
</beans>

定義id為bean2的Bean,通過class屬性指定其對應(yīng)的工廠實(shí)現(xiàn)類(MyBean2Factory.java),需要增加factory-method屬性來告訴Spring容器其方法名稱為createBean。

package com.itheima.instance.static_factory;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceTest2 {
  public static void main(String[] args) {
    //定義配置文件路徑
    String xmlPath = "com/itheima/instance/static_factory/beans2.xml";
    //ApplicationContext在加載配置文件時(shí),對Bean進(jìn)行實(shí)例化
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    System.out.println(applicationContext.getBean("bean2"));
  }
}

3.實(shí)例工廠方式實(shí)例化

package com.itheima.instance.factory;
 
public class Bean3 {
 
}
package com.itheima.instance.factory;
 
public class MyBean3Factory {
  public MyBean3Factory(){
    System.out.println("bean3工廠實(shí)例化中");
  }
  //創(chuàng)建Bean3實(shí)例的方法
  public Bean3 createBean(){
    return new Bean3();
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory"></bean>
<!-- 使用 factory-bean屬性指向配置的實(shí)例工廠
   使用factory-method屬性確定使用工廠中的哪個(gè)方法 -->
   <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"></bean>
</beans>

首先配置了一個(gè)工廠Bean,然后配置了需要實(shí)例化的Bean。在id為bean3的Bean中,使用factory-bean屬性指向配置的實(shí)例工廠,使用factory-method屬性來確定使用工廠中的createBean()方法

package com.itheima.instance.factory;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceTest3 {
  public static void main(String[] args) {
    //指定配置文件路徑
    String xmlPath = "com/itheima/instance/factory/beans3.xml";
    //ApplicationContext加載配置文件時(shí),對Bean進(jìn)行實(shí)例化
    @SuppressWarnings("resource")
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    System.out.println(applicationContext.getBean("bean3"));
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解JAVA中的for-each循環(huán)與迭代

    詳解JAVA中的for-each循環(huán)與迭代

    本文詳解了JAVA中的for-each循環(huán)與迭代,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,也是java中的難點(diǎn)知識,需要的朋友可以參考下 。
    2016-10-10
  • 淺談Servlet 實(shí)現(xiàn)網(wǎng)頁重定向的方法

    淺談Servlet 實(shí)現(xiàn)網(wǎng)頁重定向的方法

    本篇文章主要介紹了Servlet 實(shí)現(xiàn)重定向幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • MyBatis輸入映射和輸出映射實(shí)例詳解

    MyBatis輸入映射和輸出映射實(shí)例詳解

    mapper.xml是我們配置操作數(shù)據(jù)庫的sql語句的地方.這篇文章主要介紹了MyBatis輸入映射和輸出映射實(shí)例詳解,需要的朋友可以參考下
    2017-02-02
  • Java利用redis zset實(shí)現(xiàn)延時(shí)任務(wù)詳解

    Java利用redis zset實(shí)現(xiàn)延時(shí)任務(wù)詳解

    zset作為redis的有序集合數(shù)據(jù)結(jié)構(gòu)存在,排序的依據(jù)就是score。本文就將利用zset score這個(gè)排序的這個(gè)特性,來實(shí)現(xiàn)延時(shí)任務(wù),感興趣的可以了解一下
    2022-08-08
  • 最新評論