Spring AOP控制日志管理

Spring AOP控制日志管理

1. 编写po类 LogInfo
package  com.strongit.credit.manager.log.po;

/**
 * LogInfo generated by MyEclipse Persistence Tools
 
*/

public   class  LogInfo  implements  java.io.Serializable {

    
//  Fields

    
/**
     * 
     
*/
    
private   static   final   long  serialVersionUID  =   1L ;

    
private  Long logId;

    
private  String logUserName;

    
private  String logOrgName;

    
private  String logContent;

    
private  String logIp;

    
private  String remark;

    
private  String logDate;

    
private  String logOrgId;

    
//  Constructors

    
/**  default constructor  */
    
public  LogInfo() {
    }

    
/**  full constructor  */
    
public  LogInfo(String logUserName, String logOrgName, String logContent,
            String logIp, String remark, String logDate, String logOrgId) {
        
this .logUserName  =  logUserName;
        
this .logOrgName  =  logOrgName;
        
this .logContent  =  logContent;
        
this .logIp  =  logIp;
        
this .remark  =  remark;
        
this .logDate  =  logDate;
        
this .logOrgId  =  logOrgId;
    }

    
//  Property accessors

    
public  Long getLogId() {
        
return   this .logId;
    }

    
public   void  setLogId(Long logId) {
        
this .logId  =  logId;
    }

    
public  String getLogUserName() {
        
return   this .logUserName;
    }

    
public   void  setLogUserName(String logUserName) {
        
this .logUserName  =  logUserName;
    }

    
public  String getLogOrgName() {
        
return   this .logOrgName;
    }

    
public   void  setLogOrgName(String logOrgName) {
        
this .logOrgName  =  logOrgName;
    }

    
public  String getLogContent() {
        
return   this .logContent;
    }

    
public   void  setLogContent(String logContent) {
        
this .logContent  =  logContent;
    }

    
public  String getLogIp() {
        
return   this .logIp;
    }

    
public   void  setLogIp(String logIp) {
        
this .logIp  =  logIp;
    }

    
public  String getRemark() {
        
return   this .remark;
    }

    
public   void  setRemark(String remark) {
        
this .remark  =  remark;
    }

    
public  String getLogDate() {
        
return   this .logDate;
    }

    
public   void  setLogDate(String logDate) {
        
this .logDate  =  logDate;
    }

    
public  String getLogOrgId() {
        
return   this .logOrgId;
    }

    
public   void  setLogOrgId(String logOrgId) {
        
this .logOrgId  =  logOrgId;
    }

}

2. 编写action  class LogManage
package  com.strongit.credit.manager.log.action;

import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import  com.strongit.credit.manager.log.po.LogInfo;

/**
 * 
 * 
@author  lanjh
 * @ 2008-10-20  上午10:18:04
 *
 
*/
public   class  LogManage  extends  HibernateDaoSupport {
    
/**
     * 保存日志信息
     * 
@param  joinpoint
     * 
@return
     
*/
    
public   boolean  getLog(final LogInfo logInfo) {
        
boolean  flag  =   false ;
        LogInfo logInfo 
=   null ;
        
try  {
      
       this .getHibernateTemplate().save(logInfo);
            
this .getHibernateTemplate().flush();
            flag 
=   true ;
        } 
catch  (Exception e) {
            flag 
=   false ;
            e.printStackTrace();
        }
        
return  flag;
    }
}


3 . spring 配置文件
<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xmlns:jee
="http://www.springframework.org/schema/jee"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
>
    
    
<!--  AOP控制 角色管理 日志管理  -->
    
< bean  id ="log"  class ="com.strongit.credit.manager.log.action.LogManage" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory" />
        
</ property >
    
</ bean >     
    
    
<!--  AOP控制 树形管理  -->
    
<!--  增加 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="treeInsertMethods"  expression ="execution(* com.strongit.credit.manager.treemanager.service.*.add*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="treeInsertMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  编辑  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="treeUpdateMethods"  expression ="execution(* com.strongit.credit.manager.treemanager.service.*.update*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="treeUpdateMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  删除  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="treeDeleteMethods"  expression ="execution(* com.strongit.credit.manager.treemanager.service.*.delete*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="treeDeleteMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
    
