posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

利用java的动态代理写日志

Posted on 2008-02-05 03:12 semovy 阅读(1137) 评论(0)  编辑  收藏 所属分类: 设计模式JAVA应用
1.测试的实体Bean  Person类

Person.java

package com.semovy.bean;

public class Person {
 
 private String id;

 private String name;

 private String note;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getNote() {
  return note;
 }

 public void setNote(String note) {
  this.note = note;
 }

 public Person(String id, String name, String note) {
  super();
  this.id = id;
  this.name = name;
  this.note = note;
 }

 @Override
 public String toString() {
  // TODO Auto-generated method stub
  return "id: " + id + " name: " +  name + " note: " + note;
 }

}
2.操作Person实体类的接口PersonService

PersonService.java

package com.semovy.service;

import java.util.ArrayList;
import java.util.List;

import com.semovy.bean.Person;

public interface PersonService {

 public List<Person> persons = new ArrayList<Person>();

 public abstract void addPerson(Person person);

 public abstract Person getPersonById(String id);

 public abstract void deletePersonById(String id);

 //public abstract void updatePerson(Person person);

 public abstract void displayPersonById(String id);

 public abstract void displayAllPersons();
}
3.实现PersonService接口的PersonServiceImpl

PersonServiceImpl.java

package com.semovy.service.impl;

import java.util.Iterator;

import com.semovy.bean.Person;
import com.semovy.service.PersonService;

public class PersonServiceImpl implements PersonService {

 public void addPerson(Person person) {
  persons.add(person);
 }

 public void deletePersonById(String id) {
  Iterator it = persons.iterator();
  while(it.hasNext())
  {
   Person person = (Person)it.next();
   if(person.getId().equals(id))
   {
    it.remove();
    break;
   }
  }
 }

 public void displayAllPersons() {
  
  Iterator it = persons.iterator();
  while(it.hasNext())
  {
   Person person = (Person)it.next();
   System.out.println(person.toString());
  }
 }

 public void displayPersonById(String id) {
  
  Iterator it = persons.iterator();
  while(it.hasNext())
  {
   Person person = (Person)it.next();
   if(person.getId().equals(id))
   {
    System.out.println(person.toString());
    break;
   }
  }
  
 }

 public Person getPersonById(String id) {
  Iterator it = persons.iterator();
  while(it.hasNext())
  {
   Person person = (Person)it.next();
   if(person.getId().equals(id))
   {
    return person;
   }
  }
  return null;
 }

 /*public void updatePerson(Strin) {
  Iterator it = persons.iterator();
  while(it.hasNext())
  {
   Person person = (Person)it.next();
   if(person == aperson)
   {
    persons.set(arg0, arg1)
   }
  }
 }*/

}

4.写日志的代理类LogProxy   它实现了InvocationHandler接口

LogProxy.java

package com.semovy.bean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;


public class LogProxy implements InvocationHandler {
 
 Logger logger = Logger.getLogger(this.getClass().getName());
 private Object delegate = null;
 
 public Object getDelegate() {
  return delegate;
 }

 public void setDelegate(Object delegate) {
  this.delegate = delegate;
 }

 public Object bind(Object delegate) {
  this.delegate = delegate;
  return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
    delegate.getClass().getInterfaces(),this);
 }

 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  Object result = null;
  try
  {
   logger.log(Level.INFO,method.getName() +  " 开始处理...");
   result = method.invoke(this.delegate, args);
   logger.log(Level.INFO,method.getName() +  " 结束处理...");
  }catch(Exception e)
  {
   logger.log(Level.ERROR,e.getLocalizedMessage());
  }
  
  return result;
 }
}

5.在eclipse中添加log4j.properties

log4j.properties

log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p (%F:%L) - %m%n

5.写测试类Test.java

Test.java

