初识Spring的AOP,在项目中用到了其日志功能: AOP的强大是大家所共识的,

        事务的处理,日志的操作,权限的分配...有了这么多已经足够了,已经可以让他在所有的框架面前独树一帜,原来需要那么复杂的处理逻辑,现在有了AOP 如此的简单化,确实能看出其实力

        1:建立日志表

        create table     loginfo(
    id int auto_increment primary key ,
    author varchar(20),
    marktime varchar(50),
    action varchar(50),
    isok varchar(20)
);

        2:在service层中建立对表loginfo的操作逻辑处理类LoginfoServiceImpl:

        package com.viita.service;

import java.util.List;


import com.viita.dao.LoginfoDAO;
import com.viita.entity.Loginfo;

public class LoginfoServiceImpl implements ILoginfoService{



    private LoginfoDAO dao;
    
    public void delete(Loginfo info) {

        // TODO Auto-generated method stub
        dao.delete(info);
    }

    public List findAll() {

        // TODO Auto-generated method stub
        return dao.findAll();
    }

    public Loginfo findById(int id) {

        // TODO Auto-generated method stub
        return dao.findById(id);
    }

    public void save(Loginfo info) {

        // TODO Auto-generated method stub
        dao.save(info);
    }

    public void setDao(LoginfoDAO dao) {

        this.dao = dao;
    }

}

        3:接着建立代理类 LogAop

        package com.viita.common;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;

import com.viita.entity.Loginfo;
import com.viita.service.LoginfoServiceImpl;

public class LogAop {


    private LoginfoServiceImpl service;

    public void setService(LoginfoServiceImpl service) {

        this.service = service;
    }
    
    public Object write(ProceedingJoinPoint p) throws Throwable
    {

        String userName = UserLocal.getUser();
        Loginfo log = new Loginfo();
        if(userName!=null)
        {

            log.setAuthor(userName);
        }else
        {

            log.setAuthor("");
        }
        log.setMarktime(getCurrentTime());
        
        Method method = null;
        
        Method[] methods = p.getTarget().getClass().getDeclaredMethods();
        for (Method method2 : methods) {

            if(p.getSignature().getName().equals(method2.getName()))
            {

                method = method2;
                break;
            }
        }
        LogDesc desc = method.getAnnotation(LogDesc.class);
        if(desc!=null)
            log.setAction(desc.actionName());
        Object o = null;
        try
        {

            o = p.proceed();
            log.setIsok("成功!");
        }catch(Exception e)
        {

            log.setIsok("失败");
            throw new Throwable(e.toString());
        }
        if(log.getAction()!=null)
            service.save(log);
        return o;
    }
    
    public static String getCurrentTime()
    {

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH小时mm分ss秒");
        return sdf.format(d);
    }
}
                还有其中调用的UserLocal,用于获取用户名:

        

package com.
viita.
common;


public 
class UserLocal {

    
public 
static ThreadLocal user = 
new ThreadLocal();

    

    
public 
static 
void setUser(String userName)

    {

        user.
set(userName);

    }

    

    
public 
static String getUser()

    {

        
return (String)user.
get();

    }

}

 

        建立脚注类LogDesc:

package com.viita.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

public @interface LogDesc {


    String actionName();
}

        去登陆的action中获取用户名     UserLocal.setUser(loginForm.getInfo().getUserName());

        接着在 applicationContext.xml 中对其关系进行描述:

        先换掉xml的头文件:

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-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/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

然后在其中描述代理类与被代理者之间的关系

        <bean id="LogAop" class="com.viita.common.LogAop">
        <property name="service">
            <ref bean="LoginfoServiceImpl"/>
        </property>
    </bean>
    
    
<aop:config>
        <aop:aspect ref="LogAop">
            <aop:pointcut id="mp" expression="execution(* com.viita.service.*.*(..))"/>
            <aop:around pointcut-ref="mp" method="write" />
        </aop:aspect>
    </aop:config>

最后就可以在service中对需要加入日志的方法前加入脚注: @LogDesc(actionName="登陆")

至此 AOP的日志功能就完成了!