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
private List
private 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
{
return sets;
}
public void setSets(Set
{
this.sets = sets;
}
public List
{
return lst;
}
public void setLst(List
{
this.lst = lst;
}
public Map
{
return map;
}
public void setMap(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" >
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">
TestJunit.java
public class TestJUnit
{
@Test
public void test()
{
// 启动Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 容器中获取类的实例
HelloWorld hello = (HelloWorld) context.getBean("xyhello");
// 调用方法
hello.DI();
}
}