现在在做个小项目,使系统能够自动记录数据库的操作日志,想到的方法是用动态代理,现在我能够得到代理的数据库实体类,我现在想根据这些实体类判断出操作的到底是哪个数据表的内容,而且这东西以后加了数据表后还可以进行判断,具体该怎么设计比较好啊?关键代码如下:
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
Session session = HibernateSessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
try {
// before handling
//logger.log(Level.INFO, "method starts..." + method.getName());
String methodname = method.getName();
result = method.invoke(delegate, args);
Class c = Class.forName(delegate.getClass().getName());
Method[] ms = c.getMethods();
Method mm = null;
for(int i=0;i<ms.length;i++){
Method m = ms[i];
String mn=m.getName();
if(mn.equals(method.getName())){
mm = m;
break;
}
}
if(mm !=null){
// boolean b=mm.isAnnotationPresent(MethodDescription.class);
// if(b){
// SOperationLog so=new SOperationLog();
// so.setDtOperateDate(new Date());
// //获取session
// HttpSession sess = WebContextFactory.get().getSession();
// Object o=sess.getAttribute("user");
// if(o==null){
// so.setUserId(-1);
// so.setStrUserAccount("系统管理");
// }else{
// SUser user=(SUser)o;
// so.setUserId(user.getNid());
// so.setStrUserAccount(user.getRealName());
// }
//
// MethodDescription ann=mm.getAnnotation(MethodDescription.class);
// so.setStrOperateContent(ann.dec());
// System.out.println(ann.dec());
// sdao.save(so);
// }
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
Session session = HibernateSessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
try {
// before handling
//logger.log(Level.INFO, "method starts..." + method.getName());
String methodname = method.getName();
result = method.invoke(delegate, args);
Class c = Class.forName(delegate.getClass().getName());
Method[] ms = c.getMethods();
Method mm = null;
for(int i=0;i<ms.length;i++){
Method m = ms[i];
String mn=m.getName();
if(mn.equals(method.getName())){
mm = m;
break;
}
}
if(mm !=null){
// boolean b=mm.isAnnotationPresent(MethodDescription.class);
// if(b){
// SOperationLog so=new SOperationLog();
// so.setDtOperateDate(new Date());
// //获取session
// HttpSession sess = WebContextFactory.get().getSession();
// Object o=sess.getAttribute("user");
// if(o==null){
// so.setUserId(-1);
// so.setStrUserAccount("系统管理");
// }else{
// SUser user=(SUser)o;
// so.setUserId(user.getNid());
// so.setStrUserAccount(user.getRealName());
// }
//
// MethodDescription ann=mm.getAnnotation(MethodDescription.class);
// so.setStrOperateContent(ann.dec());
// System.out.println(ann.dec());
// sdao.save(so);
// }
}