背景:
阅读新闻
Spring中注入基本类型
[日期:2012-09-08] 来源:Linux社区 作者:woshixuye [字体:]
HelloWorld.Java
package com.xy.Hello;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* 该方法Spring容器通过setter方法注入
* @author xy
*
*/
public class HelloWorld
{
public HelloWorld()
{
super();
}
private String name;
private Set<String> sets;
private List<String> lst;
private Map<String, String> map;
private Properties pros;
public void DI()
{
System.out.println("-----name------");
System.out.println("Hello " + name);
System.out.println("-----sets------");
for (String s : sets)
{
System.out.println(s);
}
System.out.println("-----lst------");
for (String s : lst)
{
System.out.println(s);
}
System.out.println("-----map------");
for (String key : map.keySet())
{
System.out.println(key + "..." + map.get(key));
}
System.out.println("-----pro------");
for (Object key : pros.keySet())
{
System.out.println(key + "..." + pros.getProperty((String) key));
}
}
/***************************** Getter和Setter **************************************/
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Set<String> getSets()
{
return sets;
}
public void setSets(Set<String> sets)
{
this.sets = sets;
}
public List<String> getLst()
{
return lst;
}
public void setLst(List<String> lst)
{
this.lst = lst;
}
public Map<String, String> getMap()
{
return map;
}
public void setMap(Map<String, String> map)
{
this.map = map;
}
public Properties getPros()
{
return pros;
}
public void setPros(Properties pros)
{
this.pros = pros;
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
">
<bean>
<property>
<value>xy</value>
</property>
<property>
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property>
<list>
<value>lst1</value>
<value>lst2</value>
<value>lst3</value>
</list>
</property>
<property>
<map>
<entry key="mk1" value="mv1"></entry>
<entry key="mk2" value="mv2"></entry>
<entry key="mk3" value="mv3"></entry>
</map>
</property>
<property>
<props>
<prop key="pk1">pv1</prop>
<prop key="pk2">pv2</prop>
<prop key="pk3">pv3</prop>
</props>
</property>
</bean>
</beans>
TestJunit.java