自己重写Spring_XmlBeanFactory_BackUp

自己重写Spring_XmlBeanFactory_BackUp
2007年11月25日 星期日 04:24
自己重写Spring_XmlBeanFactory_BackUp
-------------------------------------------------------------------------------------------
八个文件
-------------------------------------------------------------------------------------------
log4j.properties
-------------------------------------------------------------------------------------------
log4j.logger.netlogger=DEBUG,file


log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/info0620.html



log4j.appender.file.layout=org.apache.log4j.HTMLLayout




-------------------------------------------------------------------------------------------
beans.xml
-------------------------------------------------------------------------------------------

<bean >
<property value="log4j.properties" />
<property value="netlogger" />

</bean>



<bean >
<property value="backup.txt" />

</bean>
</beans>

-------------------------------------------------------------------------------------------
BackUpIf.java
-------------------------------------------------------------------------------------------
import java.util.Collection;
/**
* 备份系统,只管对出异常的数据进行保存,不用管什么地方 发生异常,有数据来,保存就可以了.并且提

供相应的获取 备份数据的方法和清空备份文件
*/
public interface BackUpIf {
// 把数据保存到外存,当发生异常的时候,这个
// 方法就会被调用,用来保存发送失败的数据
public void store(Collection col);
// 把保存的文件拿出来.
public Collection load();
// 清空备份文件
public void clear();
}

-------------------------------------------------------------------------------------------
BackUpImpl.java
-------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
/**
* 备份系统,只管对出异常的数据进行保存,不用管什么地方 发生异常,有数据来,保存就可以了.并且提

供相应的获取 备份数据的方法和清空备份文件
*/
public class BackUpImpl implements BackUpIf {
private String fileName;
public BackUpImpl() {}
private LogIf getLog() {
XmlBeanFactory factory = new XmlBeanFactory("beans.xml");
LogIf log = (LogIf) factory.getBean("log");
return log;
}
// 把数据保存到外存,当发生异常的时候,这个
// 方法就会被调用,用来保存发送失败的数据
public void store(Collection col) {
getLog().writeDebug(this.getClass().getName()+"===>abcdef");
try {
File file = new File(fileName);
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(col);
oos.close();
} catch (Exception ex) {
getLog().writeWarn("store backUp error: " + ex.getMessage());
ex.printStackTrace();
}
}
// 把保存的文件拿出来.
public Collection load() {
try {
File file = new File(fileName);
if (file.createNewFile()) {
return new ArrayList();
} else {
if (file.length() > 4) {
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(file));
ArrayList array = new ArrayList();
array = (ArrayList) ois.readObject();
ois.close();
return array;
} else {
return new ArrayList();
}
}
} catch (Exception ex) {
getLog().writeWarn("load backUp error: " + ex.getMessage());
ex.printStackTrace();
return null;
}
}
// 清空备份文件
public void clear() {
try {
File file = new File(fileName);
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.println();
pw.close();
} catch (Exception ex) {
getLog().writeWarn("clear backUp error: " + ex.getMessage());
ex.printStackTrace();
}
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}

-------------------------------------------------------------------------------------------
LogIf.java
-------------------------------------------------------------------------------------------
public interface LogIf {
public void writeDebug(String info);
public void writeInfo(String info);
public void writeWarn(String info);
public void writeError(String info);
public void writeFatal(String info);
}

-------------------------------------------------------------------------------------------
LogImpl.java
-------------------------------------------------------------------------------------------
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
* 分别提供五个方法给客户调用,debug,info,warn,error, fatal分别是五个从低到高的级别.用户只管

调用该Log模块
* 所提供的方法,来写日志,而不用取关系LogImpl是怎么去 实现的.
*/
public class LogImpl implements LogIf {
private Properties pro;
private Logger netLogger;
private String propfile;
private String logger_name;
public LogImpl() {}
private void init() {
PropertyConfigurator.configure(propfile); // 初始化配置文件
netLogger = Logger.getLogger(logger_name);// 获取Logger实例
}
public void writeDebug(String info) {
init();
netLogger.debug("Debug: " + info);
}
public void writeInfo(String info) {
init();
netLogger.info("Info: " + info);
}
public void writeWarn(String info) {
init();
netLogger.warn("Warn: " + info);
}
public void writeError(String info) {
init();
netLogger.error("Error: " + info);
}
public void writeFatal(String info) {
init();
netLogger.fatal("Fatal: " + info);
}
public void setLogger_name(String logger_name) {
this.logger_name = logger_name;
}
public void setPropfile(String propfile) {
this.propfile = propfile;
}
}

-------------------------------------------------------------------------------------------
SpringStartup.java
-------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class SpringStartup {
/**
* @param args
*/
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory("beans.xml");
BackUpIf backUp = (BackUpIf) factory.getBean("backup");
ArrayList al=new ArrayList();
al.add("bb");
al.add("cc");
backUp.store(al);
Collection col=backUp.load();
Iterator iter=col.iterator();
while(iter.hasNext()){

System.out.println(iter.next());
}
}
}
-------------------------------------------------------------------------------------------
XmlBeanFactory.java
-------------------------------------------------------------------------------------------
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlBeanFactory {
private Properties pro;
private Document document = null;
private static String XMLFILE = null;
private static String key = "key";
public XmlBeanFactory(String XMLFILE) {
this.XMLFILE = XMLFILE;
init();
}
private void init() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(XMLFILE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 解析XML文件
private HashMap xmlParser(String nodeName, String id_key, String id_value) {
HashMap pro = new HashMap();
try {
NodeList list = document.getElementsByTagName(nodeName);
for (int k = 0; k < list.getLength(); k++) {
Node fatherNode = list.item(k);
NamedNodeMap attributes = fatherNode.getAttributes();
//
boolean flag = false;
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().equals(id_key)
&& attribute.getNodeValue().equals

(id_value)) {
flag = true;
}
}
if (!flag)
continue;
//
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
pro.put(attribute.getNodeName(),

attribute.getNodeValue());
}
//
NodeList childNodes = fatherNode.getChildNodes();
HashMap property = new HashMap();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
if (childNode instanceof Element) {
NamedNodeMap child_attributes = childNode
.getAttributes();
Node child_attribute_name =

child_attributes.item(0);
Node child_attribute_value =

child_attributes.item(1);
property.put

(child_attribute_name.getNodeValue(),


child_attribute_value.getNodeValue());
}
}
pro.put("property", property);
break;
}
return pro;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public Object getBean(String id_value) {
String nodeName = "bean";
HashMap pro = xmlParser(nodeName, "id", id_value);
return ClassLoadFactory.loadClassByConstructor(pro);
}
}
/** 类装载工厂 */
class ClassLoadFactory {
public static Object loadClassByConstructor(HashMap pro) {
try {
Class cl = Class.forName((String) pro.get("class"));
Object obj = cl.newInstance();
Class[] p = { String.class };
//
HashMap property = (HashMap) pro.get("property");
Set mappings = property.entrySet();
Iterator it = mappings.iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
String me.getKey();
String value = (String) me.getValue();
String methodName = name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
Method method = cl.getMethod("set" + methodName, p);
Object[] values = { value };
method.invoke(obj, values);
}
//
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}

-------------------------------------------------------------------------------------------

本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 自己重写Spring_XmlBeanFactory_BackUp_熊熊之家

你可能感兴趣的:(java,apache,spring,log4j,bean)