Spring -aop 实现系统日志
application.xml文件中配置一个aop:
<!-- 系统日志操作拦截器 -->
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="com.cheyou.action.LogAspect"/>
<aop:config>
<aop:aspect ref="logAspect">
<aop:pointcut id="logPointCut" expression="execution(* com.cheyou.service..*(..))"/>
<aop:around pointcut-ref="logPointCut" method="doSystemLog"/>
</aop:aspect>
</aop:config>
说明:这里拦截可以是在action层拦截也可以在service层,我选择的是在service层,先说明下他们的区别,在action拦截时是不用去考虑拦截的方法的参数的,他们都是没参数的,但是有一个问题是他们应该是继承了action的ActionSupport,每次拦截的时候都是拦截的他们上级,这也是我选择拦截service层的原因,当然也有解决方法,拦截service层就要去判断方法的参数问题。
在action层创建一个记录日志的action:
public class LogAspect extends CitCoreAction{
@Resource
private ISystemLogService systemLogService;
private Log logger = LogFactory.getLog(LogAspect.class);
public Object doSystemLog(ProceedingJoinPoint point) throws Throwable {
String methodName = point.getSignature().getName();
try {
if(methodName!=null && !"".equals(methodName)){
if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {
Class targetClass = point.getTarget().getClass();
Object[] p = point.getArgs();
Method method = null;
Method[] methodS = targetClass.getMethods();
List<Method> ms = new ArrayList<Method>();
for (Method method2 : methodS) {
if (method2.getName().equals(methodName)) {
ms.add(method2);
}
}
if(ms.size()>=1){
//这里判断参数个数
for(Method m : ms){
Class[] a = m.getParameterTypes();
if(p.length == a.length){
//验证参数的类型和顺序
// int j = 0;
// for(int i=0;i<p.length;i++){
// System.out.println(p[i].getClass().toString());
// System.out.println(a[i].getClass().getName().toString());
// if (p[i].getClass().getName().toString().equals(a[i].getClass().getName().toString())) {
// j++;
//
// }
// }
// if(j==p.length){
//
// }
method = m;
}
}
}
if (method != null) {
boolean hasAnnotation = method.isAnnotationPresent(Action.class);
if (hasAnnotation) {
Action annotation = method.getAnnotation(Action.class);
String methodDescp = annotation.description();
if (logger.isDebugEnabled()) {
logger.debug("Action method:" + method.getName() + " Description:" + methodDescp);
}
//取到当前的操作用户
HttpServletRequest request = ServletActionContext.getRequest();
User user = (User)request.getSession().getAttribute("loginUser");
user = new User();
user.setUserName("aa");
if(user!=null){
try{
SystemLog sysLog=new SystemLog();
sysLog.setCreatetime(new Date().getTime()+"");
sysLog.setUserId(user.getPid());
sysLog.setUsername(user.getUserName());
sysLog.setOperation(methodDescp);
systemLogService.save(sysLog);
}catch(Exception ex){
logger.error(ex.getMessage());
}
}
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
return point.proceed();
}
}
最后就是在每个service层的方法上以注解的方式增加说明文字,比如:
public class AddActiveserviceImpl implements AddActiveService{
@Autowired
AddActiveDao addActiveDao;
@Action(description="增加活动")
public boolean addActive(ActiveInfo activeInfo) {
// TODO Auto-generated method stub
return addActiveDao.addActive(activeInfo);
}
public List<ActiveInfo> getActiveList() {
// TODO Auto-generated method stub
return addActiveDao.getActiveList();
}
@Action(description="控制活动可以报名")
public boolean controlActivePay(String activeId, String swicth) {
// TODO Auto-generated method stub
return addActiveDao.controlActivePay(activeId, swicth);
}
@Action(description="控制用户不能取消报名")
public boolean controlCarUserNoCancel(String CarUserId) {
// TODO Auto-generated method stub
return addActiveDao.controlCarUserNoCancel(CarUserId);
}
/* (non-Javadoc)
* @see com.cheyou.service.active.AddActiveService#endActive()
*/
@Action(description="结束活动")
public boolean endActive(String activeId) {
// TODO Auto-generated method stub
return addActiveDao.endActive(activeId);
}
@Action(description="删除打卡信息")
public String deleteCarActive(String activeId, String carUserId) {
// TODO Auto-generated method stub
return addActiveDao.deleteCarActive(activeId, carUserId);
}
@Action(description="控制活动可以开始打卡")
public boolean controlActiveCanSign(String activeId) {
// TODO Auto-generated method stub
return addActiveDao.controlActiveCanSign(activeId);
}
}
application.xml文件中配置一个aop:
<!-- 系统日志操作拦截器 -->
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="com.cheyou.action.LogAspect"/>
<aop:config>
<aop:aspect ref="logAspect">
<aop:pointcut id="logPointCut" expression="execution(* com.cheyou.service..*(..))"/>
<aop:around pointcut-ref="logPointCut" method="doSystemLog"/>
</aop:aspect>
</aop:config>
说明:这里拦截可以是在action层拦截也可以在service层,我选择的是在service层,先说明下他们的区别,在action拦截时是不用去考虑拦截的方法的参数的,他们都是没参数的,但是有一个问题是他们应该是继承了action的ActionSupport,每次拦截的时候都是拦截的他们上级,这也是我选择拦截service层的原因,当然也有解决方法,拦截service层就要去判断方法的参数问题。
在action层创建一个记录日志的action:
public class LogAspect extends CitCoreAction{
@Resource
private ISystemLogService systemLogService;
private Log logger = LogFactory.getLog(LogAspect.class);
public Object doSystemLog(ProceedingJoinPoint point) throws Throwable {
String methodName = point.getSignature().getName();
try {
if(methodName!=null && !"".equals(methodName)){
if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {
Class targetClass = point.getTarget().getClass();
Object[] p = point.getArgs();
Method method = null;
Method[] methodS = targetClass.getMethods();
List<Method> ms = new ArrayList<Method>();
for (Method method2 : methodS) {
if (method2.getName().equals(methodName)) {
ms.add(method2);
}
}
if(ms.size()>=1){
//这里判断参数个数
for(Method m : ms){
Class[] a = m.getParameterTypes();
if(p.length == a.length){
//验证参数的类型和顺序
// int j = 0;
// for(int i=0;i<p.length;i++){
// System.out.println(p[i].getClass().toString());
// System.out.println(a[i].getClass().getName().toString());
// if (p[i].getClass().getName().toString().equals(a[i].getClass().getName().toString())) {
// j++;
//
// }
// }
// if(j==p.length){
//
// }
method = m;
}
}
}
if (method != null) {
boolean hasAnnotation = method.isAnnotationPresent(Action.class);
if (hasAnnotation) {
Action annotation = method.getAnnotation(Action.class);
String methodDescp = annotation.description();
if (logger.isDebugEnabled()) {
logger.debug("Action method:" + method.getName() + " Description:" + methodDescp);
}
//取到当前的操作用户
HttpServletRequest request = ServletActionContext.getRequest();
User user = (User)request.getSession().getAttribute("loginUser");
user = new User();
user.setUserName("aa");
if(user!=null){
try{
SystemLog sysLog=new SystemLog();
sysLog.setCreatetime(new Date().getTime()+"");
sysLog.setUserId(user.getPid());
sysLog.setUsername(user.getUserName());
sysLog.setOperation(methodDescp);
systemLogService.save(sysLog);
}catch(Exception ex){
logger.error(ex.getMessage());
}
}
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
return point.proceed();
}
}
最后就是在每个service层的方法上以注解的方式增加说明文字,比如:
public class AddActiveserviceImpl implements AddActiveService{
@Autowired
AddActiveDao addActiveDao;
@Action(description="增加活动")
public boolean addActive(ActiveInfo activeInfo) {
// TODO Auto-generated method stub
return addActiveDao.addActive(activeInfo);
}
public List<ActiveInfo> getActiveList() {
// TODO Auto-generated method stub
return addActiveDao.getActiveList();
}
@Action(description="控制活动可以报名")
public boolean controlActivePay(String activeId, String swicth) {
// TODO Auto-generated method stub
return addActiveDao.controlActivePay(activeId, swicth);
}
@Action(description="控制用户不能取消报名")
public boolean controlCarUserNoCancel(String CarUserId) {
// TODO Auto-generated method stub
return addActiveDao.controlCarUserNoCancel(CarUserId);
}
/* (non-Javadoc)
* @see com.cheyou.service.active.AddActiveService#endActive()
*/
@Action(description="结束活动")
public boolean endActive(String activeId) {
// TODO Auto-generated method stub
return addActiveDao.endActive(activeId);
}
@Action(description="删除打卡信息")
public String deleteCarActive(String activeId, String carUserId) {
// TODO Auto-generated method stub
return addActiveDao.deleteCarActive(activeId, carUserId);
}
@Action(description="控制活动可以开始打卡")
public boolean controlActiveCanSign(String activeId) {
// TODO Auto-generated method stub
return addActiveDao.controlActiveCanSign(activeId);
}
}