今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理。废话不多说,直接开始!
关于配置我还是的再说一遍。
在applicationContext-mvc.xml中要添加的
<mvc:annotation-driven />
<!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->
<context:component-scan base-package="com.gcx" />
<!-- 启动对@AspectJ注解的支持 -->
<!-- proxy-target-class等于true是强制使用cglib代理,proxy-target-class默认是false,如果你的类实现了接口 就走JDK代理,如果没有,走cglib代理 -->
<!-- 注:对于单利模式建议使用cglib代理,虽然JDK动态代理比cglib代理速度快,但性能不如cglib -->
<!--如果不写proxy-target-class="true"这句话也没问题-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--切面-->
<bean></bean>
接下来开始编写代码。
创建日志类实体
public class SystemLog {
private String id;
private String description;
private String method;
private Long logType;
private String requestIp;
private String exceptioncode;
private String exceptionDetail;
private String params;
private String createBy;
private Date createDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method == null ? null : method.trim();
}
public Long getLogType() {
return logType;
}
public void setLogType(Long logType) {
this.logType = logType;
}
public String getRequestIp() {
return requestIp;
}
public void setRequestIp(String requestIp) {
this.requestIp = requestIp == null ? null : requestIp.trim();
}
public String getExceptioncode() {
return exceptioncode;
}
public void setExceptioncode(String exceptioncode) {
this.exceptioncode = exceptioncode == null ? null : exceptioncode.trim();
}
public String getExceptionDetail() {
return exceptionDetail;
}
public void setExceptionDetail(String exceptionDetail) {
this.exceptionDetail = exceptionDetail == null ? null : exceptionDetail.trim();
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params == null ? null : params.trim();
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy == null ? null : createBy.trim();
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
编写dao接口
package com.gcx.dao;
import com.gcx.entity.SystemLog;
public interface SystemLogMapper {
int deleteByPrimaryKey(String id);
int insert(SystemLog record);
int insertSelective(SystemLog record);
SystemLog selectByPrimaryKey(String id);
int updateByPrimaryKeySelective(SystemLog record);
int updateByPrimaryKey(SystemLog record);
}
编写service层
package com.gcx.service;
import com.gcx.entity.SystemLog;
public interface SystemLogService {
int deleteSystemLog(String id);
int insert(SystemLog record);
int insertTest(SystemLog record);
SystemLog selectSystemLog(String id);
int updateSystemLog(SystemLog record);
}
编写service实现类serviceImpl
package com.gcx.service.impl;
import Javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.gcx.annotation.Log;
import com.gcx.dao.SystemLogMapper;
import com.gcx.entity.SystemLog;
import com.gcx.service.SystemLogService;
@Service("systemLogService")
public class SystemLogServiceImpl implements SystemLogService {