一个完成的spring 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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="duke" class="com.springinaction.springidol.Juggler" > 
        
        <constructor-arg value="15">constructor-arg>
    bean>
    
    <bean id="sonnect29" class="com.springinaction.springidol.Sonnet29">bean>
    
    <bean id="poeticPoem" class="com.springinaction.springidol.PoeticJuggler">
        <constructor-arg value="15">constructor-arg>
        <constructor-arg ref="sonnect29">constructor-arg>
    bean>
    
        
    <bean id="Kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells">property>
        <property name="age" value="37">property>
        
        
        
        
        <property name="instrument">
            <bean class="com.springinaction.springidol.piano">bean>
        property>
    bean>
    
    <bean id="saxphone" class="com.springinaction.springidol.saxphone">bean>
    <bean id="piano" class="com.springinaction.springidol.piano">bean>
    
    
    <bean id="Kenny2" class="com.springinaction.springidol.Instrumentalist" 
        p:song="Lemon Tree" p:age="30" p:instrument-ref="saxphone"    >
    bean>
    
    
    <bean id="hank" class="com.springinaction.springidol.OneManBand">
        <property name="instruments">
            <list>
                <ref bean="piano" />
                <ref bean="saxphone" />
            list>
        property>
        <property name="instruments2">
            <map>
                <entry key="piano" value-ref="piano">entry>
                <entry key="saxphone" value-ref="saxphone">entry>
            map>
        property>
    bean>
    
    
    <bean id="hank2" class="com.springinaction.springidol.OneManBand">
        <property name="instruments">
            <props>
                
                <prop key="piano">la la laprop> 
                <prop key="saxphone">ta ta taprop>
            props>
        property>
    bean>
    
    
    
beans>

 

package com.springinaction.springidol;

/**
 * 
 * @author Alan.chen
 * @定义表演者
 * @类型:接口
 */

public interface Proformer {
    void perform() throws Exception;
}
package com.springinaction.springidol;

public interface Instrument {
    public void play();
}
package com.springinaction.springidol;

public class saxphone implements Instrument {

    public saxphone() {
    }
    
    @Override
    public void play() {
        System.out.println("toot toot toot");
    }

}
package com.springinaction.springidol;

public class piano implements Instrument {

    public piano(){
        
    }
    
    @Override
    public void play() {
        System.out.println("弹得一手好琴。。。");
    }

}
package com.springinaction.springidol;

/**
 * 
 * @author Alan.chen
 * @诗人实现类
 */
public class Sonnet29 implements Poem {

    private static String[] LINES = {
        "When in disgrace with fortune and men's eyes,",
        "I all alone beweep my outcast state,",
        "And trouble deaf heaven with my bootless cries,",
        "And look upon myself, and curse my fate,",
        "Wishing me like to one more rich in hope,",
        "Featured like him, like him with friends posses'd,",
        "Desiring this man's art, and that man's scope,",
        "With what I most enjoy contented least;",
        "Yet in these thoughts myself almost despising,",
        "Haply I think on thee,-and then my state",
        "(Like to the lark at break of day arising From sullen earth) sings hymns at heaven's gate;",
        "For thy sweet love remember'd , such wealth brings,",
        "That then I scorn to change my state with kings.!"
    };
    
    Sonnet29(){
        
    }
    
    @Override
    public void recite() {
        for(int i=0;i)
            System.out.println(LINES[i]);
    }

}

 

package com.springinaction.springidol;

/**
 * 
 * @author Alan.chen
 * @注入Bean属性示例,用setXXX(),getXXX()接收
 * @描述:Instrumentalist,演奏家
 */
public class Instrumentalist implements Proformer {
    
    public Instrumentalist() {
    }
    
    private int age;
    
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public void perform() throws Exception {
        System.out.println("playing "+song+" : ");
        instrument.play();
    }
    
    private String song;
    public void setSong(String song){
        this.song = song;
    }
    
    public String getSong(){
        return song;
    }
    
    public String screamSong(){
        return song;
    }
    
    private Instrument instrument;
    public void setInstrument(Instrument instrument){
        this.instrument = instrument;
    }

}

 

package com.springinaction.springidol;

import java.util.Collection;
import java.util.Map;

public class OneManBand implements Proformer {
    
    public OneManBand() {
    }

    @Override
    public void perform() throws Exception {
        for(Instrument instrument:instruments)
            instrument.play();
        for(String key:instruments2.keySet()){
            System.out.println(key + " : ");
            Instrument instrument = instruments2.get(key);
            instrument.play();
        }            
    }
    
    //当被注入的对象时集合时
    private Collection instruments;

    public Collection getInstruments() {
        return instruments;
    }

    public void setInstruments(Collection instruments) {
        this.instruments = instruments;
    }
    
    
    //当被注入的对象是map时
    private Map instruments2;

    public Map getInstruments2() {
        return instruments2;
    }

    public void setInstruments2(Map instruments2) {
        this.instruments2 = instruments2;
    }
    
    //当被注入的map的key和value都是String时,可以用properties来代替map
    //private Properties instruments3;
//    public void setInstruments3(Properties instruments2) {
//        this.instruments2 = instruments3;
//    }
    
    
}
package com.springinaction.springidol;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "com/springinaction/springidol/spring-idol.xml");
//        Proformer performer = (Proformer)ctx.getBean("duke");
//        Proformer performer = (Proformer)ctx.getBean("poeticPoem");
//        Proformer performer = (Proformer)ctx.getBean("Kenny2");
        Proformer performer = (Proformer)ctx.getBean("hank");
        try {
            performer.perform();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(一个完成的spring xml配置文件)