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

一、通过XML装配Bean

  • 装配简易值
  • 装配集合
  • 命名空间装配(暂不测试)

二、测试例子

创建一个用户类:UserBean.java

 package com.xfwl.spring.assem;
/**
* Pojo实体类
* @author Jason
*
*/
public class UserBean {
private String uname;
private String upwd;
public UserBean(){}
public UserBean(String uname,String upwd){
this.uname=uname;
this.upwd=upwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
@Override
public String toString() {
return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";
} }

创建一个管理类:Manager.java

 package com.xfwl.spring.assem;

 import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
*
* @function
* @author 小风微凉
* @time 2018-7-10 上午11:32:01
*/
public class Manager{
private long id;
private List<UserBean> userList;
private Map<String,UserBean> userMap;
private Properties userProps;
private Set<UserBean> userSet;
private String[] userArr; /****getter和setter*******/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<UserBean> getUserList() {
return userList;
}
public void setUserList(List<UserBean> userList) {
this.userList = userList;
}
public Map<String, UserBean> getUserMap() {
return userMap;
}
public void setUserMap(Map<String, UserBean> userMap) {
this.userMap = userMap;
}
public Properties getUserProps() {
return userProps;
}
public void setUserProps(Properties userProps) {
this.userProps = userProps;
}
public Set<UserBean> getUserSet() {
return userSet;
}
public void setUserSet(Set<UserBean> userSet) {
this.userSet = userSet;
}
public String[] getUserArr() {
return userArr;
}
public void setUserArr(String[] userArr) {
this.userArr = userArr;
} }

配置文件

 <?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.0.xsd"> <!-- XML装配:装配简易值 -->
<bean id="user_tom" class="com.xfwl.spring.assem.UserBean">
<property name="uname" value="tom"></property>
<property name="upwd" value="123456"></property>
</bean>
<!-- XML装配:装配集合 -->
<bean id="user_xfww" class="com.xfwl.spring.assem.UserBean">
<property name="uname" value="小风微微"></property>
<property name="upwd" value="123456"></property>
</bean>
<bean id="user_xfll" class="com.xfwl.spring.assem.UserBean">
<property name="uname" value="小风凉凉"></property>
<property name="upwd" value="123456"></property>
</bean>
<bean id="manager" class="com.xfwl.spring.assem.Manager">
<property name="id" value="100001"/>
<property name="userList">
<list>
<ref bean="user_xfww"/>
<ref bean="user_xfll"/>
</list>
</property>
<property name="userMap">
<map>
<entry key="xfww" value-ref="user_xfww"/>
<entry key="xfll" value-ref="user_xfll"/>
</map>
</property>
<property name="userProps">
<props>
<prop key="xfww">小风微微</prop>
<prop key="xfll">小风凉凉</prop>
</props>
</property>
<property name="userSet">
<set>
<ref bean="user_xfww"/>
<ref bean="user_xfll"/>
</set>
</property>
<!-- property下不存在array -->
</bean> </beans>

测试类:TestBean.java

 package com.xfwl.spring.assem;
import java.util.Iterator; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; /**
* Spring Ioc测试
* @function
* @author 小风微凉
* @time 2018-7-10 上午9:55:15
*/
public class TestBean {
//项目相对路径
private static final String xmlRelPath="com/xfwl/spring/assem/applicationContext.xml";
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(xmlRelPath);
Manager manager =(Manager) context.getBean("manager");
System.out.println("size="+manager.getUserList().size());
Iterator<UserBean> its=manager.getUserList().iterator();
for(;its.hasNext();){
UserBean u=(UserBean)its.next();
System.out.println(u.toString());
}
}
}

测试结果:

 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
size=2
5 UserBean [uname=小风微微, upwd=123456]
6 UserBean [uname=小风凉凉, upwd=123456]
上一篇:JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解


下一篇:win7安装iis及web配置教程