Spring基于xml文件配置Bean過程詳解
這篇文章主要介紹了spring基于xml文件配置Bean過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
通過全類名來配置:
class:bean的全類名,通過反射的方式在IOC容器中創(chuàng)建Bean,所以要求bean中必須有一個無參的構(gòu)造器。
<bean id="helloWorld" class="com.gong.spring.beans.HelloWorld">
<property name="name" value="jack"></property>
</bean>
在springIOC容器讀取Bean配置創(chuàng)建Bean的實例之前,需要對容器進行實例化。spring提供了兩種類型的IOC容器實現(xiàn):
Beanfactory:IOC容器的基本實現(xiàn)。
ApplicationContext:提供了更多高級特性,是BeanFactory的子接口。
ApplicationContext主要實現(xiàn)類:
- ClassPathXmlApplicationContext:從類路徑加載配置文件。
- FileSystemXmlApplicationContext:從文件系統(tǒng)中加載配置文件。
- ConfigureableApplicationContext擴展于ApplicationContext,新增兩個方法refresh()和close(),讓ApplicationContext具有啟動、刷新和關(guān)閉上下文的能力。
ApplicaiotnContex在初始化時就上下文時就實例化所有單例的Bean。
WebApplicationContext是專門用于WEB應(yīng)用的,它允許從相對于WEB根目錄的路徑中完成初始化工作。
依賴注入的三種方式
(1)屬性注入:通過setter方法:<property name="name" value="jack"></property>,即在bean中存在setter方法。
(2)構(gòu)造器注入:<constructor-arg value="" index="0" type=""></constructor-arg>,根據(jù)構(gòu)造方法中初始化的參數(shù)進行一一設(shè)置,同時,可以根據(jù)參數(shù)的順序index,參數(shù)的類型type來區(qū)分重載的構(gòu)造器。
(3)工廠方法注入(很少使用,不推薦)
<bean id="student" class="com.gong.spring.beans.Student"> //第一種方式注入屬性值
<constructor-arg value="tom" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg value="12" index="1" type="int"></constructor-arg> //第二種方式注入屬性值
<constructor-arg index="2" type="double">
<value>99.00</value>
</constructor-arg>
</bean>
package com.gong.spring.beans;
public class Student {
private String name;
private int age;
private double score;
public Student(String name,int age,double score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
public static void main(String[] args) {
//1.創(chuàng)建spring的IOC容器對象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.從容器中獲取Bean實例
Student student = (Student) ctx.getBean("student");
System.out.println(student.toString());
}
輸出:
當屬性值有特殊符號時,要用以下方式:
<constructor-arg index="0" type="java.lang.String">
<value><![CDATA[<tom>]]></value>
</constructor-arg>
用<![CDATA[屬性值]]>。
欄 目:Java
本文地址:http://www.jygsgssxh.com/a1/Java/8724.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實例
- 01-10基于Java驗證jwt token代碼實例
- 01-10springboot實現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時隊列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10SPRING BOOT啟動命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過程代碼實例


閱讀排行
本欄相關(guān)
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10利用Java實現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當前時間
- 01-10基于Java驗證jwt token代碼實例
- 01-10Java動態(tài)顯示當前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)


