程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 集合對象注入&&通過構造函數注入

集合對象注入&&通過構造函數注入

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved