溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring中xml配置的示例分析

發(fā)布時(shí)間:2021-12-07 11:40:48 來(lái)源:億速云 閱讀:125 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)Spring中xml配置的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

1、一個(gè)Performer接口:

package springAction;

public interface Performer {

void perform();

}

2、一個(gè)Juggler類(lèi)實(shí)現(xiàn)了Performer接口:

package springAction;

public class Juggler implements Performer{

private int beanBags = 3;

public Juggler(){

}

public Juggler(int beanBags){

this.beanBags = beanBags;

}

public void perform(){

System.out.println("Juggling " + beanBags + " beanbags");

}

}

3、一個(gè) PoeticJuggler類(lèi)繼承了Juggler類(lèi):

package springAction;

public class PoeticJuggler extends Juggler {

private Poem poem;

public PoeticJuggler(Poem poem){

super();

this.poem = poem;

}

public PoeticJuggler(int beanBags, Poem poem){

super(beanBags);

this.poem = poem;

}

public void perform(){

super.perform();

System.out.println("PoeticJugger reciting...");

poem.recite();

}

}

4、一個(gè)Poem接口:

package springAction;

public interface Poem {

void recite();

}

5、一個(gè)單例類(lèi)Stage:

package springAction;

public class Stage {

private Stage(){

}

private static class StageSingletonHolder{

static Stage instance = new Stage();

}

public static Stage getInstance(){

return StageSingletonHolder.instance;

}

}

6、看看xml文件怎么寫(xiě)(springAction.xml):

<?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-4.1.xsd">

<!-- 配置一個(gè)bean -->

<bean id="duke" class="springAction.Juggler" >

<!--給構(gòu)造函數(shù)傳遞參數(shù),沒(méi)有的話則調(diào)用默認(rèn)構(gòu)造方法 -->

<constructor-arg value="15"/>

</bean>

<bean id="sonnet29" class="springAction.Sonnet29"></bean>

<bean id="poeticDuke" class="springAction.PoeticJuggler">

<constructor-arg value="22"></constructor-arg>

<!-- 基本數(shù)據(jù)類(lèi)型參數(shù)用value=字符串(Spring根據(jù)構(gòu)造參數(shù)類(lèi)型自動(dòng)解析字符串),

引用類(lèi)型的參數(shù)用ref= bean id -->

<constructor-arg ref="sonnet29"></constructor-arg>

</bean>

<!-- factory-method通過(guò)工廠方法將單例類(lèi)配置為bean,因?yàn)镾tage沒(méi)有構(gòu)造函數(shù) -->

<bean id="theStage" class="springAction.Stage" factory-method="getInstance">

</bean>

</beans>

加個(gè)main函數(shù)運(yùn)行一下,看看效果:

package springAction;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springActionMain {

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext ctx = new ClassPathXmlApplicationContext(

"springAction/springAction.xml");

try{

Performer performer = (Performer)ctx.getBean("duke");

performer.perform();

performer = (Performer)ctx.getBean("poeticDuke");

performer.perform();

}catch(Exception e){

e.printStackTrace();

}finally{

((ClassPathXmlApplicationContext)ctx).close();

}

}

}

7、bean的作用域

所有的Spring Bean默認(rèn)是單例,當(dāng)容器分配一個(gè)Bean時(shí),它總是返回同一個(gè)實(shí)例。但,spring是靈活的,不需要單例模式時(shí),可以如下配置:
<bean id="ticket" class="省略號(hào)" scope="prototype"/>

8、初始化Bean和銷(xiāo)毀Bean

當(dāng)Spring容器實(shí)例化一個(gè)Bean時(shí)可能需要做一些初始化的工作,當(dāng)Spring銷(xiāo)毀一個(gè)Bean時(shí),可能需要做一些清理工作。Spring支持Bean提供自定義的方法來(lái)做初始化工作和銷(xiāo)毀工作。

9、為Bean注入屬性

一般地,java Bean會(huì)有一些私有屬性,并有一些set和get方法。Spring可以利用set方法為bean的私有屬性注入值。

例子:

package springAction;

public class Instrumentalist implements Performer{

private String song;

private Instrument instrument;

private int age;

public Instrumentalist(){

}

public void perform(){

System.out.println("age = " + age);

System.out.println("Playing "+song);

instrument.play();

}

public String getSong() {

return song;

}

public void setSong(String song) {

this.song = song;

}

public Instrument getInstrument() {

return instrument;

}

public void setInstrument(Instrument instrument) {

this.instrument = instrument;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

xml文件配置:

<bean id="saxophone" class="springAction.Saxophone"/>

<bean id="kenny" class="springAction.Instrumentalist">

<!-- 配置屬性song -->

<property name="song" value="ABC"></property>

<!-- 配置屬性age,雖然此處是字符串“14”,但是Spring會(huì)識(shí)別age的類(lèi)型,然后把字符串“14”轉(zhuǎn)變后賦值給age -->

<property name="age" value="14"></property>

<!-- 配置instrument對(duì)象,用ref的方式賦引用,和構(gòu)造函數(shù)一致 -->

<property name="instrument" ref="saxophone"></property>

</bean>

感謝各位的閱讀!關(guān)于“Spring中xml配置的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI