返回顶部

JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(9):通过XML装配Bean

一、通过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     @Override
28     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:01
12  */
13 public class Manager{
14     private long id;
15     private List<UserBean> userList;
16     private Map<String,UserBean> userMap;
17     private Properties userProps;
18     private Set<UserBean> 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<UserBean> getUserList() {
29         return userList;
30     }
31     public void setUserList(List<UserBean> userList) {
32         this.userList = userList;
33     }
34     public Map<String, UserBean> getUserMap() {
35         return userMap;
36     }
37     public void setUserMap(Map<String, UserBean> 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<UserBean> getUserSet() {
47         return userSet;
48     }
49     public void setUserSet(Set<UserBean> 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 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans 
 3     xmlns="http://www.springframework.org/schema/beans" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 7     
 8     <!-- XML装配:装配简易值 -->
 9     <bean id="user_tom" class="com.xfwl.spring.assem.UserBean">
10         <property name="uname" value="tom"></property>        
11         <property name="upwd" value="123456"></property>
12     </bean>    
13     <!-- XML装配:装配集合 -->
14     <bean id="user_xfww" class="com.xfwl.spring.assem.UserBean">
15         <property name="uname" value="小风微微"></property>        
16         <property name="upwd" value="123456"></property>
17     </bean>    
18     <bean id="user_xfll" class="com.xfwl.spring.assem.UserBean">
19         <property name="uname" value="小风凉凉"></property>        
20         <property name="upwd" value="123456"></property>
21     </bean>
22     <bean id="manager" class="com.xfwl.spring.assem.Manager">
23         <property name="id" value="100001"/> 
24         <property name="userList">
25             <list>
26                 <ref bean="user_xfww"/>
27                 <ref bean="user_xfll"/>
28             </list>
29         </property>
30         <property name="userMap">
31             <map>
32                 <entry key="xfww" value-ref="user_xfww"/>
33                 <entry key="xfll" value-ref="user_xfll"/>
34             </map>
35         </property>
36         <property name="userProps">
37             <props>
38                 <prop key="xfww">小风微微</prop>
39                 <prop key="xfll">小风凉凉</prop>
40             </props>
41         </property>
42         <property name="userSet">
43             <set>
44                 <ref bean="user_xfww"/>
45                 <ref bean="user_xfll"/>
46             </set>        
47         </property>
48         <!-- property下不存在array -->
49     </bean>    
50     
51 </beans>

 测试类: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:15
13  */
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         Iterator<UserBean> its=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=2
5 UserBean [uname=小风微微, upwd=123456]
6 UserBean [uname=小风凉凉, upwd=123456]

 

posted @ 2018-07-11 13:51  小风微灵-彦  阅读(508)  评论(0编辑  收藏  举报
加载中……