为什么80%的码农都做不了架构师?>>>
Spring 复杂对象的注入
复杂对象主要分为有两类:Collection类(包括数组和list)和Map类。
List的注入形式:(数组形式其实是差不多的)
public class OneManBand implements Performer {public OneManBand() {}public void perform() throws PerformanceException {for (Instrument instrument : instruments) {instrument.play();}}private Collection instruments;private Collection names;public Collection getNames() {return names;}public void setNames(Collection names) {this.names = names;}public void setInstruments(Collection instruments) {this.instruments = instruments; // }public static void main(String args[]){ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml");OneManBand one=(OneManBand)ac.getBean("oneManBand");System.out.println(one.getNames());}
}
1nihao
map 注入:
public class OneManBand implements Performer {private Collection instruments;private Map prices;public Map getPrices() {return prices;}public void setPrices(Map prices) {this.prices = prices;}public static void main(String args[]){ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml");OneManBand one=(OneManBand)ac.getBean("oneManBand");System.out.println(one.getPrices());}
}
xml的正确写法:可以装配空值这个很关键。