一、通过XML装配Bean
- 装配简易值
- 装配集合
- 命名空间装配(暂不测试)
二、测试例子
创建一个用户类:UserBean.java
1 package com.xfwl.spring.assem; 2 /** 3 * Pojo实体类 4 * @author Jason 5 * 6 */ 7 public class UserBean { 8 private String uname; 9 private String upwd;10 public UserBean(){}11 public UserBean(String uname,String upwd){12 this.uname=uname;13 this.upwd=upwd;14 }15 public String getUname() {16 return uname;17 }18 public void setUname(String uname) {19 this.uname = uname;20 }21 public String getUpwd() {22 return upwd;23 }24 public void setUpwd(String upwd) {25 this.upwd = upwd;26 }27 @Override28 public String toString() {29 return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";30 } 31 32 }
创建一个管理类:Manager.java
1 package com.xfwl.spring.assem; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 /** 8 * 9 * @function 10 * @author 小风微凉11 * @time 2018-7-10 上午11:32:0112 */13 public class Manager{14 private long id;15 private ListuserList;16 private Map userMap;17 private Properties userProps;18 private Set userSet;19 private String[] userArr;20 21 /****getter和setter*******/22 public long getId() {23 return id;24 }25 public void setId(long id) {26 this.id = id;27 }28 public List getUserList() {29 return userList;30 }31 public void setUserList(List userList) {32 this.userList = userList;33 }34 public Map getUserMap() {35 return userMap;36 }37 public void setUserMap(Map userMap) {38 this.userMap = userMap;39 }40 public Properties getUserProps() {41 return userProps;42 }43 public void setUserProps(Properties userProps) {44 this.userProps = userProps;45 }46 public Set getUserSet() {47 return userSet;48 }49 public void setUserSet(Set userSet) {50 this.userSet = userSet;51 }52 public String[] getUserArr() {53 return userArr;54 }55 public void setUserArr(String[] userArr) {56 this.userArr = userArr;57 }58 59 60 }
配置文件
1 27 8 9 10 13 1411 12 15 1816 17 19 2220 21 23 50 5124 25 29 3031 35 3637 4238 41小风微微 39小风凉凉 4043 48 4944 45 46 47
测试类:TestBean.java
1 package com.xfwl.spring.assem; 2 import java.util.Iterator; 3 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 import org.springframework.context.support.FileSystemXmlApplicationContext; 7 8 /** 9 * Spring Ioc测试10 * @function 11 * @author 小风微凉12 * @time 2018-7-10 上午9:55:1513 */14 public class TestBean {15 //项目相对路径16 private static final String xmlRelPath="com/xfwl/spring/assem/applicationContext.xml";17 public static void main(String[] args) {18 ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(xmlRelPath);19 Manager manager =(Manager) context.getBean("manager");20 System.out.println("size="+manager.getUserList().size());21 Iteratorits=manager.getUserList().iterator();22 for(;its.hasNext();){23 UserBean u=(UserBean)its.next();24 System.out.println(u.toString());25 }26 }27 }
测试结果:
1 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).2 log4j:WARN Please initialize the log4j system properly.3 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.4 size=25 UserBean [uname=小风微微, upwd=123456]6 UserBean [uname=小风凉凉, upwd=123456]