<!--  AOP控制 信息管理模块  -->
    
<!--  增加 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="infoInsertMethods"  expression ="execution(* com.strongit.credit.manager.infomanager.service.*.insert*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="infoInsertMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  编辑  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="infoUpdateMethods"  expression ="execution(* com.strongit.credit.manager.infomanager.service.*.update*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="infoUpdateMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  删除  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="infoDeleteMethods"  expression ="execution(* com.strongit.credit.manager.infomanager.service.*.delete*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="infoDeleteMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  审核与消审  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="infoIsauditMethods"  expression ="execution(* com.strongit.credit.manager.infomanager.service.*.isaudit(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="infoIsauditMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
    
<!--  AOP控制 意见建议模块  -->
    
<!--  回复  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="recommandUpdateMethods"  expression ="execution(* com.strongit.credit.manager.recommandmanager.service.*.update*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="recommandUpdateMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  删除  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="recommandDeleteMethods"  expression ="execution(* com.strongit.credit.manager.recommandmanager.service.*.delete*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="recommandDeleteMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  审核与消审  -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="recommandIsauditMethods"  expression ="execution(* com.strongit.credit.manager.recommandmanager.service.*.isaudit*(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="recommandIsauditMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
    
    
<!--  AOP控制 企业账号管理  -->
    
<!--  增加 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="memberInsertMethods"  expression ="execution(* com.strongit.credit.manager.membermanager.service.*.save(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="memberInsertMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  删除 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="memberDeleteMethods"  expression ="execution(* com.strongit.credit.manager.membermanager.service.*.delete(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="memberDeleteMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  修改 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="memberUpdateMethods"  expression ="execution(* com.strongit.credit.manager.membermanager.service.*.update(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="memberUpdateMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
    
<!--  AOP控制 个人账号管理  -->
    
<!--  增加 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="personalInsertMethods"  expression ="execution(* com.strongit.credit.manager.personalmanager.service.*.save(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="personalInsertMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  删除 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="personalDeleteMethods"  expression ="execution(* com.strongit.credit.manager.personalmanager.service.*.delete(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="personalDeleteMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  更新 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="personalUpdateMethods"  expression ="execution(* com.strongit.credit.manager.personalmanager.service.*.update(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="personalUpdateMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
    
    
<!--  AOP控制 投票主题管理  -->
    
<!--  增加 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="themeInsertMethods"  expression ="execution(* com.strongit.credit.manager.poll.service.*.save(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="themeInsertMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  删除 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="themeDeleteMethods"  expression ="execution(* com.strongit.credit.manager.poll.service.*.del(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="themeDeleteMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
<!--  更新 -->
    
< aop:config >
        
< aop:aspect  ref ="log" >
            
< aop:pointcut  id ="themeUpdateMethods"  expression ="execution(* com.strongit.credit.manager.poll.service.*.isaudit(..))" />
            
< aop:after-returning  method ="getLog"  pointcut-ref ="themeUpdateMethods"  returning ="retVal" />
        
</ aop:aspect >
    
</ aop:config >
    
    
</ beans >


4 . gg

5、no gg,,,go on 。 by lanjh  2011.9.8

另外还可以采用一种方式

   
/**
     * <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="AllCreateMethods" expression="execution(* com.strongit..service.*.create*(..))"/>
            <aop:around method="recordLog1"  pointcut-ref="AllCreateMethods"/>
        </aop:aspect>
    </aop:config>
     * 
@param  logInfo
     
*/
    
public   void  recordLog1( final  ProceedingJoinPoint joinpoint) {

        
try  {
        
this .getHibernateTemplate().execute( new  HibernateCallback() {
            
public  Object doInHibernate(Session session)
                    
throws  HibernateException, SQLException {
                
try  {
                    session.save((StrongLogInfo) joinpoint.proceed());
                } 
catch  (Throwable e) {
                    
//  TODO Auto-generated catch block
                    e.printStackTrace();
                }
                session.flush();
                
return   null ;
            }
        });
    } 
catch  (Exception e) {
        e.printStackTrace();
    }

你可能感兴趣的:(Spring AOP控制日志管理)