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="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-2.5.xsd">
<bean id="xyhello" class="com.xy.Hello.HelloWorld">
<property name="name">
<value>xy</value>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="lst">
<list>
<value>lst1</value>
<value>lst2</value>
<value>lst3</value>
</list>
</property>
<property name="map">
<map>
<entry key="mk1" value="mv1"></entry>
<entry key="mk2" value="mv2"></entry>
<entry key="mk3" value="mv3"></entry>
</map>
</property>
<property name="pros">
<props>
<prop key="pk1">pv1</prop>
<prop key="pk2">pv2</prop>
<prop key="pk3">pv3</prop>
</props>
</property>
</bean>
</beans>
TestJunit.java
public class TestJUnit
{
@Test
public void test()
{
// 启动Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 容器中获取类的实例
HelloWorld hello = (HelloWorld) context.getBean("xyhello");
// 调用方法
hello.DI();
}
}
本文转自IT徐胖子的专栏博客51CTO博客,原文链接http://blog.51cto.com/woshixy/983038如需转载请自行联系原作者
woshixuye111