public interface IPersonService {
public abstract void Save();
public Set<String> getSets() ;
public List<String> getLists() ;
public Properties getProperties() ;
public Map<String, String> getMaps() ;
}
public class PersonServiceBean implements IPersonService {
private IPersonDao iPersonDao;
private Set<String> sets=new HashSet<String>();
private List<String> lists=new ArrayList<String>();
private Properties properties=new Properties();
private Map<String,String> maps=new HashMap<String,String>();
public PersonServiceBean(IPersonDao personDao, String name) {
iPersonDao = personDao;
this.name = name;
}
public void Save(){
System.out.println(name);//输出name
iPersonDao.add();
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public IPersonDao getIPersonDao() {
return iPersonDao;
}
public void setIPersonDao(IPersonDao personDao) {
iPersonDao = personDao;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
}
测试类:
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");
// ItcastClassPathXMLApplicationContext ctx=new
// ItcastClassPathXMLApplicationContext("beans.xml");
//
IPersonService ipersonService = (IPersonService) ctx
.getBean("personService");
//集合对象的遍历
System.out.println("===========set==================");
for (String value : ipersonService.getSets()) {
System.out.println(value);
}
// ipersonService.Save();
// ctx.close();
// ctx.registerShutdownHook();
System.out.println("===========List=================");
for(String value:ipersonService.getLists()){
System.out.println(value);
}
System.out.println("=========properties===============");
for(Object value:ipersonService.getProperties().keySet()){
System.out.println(value);
}
System.out.println("================maps==================");
for(Object value:ipersonService.getMaps().keySet()){
System.out.println(value);
}
//调用PersonServiceBean的sava方法,输出结果
ipersonService.Save();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="personService"
class="cn.itcast.service.impl.PersonServiceBean">
<property name="IPersonDao" ref="personDaoBean"></property>
<constructor-arg index="0" ref="personDaoBean"
type="cn.itcast.dao.IPersonDao" />
<constructor-arg index="1" type="java.lang.String"
value="传智博客">
</constructor-arg>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="properties">
<props>
<prop key="properties1">property1</prop>
<prop key="properties2">property2</prop>
<prop key="properties3">property3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key1" value="keyFirst"></entry>
<entry key="key2" value="keySecond"></entry>
<entry key="key3" value="keyThird"></entry>
</map>
</property>
</bean>
<bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<!--
<bean id="anotherPersonServiceBean"
class="cn.itcast.service.impl.AnotherPersonServiceBean" >
</bean>
-->
</beans>
public class PersonDaoBean implements IPersonDao {
public void add(){
System.out.println("这是personDaoBean的Add()方法");
}
}
输出:
===========set==================
set1
set2
set3
===========List=================
list1
list2
list3
=========properties===============
properties3
properties2
properties1
================maps==================
key1
key2
key3
传智博客
这是personDaoBean的Add()方法