Spring 裝配基本屬性和集合
阿新 • • 發佈:2019-02-18
Spring 裝配基本屬性的原理 String 型別 <property name="name" value="wtljiayou"></property> Integer型別 <property name="id" value="88"></property> Spring裝配各種集合型別的屬性 <?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" > <!-- String 型別--> <property name="name" value="wtljiayou"></property> <!--Integer型別--> <property name="id" value="88"></property> <!-- 注入的集合 --> application.xml <property name="sets"> <set> <value>第一個</value> <value>第二個</value> <value>第三個</value> <value>第四個</value> </set> </property> <!-- 注入list集合 --> <property name="list"> <list> <value>第一個</value> <value>第三個</value> <value>第二個</value> <value>第一個</value> </list> </property> <!-- 注入properties --> <property name="properties"> <props> <prop key="key1">可以1</prop> <prop key="key2">可以2</prop> <prop key="key3">可以3</prop> </props> </property> <!-- 注入Map --> <property name="maps"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> </map> </property> </beans> //這裡本身只需要set方法就行,但是為了測試時的呼叫,就寫了get方法 PersonServiceBean.java package com.service.impl; public class PersonServiceBean implements PersonService { private PersonDao personDao; private String name; //注入集合 private Set<String> sets =new HashSet<String>(); //注入List集合 private List<String> list = new ArrayList<String>(); //注入properties private Properties properties =new Properties(); //注入Map<String,String> private Map<String ,String> maps=new HashMap<String, String>(); public void setName(String name) { this.name = name; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } 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 List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public void save(){ personDao.add(); System.out.println(name); } } PersonService.java package com.service; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public interface PersonService { public abstract void save(); public abstract Set<String> getSets(); public abstract List<String> getList(); public abstract Properties getProperties(); public abstract Map<String, String> getMaps(); } PersonDao.java package com.dao; public interface PersonDao { public abstract void add(); } PersonDaoBean.java package com.dao.impl; import com.dao.PersonDao; public class PersonDaoBean implements PersonDao { public void add(){ System.out.println("z執行PersonDaoBean中的方法"); } }