aop是sPRing框架的兩大核心之一,即面向切面編程,它是對oo編程的補(bǔ)充。那么spring aop在實(shí)際項(xiàng)目開發(fā)中有什么作用呢?它最大的作用就是完成日志記錄,一個(gè)簡單的例子,你在ATM上取款系統(tǒng)是會記錄下來一些信息的,比如取款時(shí)間、銀行卡號、ATM位置、等等。不管是一般還是不一般的項(xiàng)目,應(yīng)該來說,只要是敏感的數(shù)據(jù),只要用戶對它進(jìn)行操作了,牽扯到數(shù)據(jù)庫了,系統(tǒng)都應(yīng)該以日志的形式記錄下來。有日志記錄功能,當(dāng)用戶對數(shù)據(jù)進(jìn)行“非法”操作的時(shí)候,spring aop會很容易幫你找到當(dāng)事人。
使用spring aop開發(fā)日志管理相當(dāng)常見,現(xiàn)在幾乎可以說每個(gè)業(yè)務(wù)系統(tǒng)都需要有日志管理模塊,有的叫審計(jì),間接地和安全掛上了鉤。 1、定義Log實(shí)體
import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "SYS_LOG")public class Log { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Integer id; /** * 操作賬戶 * */ @Column(name = "USERUID") private String useruid; /** * 操作用戶名稱 * */ @Column(name = "USERNAME") private String username; /** * 操作位置 * */ @Column(name = "OPPOSIT") private String opposit; /** * 操作內(nèi)容 * */ @Column(name = "OPCONT") private String opcont; /** * 操作時(shí)間 * */ @Column(name = "CREATETIME") private Date createtime; //setters and getters}2、自定義注解AnnotationLog
import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface AnnotationLog { /** * 功能說明:日志名 * @return */ String name() default ""; /** * 功能說明:日志描述 * @return */ String description() default "";}其中@Target等4個(gè)注解的作用

3、實(shí)現(xiàn)AOPLog
import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.beans.factory.annotation.Autowired;import com.onenzg.common.hibernate.HibernateDAO;import com.onezg.common.log.domain.Log;import com.onezg.common.log.service.LogService;import com.onezg.sys.usermanage.domain.UserInfo;import com.onezg.sys.usermanage.services.UserServices;public class AopLog extends HibernateDAO<Log> { /** * 引入LogService */ @Autowired private LogService logservice; @Autowired private UserServices userServices; /** * 功能說明:輸出log信息 */ public void writeLogInfo(JoinPoint point) throws Throwable { Method method = ((MethodSignature) point.getSignature()).getMethod(); AnnotationLog ann = method.getAnnotation(AnnotationLog.class); if (ann != null) { UserInfo cui = userServices.getCurrentUser(); if (null != cui) { Map<String, Object> map = new HashMap<String, Object>(); map.put("uid", cui.getuId()); map.put("givenName", cui.getuName()); logservice.insertLog(ann.name(), ann.description(), map); } } }}4、LogDAO和LogDAOImpl
import java.util.Map;public interface LogDao { /** * 功能說明:AOP保存日志信息 * @param strLocation 參數(shù) * @param strContent 參數(shù) * @param userInfo 要保存的信息集合 */ void insertLog(String strLocation, String strContent, @SuppressWarnings("rawtypes") Map userInfo);}import java.util.Date;import java.util.Map;import org.springframework.stereotype.Repository;import com.onezg.common.exception.DatabaseException;import com.onezg.common.hibernate.HibernateDAO;import com.onezg.common.log.dao.LogDao;import com.onezg.common.log.domain.Log;@Repository(value = "logdao")public class LogDaoImpl extends HibernateDAO<Log> implements LogDao { @SuppressWarnings("rawtypes") @Override public void insertLog(String strLocation, String strContent, Map userInfo) { //useruid String useruid = String.valueOf(userInfo.get("uid")); //username, 操作賬戶. String userName = String.valueOf(userInfo.get("givenName")); //opposit, 操作位置. String opposit = strLocation; //opcont, 操作內(nèi)容. String opcont = strContent; try { Log log = new Log(); //bean對象 //封裝bean數(shù)據(jù)處理 log.setUseruid(useruid); log.setUsername(userName); log.setOpposit(opposit); log.setOpcont(opcont); log.setCreatetime(new Date()); //保存對象 this.save(log); } catch (Exception ex) { throw new DatabaseException("插入登錄日志失敗", ex.getMessage()); } }}5、配置文件寫spring aop配置,本人例子是在Spring-context.xml里寫配置
<!-- Aop配置——開始 --> <aop:config proxy-target-class="true"> <aop:aspect id="goLogAspect" ref="AfterReturningAdvice"> <aop:pointcut id="actionPointcut" expression="within(com.onezg..*)" /> <aop:before pointcut-ref="actionPointcut" method="writeLogInfo" /> </aop:aspect> </aop:config> <beans:bean id="AfterReturningAdvice" class="com.onezg.common.aop.AopLog"></beans:bean> <!-- Aop配置——結(jié)束 -->6、使用注解
@AnnotationLog(name = "用戶管理", description = "修改用戶") @RequestMapping(value = "/userupdate") public void getUserUpdate(Httpsession session, HttpServletRequest request, HttpServletResponse response, Model model, UserInfoDTO dto) { try { userServices.updateUser(dto); } catch (Exception e) { throw new ParameterException("用戶修改失敗", e.getMessage()); } }到這里,使用spring aop完成日志記錄就結(jié)束了。
新聞熱點(diǎn)
疑難解答