我把这个放在设计模式这一栏,是因为我们可以使用这种简单的相当于spring的IOC原理的一种设计方式,对产生任何bean 带来了方便了.对我自己的理解,它更像是一种dao工厂的实现.而在前面,我贴了一篇文章,名字叫:很好用的工厂模式的实现方式,和这个是差不多的,只不过那种是读取properties文件,而这种是在使用了一个工具:commons-digester.jar,它是一个专门用于读取xml文件的一个工具,我们可以在xml文件中,配置好我们的bean,然后使用一个digesterFactory,来对bean进行生产.
一、先设计一个xml文件
config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans>
- <bean id="user" type="com.hejianjiao.entity.User"></bean>
- <bean id="message" type="com.hejianjiao.entity.Message"></bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="user" type="com.hejianjiao.entity.User"></bean> <bean id="message" type="com.hejianjiao.entity.Message"></bean> </beans>
这里的标签,我们可以自己定义。只不过现在没有使用DTD来对xml文件进行验证。然后我们就要设计两个对象了:Beans \ Bean
- Beans.java
- package com.hejianjiao.digester;
- import java.util.HashMap;
- import java.util.Iterator;
- public class Beans {
- private HashMap<String, Bean> beans = new HashMap<String, Bean>();
- public Iterator<String> getBeans() {
- return beans.keySet().iterator();
- }
- public Bean getBean(String id) {
- return (Bean) beans.get(id);
- }
- public void addBean(Bean bean) {
- beans.put(String.valueOf(bean.getId()), bean);
- }
- }
- Bean.java
- package com.hejianjiao.digester;
- public class Bean {
- private String id;
- private String type;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- }
Beans.java
package com.hejianjiao.digester;
import java.util.HashMap;
import java.util.Iterator;
public class Beans {
private HashMap<String, Bean> beans = new HashMap<String, Bean>();
public Iterator<String> getBeans() {
return beans.keySet().iterator();
}
public Bean getBean(String id) {
return (Bean) beans.get(id);
}
public void addBean(Bean bean) {
beans.put(String.valueOf(bean.getId()), bean);
}
}
Bean.java
package com.hejianjiao.digester;
public class Bean {
private String id;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
这个就是对xml文件的属性进行一个对象的标明。
三、就是一个DigesterFactory产生bean的工厂:
- package com.hejianjiao.digester;
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.digester.Digester;
- import org.xml.sax.SAXException;
- public class DigesterFactory {
- private static File input = new File(
- "src/com/hejianjiao/digester/config.xml");
- private static Digester digester = new Digester();
- private static DigesterFactory factory = null;
- private static Beans doDigester() {
- // 设置对XML文档资料是否进行DTD验证
- digester.setValidating(false);
- // 当遇到beans时,就产生一个Beans对象
- digester.addObjectCreate("beans", Beans.class);
- digester.addSetProperties("beans");
- // 当遇到 beans下边的bean时,就生产一个Bean对象
- digester.addObjectCreate("beans/bean", Bean.class);
- digester.addSetProperties("beans/bean");
- // 当再一次遇到bean对象时,就调用Beans中的addBean()方法
- digester.addSetNext("beans/bean", "addBean",
- "com.hejianjiao.digester.Bean");
- Beans beans = null;
- try {
- beans = (Beans) digester.parse(input);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- return beans;
- }
- @SuppressWarnings("static-access")
- public static Object getObject(String id) {
- Object obj = null;
- // 保证工厂的单一性
- if (factory == null) {
- factory = new DigesterFactory();
- }
- Bean bean = factory.doDigester().getBean(id);
- if (bean != null) {
- String className = bean.getType();
- try {
- obj = Class.forName(className).newInstance();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- System.out.println("Not found class named : " + className);
- e.printStackTrace();
- }
- }
- return obj;
- }
- }
package com.hejianjiao.digester;
import java.io.File;
import java.io.IOException;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;
public class DigesterFactory {
private static File input = new File(
"src/com/hejianjiao/digester/config.xml");
private static Digester digester = new Digester();
private static DigesterFactory factory = null;
private static Beans doDigester() {
// 设置对XML文档资料是否进行DTD验证
digester.setValidating(false);
// 当遇到beans时,就产生一个Beans对象
digester.addObjectCreate("beans", Beans.class);
digester.addSetProperties("beans");
// 当遇到 beans下边的bean时,就生产一个Bean对象
digester.addObjectCreate("beans/bean", Bean.class);
digester.addSetProperties("beans/bean");
// 当再一次遇到bean对象时,就调用Beans中的addBean()方法
digester.addSetNext("beans/bean", "addBean",
"com.hejianjiao.digester.Bean");
Beans beans = null;
try {
beans = (Beans) digester.parse(input);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return beans;
}
@SuppressWarnings("static-access")
public static Object getObject(String id) {
Object obj = null;
// 保证工厂的单一性
if (factory == null) {
factory = new DigesterFactory();
}
Bean bean = factory.doDigester().getBean(id);
if (bean != null) {
String className = bean.getType();
try {
obj = Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("Not found class named : " + className);
e.printStackTrace();
}
}
return obj;
}
}
这里面的写法,与规则,还有方法,可以参照以下去进行学习,这都是对于commons-digester.jar这个包进行的文档:
http://commons.apache.org/digester/
四、然后再看看测试类:
- package com.hejianjiao.test;
- import com.hejianjiao.digester.DigesterFactory;
- import com.hejianjiao.entity.User;
- public class Test {
- public static void main(String[] args) {
- User user = (User) DigesterFactory.getObject("user");
- System.out.println(user);
- }
- }
package com.hejianjiao.test;
import com.hejianjiao.digester.DigesterFactory;
import com.hejianjiao.entity.User;
public class Test {
public static void main(String[] args) {
User user = (User) DigesterFactory.getObject("user");
System.out.println(user);
}
}
可以看到,我们可以像工厂模式一样,对想要的bean进行生产。