package com.semovy.test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import com.semovy.bean.LogProxy;
import com.semovy.bean.Person;
import com.semovy.service.PersonService;
import com.semovy.service.impl.PersonServiceImpl;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {

  LogProxy logProxy = new LogProxy();
  PersonService personService = (PersonService)logProxy.bind(new PersonServiceImpl());
  personService.addPerson(new Person("1","name1","note1"));
  personService.addPerson(new Person("2","name2","note2"));
  personService.addPerson(new Person("3","name3","note3"));
  personService.addPerson(new Person("4","name4","note4"));
  personService.displayPersonById("3");
  personService.displayAllPersons();
  Person person = personService.getPersonById("2");
  person.setName("名称2");
  person.setNote("注脚2");
  personService.displayAllPersons();
  personService.deletePersonById("2");
  personService.displayAllPersons();
  //saveToFile(persons,"c:/persons.txt");
 }
 
 public static void saveToFile(List list,String path)
 {
  System.out.println("开始写入文件...");
  try
  {
   PrintWriter out = new PrintWriter(new File(path));
   Iterator it = list.iterator();
   while(it.hasNext())
   {
    Person person = (Person)it.next();
    out.println(person.toString());
   }
   out.flush();
   out.close();
   System.out.println("结束写入文件...");
  }catch(IOException e)
  {
   System.out.println(e.getLocalizedMessage());
  }
  
 }
}


6.运行测试结果:

2008-02-05 02:55:28,926  INFO (LogProxy.java:34) - addPerson 开始处理...
2008-02-05 02:55:28,926  INFO (LogProxy.java:36) - addPerson 结束处理...
2008-02-05 02:55:28,926  INFO (LogProxy.java:34) - addPerson 开始处理...
2008-02-05 02:55:28,926  INFO (LogProxy.java:36) - addPerson 结束处理...
2008-02-05 02:55:28,926  INFO (LogProxy.java:34) - addPerson 开始处理...
2008-02-05 02:55:28,936  INFO (LogProxy.java:36) - addPerson 结束处理...
2008-02-05 02:55:28,936  INFO (LogProxy.java:34) - addPerson 开始处理...
2008-02-05 02:55:29,036  INFO (LogProxy.java:36) - addPerson 结束处理...
2008-02-05 02:55:29,036  INFO (LogProxy.java:34) - displayPersonById 开始处理...
id: 3 name: name3 note: note3
2008-02-05 02:55:29,036  INFO (LogProxy.java:36) - displayPersonById 结束处理...
2008-02-05 02:55:29,036  INFO (LogProxy.java:34) - displayAllPersons 开始处理...
id: 1 name: name1 note: note1
id: 2 name: name2 note: note2
id: 3 name: name3 note: note3
id: 4 name: name4 note: note4
2008-02-05 02:55:29,046  INFO (LogProxy.java:36) - displayAllPersons 结束处理...
2008-02-05 02:55:29,046  INFO (LogProxy.java:34) - getPersonById 开始处理...
2008-02-05 02:55:29,046  INFO (LogProxy.java:36) - getPersonById 结束处理...
2008-02-05 02:55:29,046  INFO (LogProxy.java:34) - displayAllPersons 开始处理...
id: 1 name: name1 note: note1
id: 2 name: 名称2 note: 注脚2
id: 3 name: name3 note: note3
id: 4 name: name4 note: note4
2008-02-05 02:55:29,046  INFO (LogProxy.java:36) - displayAllPersons 结束处理...
2008-02-05 02:55:29,046  INFO (LogProxy.java:34) - deletePersonById 开始处理...
2008-02-05 02:55:29,046  INFO (LogProxy.java:36) - deletePersonById 结束处理...
2008-02-05 02:55:29,046  INFO (LogProxy.java:34) - displayAllPersons 开始处理...
id: 1 name: name1 note: note1
id: 3 name: name3 note: note3
id: 4 name: name4 note: note4
2008-02-05 02:55:29,046  INFO (LogProxy.java:36) - displayAllPersons 结束处理...



只有注册用户登录后才能发表评论。


网站导航: