List,Set,Map,Properties的Spring注入实例
2007-04-06 10:30
447 查看
定义接口:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public interface Person ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void useAxe();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
package Bean.collections;
public interface Axe {
public String chop();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
定义实现类:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashSet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Properties;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Set;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import Bean.collections.Person;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Chinese implements Person ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private List schools=new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Map scores=new HashMap();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Properties health=new Properties();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Set axes=new HashSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Set getAxes() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return axes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setAxes(Set axes) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.axes = axes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Properties getHealth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return health;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHealth(Properties health) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.health = health;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List getSchools() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return schools;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setSchools(List schools) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.schools = schools;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Map getScores() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return scores;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setScores(Map scores) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.scores = scores;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void useAxe() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(schools);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(scores);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(axes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(health);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class SteelAxe implements Axe ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String chop() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "这是一把铁斧子";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class WoodAxe implements Axe ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String chop() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "这是一把木头斧子";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
spring配置文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="chinese" class="Bean.collections.Chinese">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="schools">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>小学</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>中学</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>大学</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="health">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="血压">正常</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="身高">178</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="scores">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<entry key="数学">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>88</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</entry>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<entry key="语文">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>99</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</entry>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="axes">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<set>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>字符串斧子</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- 用嵌套bean定义属性 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="Bean.collections.WoodAxe"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- 引用bean作为属性 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref local="steelaxe"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</set>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="steelaxe" class="Bean.collections.SteelAxe"></bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</beans>
测试代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String path=new Test().getClass().getResource("/").getPath();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String realpath=path.substring(1, path.length());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/collection.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Person person1=(Person)context.getBean("chinese");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person1.useAxe();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果“
[小学, 中学, 大学]
{数学=88, 语文=99}
[字符串斧子, Bean.collections.WoodAxe@1506dc4, Bean.collections.SteelAxe@1126b07]
{血压=正常, 身高=178}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public interface Person ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void useAxe();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
package Bean.collections;
public interface Axe {
public String chop();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
定义实现类:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashSet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Properties;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Set;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import Bean.collections.Person;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Chinese implements Person ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private List schools=new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Map scores=new HashMap();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Properties health=new Properties();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Set axes=new HashSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Set getAxes() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return axes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setAxes(Set axes) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.axes = axes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Properties getHealth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return health;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHealth(Properties health) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.health = health;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List getSchools() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return schools;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setSchools(List schools) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.schools = schools;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Map getScores() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return scores;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setScores(Map scores) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.scores = scores;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void useAxe() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(schools);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(scores);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(axes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(health);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class SteelAxe implements Axe ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String chop() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "这是一把铁斧子";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Bean.collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class WoodAxe implements Axe ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String chop() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "这是一把木头斧子";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
spring配置文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="chinese" class="Bean.collections.Chinese">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="schools">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>小学</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>中学</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>大学</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="health">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="血压">正常</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="身高">178</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="scores">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<entry key="数学">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>88</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</entry>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<entry key="语文">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>99</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</entry>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="axes">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<set>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>字符串斧子</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- 用嵌套bean定义属性 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="Bean.collections.WoodAxe"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- 引用bean作为属性 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref local="steelaxe"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</set>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="steelaxe" class="Bean.collections.SteelAxe"></bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</beans>
测试代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String path=new Test().getClass().getResource("/").getPath();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String realpath=path.substring(1, path.length());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/collection.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Person person1=(Person)context.getBean("chinese");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person1.useAxe();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果“
[小学, 中学, 大学]
{数学=88, 语文=99}
[字符串斧子, Bean.collections.WoodAxe@1506dc4, Bean.collections.SteelAxe@1126b07]
{血压=正常, 身高=178}
相关文章推荐
- List,Set,Map,Properties的Spring注入实例
- List,Set,Map,Properties的Spring注入实例
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- java_spring_List,Map,Properties,Set注入与遍历
- Spring中注入List,Set,Map,Properties
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装
- Spring中注入List,Set,Map,Properties
- spring注入Set,List,Properties和Map
- Spring注入Properties、List、Map、Set
- spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装
- spring的list ,set,map,properties注入(set,get注入)
- Spring注入容器类型List、Map、Properties、Set
- Spring中注入List,Set,Map,Properties的xml文件配置方法
- (3) spring注入Properties,List,Set,Map
- Spring注入集合的值List,Set,Map,Properties
- Spring集合 (List,Set,Map,Properties) 实例
- spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装(引用)