Spring Bean實例化實現(xiàn)過程解析
這篇文章主要介紹了Spring Bean實例化實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
Bean的實例化
1.構(gòu)造器實例化:Spring容器通過Bean對應(yīng)類中默認(rèn)的無參構(gòu)造方法來實例化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文件中,定義了一個id為bean1的Bean,并通過class屬性指定其對應(yīng)的實現(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在加載配置文件時,對Bean進(jìn)行實例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Bean1 bean = (Bean1) applicationContext.getBean("bean1");
System.out.println(bean);
}
}
在InstanceTest1類中,首先定義了配置文件的路徑,然后Spring容器ApplicationContext會加載配置文件。在加載時,Spring容器會通過id為bean1的實現(xiàn)類Bean1中默認(rèn)的無參構(gòu)造方法對Bean進(jìn)行實例化。

2. 靜態(tài)工廠方法實例化
package com.itheima.instance.static_factory;
public class Bean2 {
}
package com.itheima.instance.static_factory;
public class MyBean2Factory {
//使用自己的方法創(chuàng)建Bean2實例
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)的工廠實現(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在加載配置文件時,對Bean進(jìn)行實例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
System.out.println(applicationContext.getBean("bean2"));
}
}

3.實例工廠方式實例化
package com.itheima.instance.factory;
public class Bean3 {
}
package com.itheima.instance.factory;
public class MyBean3Factory {
public MyBean3Factory(){
System.out.println("bean3工廠實例化中");
}
//創(chuàng)建Bean3實例的方法
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屬性指向配置的實例工廠
使用factory-method屬性確定使用工廠中的哪個方法 -->
<bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"></bean>
</beans>
首先配置了一個工廠Bean,然后配置了需要實例化的Bean。在id為bean3的Bean中,使用factory-bean屬性指向配置的實例工廠,使用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加載配置文件時,對Bean進(jìn)行實例化
@SuppressWarnings("resource")
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
System.out.println(applicationContext.getBean("bean3"));
}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Springboot的高校社團(tuán)管理系統(tǒng)的設(shè)計與實現(xiàn)
本文將基于Springboot+Mybatis開發(fā)實現(xiàn)一個高校社團(tuán)管理系統(tǒng),系統(tǒng)包含三個角色:管理員、團(tuán)長、會員。文中采用的技術(shù)有Springboot、Mybatis、Jquery、AjAX、JSP等,感興趣的可以了解一下2022-07-07
SpringBoot 將多個Excel打包下載的實現(xiàn)示例
本文主要介紹了SpringBoot 將多個Excel打包下載的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
java數(shù)據(jù)結(jié)構(gòu)與算法之插入排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之插入排序,結(jié)合實例形式分析了java插入排序的概念、分類、原理、實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下2017-05-05

