概述
需求:编写一个BeanFactroy类来读取XML配置文件中配置的JavaBean信息,将读取都的信息使用BeanUtils封装进JavaBean对象,将对象存储在Map集合,并在BeanFactory中提供通过id查找JavaBean的方法。
实现
准备jar包:

代码实现:
public class User {
/**
* @Fields uid : 用户ID
*/
private String uid;
/**
* @Fields userName : 用户名
*/
private String userName;
/**
* @Fields password : 密码
*/
private String password;
/**
* @Title: User
*/
public User() {
super();
}
/**
* @return the uid
*/
public String getUid() {
return uid;
}
/**
* @param uid the uid to set
*/
public void setUid(String uid) {
this.uid = uid;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @Title: toString
* @Description: 用户的toString方法
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [uid=" + uid + ", userName=" + userName + ", password=" + password + "]";
}
}
public class Book {
private String bid;
private String title;
private String price;
@Override
public String toString() {
return "Book{" +
"bid='" + bid + '\'' +
", title='" + title + '\'' +
", price='" + price + '\'' +
'}';
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
public class BeanConfig {
private String id;
private String className;
private Properties attrs;
@Override
public String toString() {
return "BeanConfig{" +
"id='" + id + '\'' +
", className='" + className + '\'' +
", attrs=" + attrs +
'}';
}
public BeanConfig(){}
public BeanConfig(String id, String className, Properties attrs) {
this.id = id;
this.className = className;
this.attrs = attrs;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public Properties getAttrs() {
return attrs;
}
public void setAttrs(Properties attrs) {
this.attrs = attrs;
}
//把当前解析出来的数据生成对象 并且将属性设置进去
public Object config(){
try {
Class c = Class.forName(className);
Object o = c.newInstance();
for(String prop:attrs.stringPropertyNames()){
String name = prop;
String value = attrs.getProperty(prop);
BeanUtils.setProperty(o, name, value);
}
return o;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}
public class BeanFactory {
//容器对象,放的是容器中所有的bean的对象
private static Map<String,BeanConfig> context =new HashMap<>();
//类加载的时候直接读取xml中的内容
static {
SAXReader sax = new SAXReader();
try {
Document doc = sax.read("beans.xml");
//根节点
Element root =doc.getRootElement();
//所有的bean对象
List<Element> beans = root.elements("bean");
for (Element bean:beans){
//id 和className是属性
String id = bean.attributeValue("id");
String className = bean.attributeValue("className");
Properties attrs = new Properties();
List<Element> props = bean.elements("property");
for(Element prop:props){
//获取property标签的name和value属性
String name = prop.attributeValue("name");
String value = prop.attributeValue("value");
attrs.setProperty(name, value);
}
//单个beanConfig对象
BeanConfig bf = new BeanConfig(id,className,attrs);
//beanConfig放到容器中
context.put(bf.getId(),bf);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
//根据ID 获取容器中的bean对象
public static Object getBean(String id){
BeanConfig bf = context.get(id);
return bf.config();
}
}
public static void main(String[] args) {
//控制权在用户身上
User u = new User();
u.setUid("001");
//控制权在容器里面
User o1 = (User) BeanFactory.getBean("userId01");
System.out.println(o1);
//控制权发生了反转 IOC
Book o2 = (Book)BeanFactory.getBean("bookId02");
System.out.println(o2);
}
306

被折叠的 条评论
为什么被折叠?



