千山鸟飞绝 万径人踪灭
勤练内功,不断实践招数。争取早日成为武林高手

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()方法

posted on 2009-08-27 18:19 笑口常开、财源滚滚来! 阅读(1600) 评论(3)  编辑  收藏 所属分类: spring学习
Comments
  • # re: 集合对象注入&&通过构造函数注入
    雨中滴
    Posted @ 2009-08-28 14:37
    真的很无聊- -  回复  更多评论   
  • # re: 集合对象注入&&通过构造函数注入
    chencx
    Posted @ 2009-08-29 16:43
    无聊…的飘过……  回复  更多评论   
  • # re: 集合对象注入&&通过构造函数注入
    game
    Posted @ 2016-07-19 09:54
    这哪是通过构造函数注入?明明就是通过setter注入的。  回复  更多评论   

只有注册用户登录后才能发表评论。


网站导航: