您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Spring中xml配置的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、一個Performer接口:
package springAction;
public interface Performer {
void perform();
}
2、一個Juggler類實現了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、一個 PoeticJuggler類繼承了Juggler類:
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、一個Poem接口:
package springAction;
public interface Poem {
void recite();
}
5、一個單例類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文件怎么寫(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">
<!-- 配置一個bean -->
<bean id="duke" class="springAction.Juggler" >
<!--給構造函數傳遞參數,沒有的話則調用默認構造方法 -->
<constructor-arg value="15"/>
</bean>
<bean id="sonnet29" class="springAction.Sonnet29"></bean>
<bean id="poeticDuke" class="springAction.PoeticJuggler">
<constructor-arg value="22"></constructor-arg>
<!-- 基本數據類型參數用value=字符串(Spring根據構造參數類型自動解析字符串),
引用類型的參數用ref= bean id -->
<constructor-arg ref="sonnet29"></constructor-arg>
</bean>
<!-- factory-method通過工廠方法將單例類配置為bean,因為Stage沒有構造函數 -->
<bean id="theStage" class="springAction.Stage" factory-method="getInstance">
</bean>
</beans>
加個main函數運行一下,看看效果:
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默認是單例,當容器分配一個Bean時,它總是返回同一個實例。但,spring是靈活的,不需要單例模式時,可以如下配置:
<bean id="ticket" class="省略號" scope="prototype"/>
8、初始化Bean和銷毀Bean
當Spring容器實例化一個Bean時可能需要做一些初始化的工作,當Spring銷毀一個Bean時,可能需要做一些清理工作。Spring支持Bean提供自定義的方法來做初始化工作和銷毀工作。
9、為Bean注入屬性
一般地,java Bean會有一些私有屬性,并有一些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會識別age的類型,然后把字符串“14”轉變后賦值給age -->
<property name="age" value="14"></property>
<!-- 配置instrument對象,用ref的方式賦引用,和構造函數一致 -->
<property name="instrument" ref="saxophone"></property>
</bean>
感謝各位的閱讀!關于“Spring中xml配置的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。