利用spring AOP注解实现日志管理
生活随笔
收集整理的這篇文章主要介紹了
利用spring AOP注解实现日志管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近剛接手一個項目,在項目的開始階段,我們的架構師分配了個任務給我,讓我利用spring的切面技術做一個日志管理的案例。要求很簡單,就是需要記錄:誰在什么時候對誰做了什么操作,同時在日志的描述中還要有修改前后內容的對比。話說兩句話是挺簡單的,但是對于對spring只有初學者水平的我還是有點難度的,于是各種百度,各種谷歌,各種想。終于在這篇http://kaowww153.iteye.com/blog/603891博客中得到啟發,但是里面的內容寫的還是不夠完整,和我的需求也有些不一樣。經過多次的修改和測試,終于成功通過。今天閑來無事,寫個博文,以備下次需要做好筆記。話不多說,把過程寫下:
1.首先創建一個自定義注解
package com.cstp.custom.interfaces;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RecordLog {
}
2.寫一個過濾方法,這個方法是實現MethodIntherceptor接口的。
package com.cstp.custom.interfaces;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.cstp.jdbc.dao.systemLogDao;
import com.cstp.jdbc.dao.impl.systemLogDaoImpl;
import com.cstp.jdbc.vo.SystemLog;
import com.cstp.jdbc.vo.User;
public class LogAroundInterceptor implements MethodInterceptor {
private systemLogDao systemLogDao = new systemLogDaoImpl();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
Object returnObject;
Object newObj = null;
Object operatObj = null;
HttpServletRequest request = null;
Method method = invocation.getMethod();
if (method.isAnnotationPresent(RecordLog.class)) {
Object[] arguments = invocation.getArguments();
if (null != arguments) {
newObj = arguments[0];//新對象
operatObj = arguments[1];//操作人
if (arguments[2] instanceof HttpServletRequest) {
request = (HttpServletRequest)arguments[2]; //通過session把舊對象傳過來作為新舊對象的比較
}
SystemLog systemLog = getSysLog(getOperaType(method.getName()), newObj, operatObj, request);
System.out.println(systemLog);
systemLogDao.addLog(systemLog);
}
returnObject = invocation.proceed(); // 執行被攔截的方法
} else {
returnObject = invocation.proceed(); // 執行被攔截的方法
}
return returnObject;
}
public int getOperaType(String methodName) {
int type = 0;
if (methodName.startsWith("save") || methodName.startsWith("add") || methodName.startsWith("insert")) {
type = 1;//增
} else if (methodName.startsWith("delete") || methodName.startsWith("del")) {
type = 2;//刪
} else if (methodName.startsWith("update")){
type = 3;//改
}
return type;
}
public SystemLog getSysLog(int type, Object newObj, Object operator,HttpServletRequest request) {
StringBuffer sysContent = new StringBuffer();
SystemLog systemLog = new SystemLog();
Object oldObject = request.getSession().getAttribute("oldUser");
if (type != 1 && type != 2 && type != 3) {
System.out.println("此方法不能被記錄日志");
return null;
}
if (newObj instanceof User) {
User user = (User) newObj;
sysContent.append("用戶:");
if (type == 1) {
sysContent.append(user.getName()).append("被保存.");
} else if (type == 2) {
// sysContent.append(user.getDeleteUser()).append(
// "刪除了用戶:" + user.getUniqueUserName());
}
}
return systemLog;
}
}
3.applicationContext.xml里配置
<bean id="common" class="com.cstp.jdbc.test.Common">
</bean>
<aop:config>
<aop:pointcut id="logPointCuts"
expression="execution(* com.cstp.jdbc.test.*.*(..))" />
<aop:advisor pointcut-ref="logPointCuts" advice-ref="springMethodInterceptor" />
</aop:config>
<bean id="springMethodInterceptor" class="com.cstp.custom.interfaces.LogAroundInterceptor">
</bean>
</beans>
4.我的實體類common
@RecordLog
public void update(User user, User opeartor, HttpServletRequest request) {
UserDao userDao = new UserDaoImpl();
userDao.updateUser(user);
System.out.println("修改成功");
}
5.在servlet測試
UserDao userDao = new UserDaoImpl();
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Common c = (Common) factory.getBean("common");
User user = new User();
user.setId(2);
user.setName("杰");
user.setPassword("123456");
user.setAge(18);
user.setSex("女");
User opeartor = userDao.findAllUserById(3);
User oldUser = userDao.findAllUserById(2);
HttpSession session = request.getSession();
session.setAttribute("oldUser", oldUser);
c.update(user, opeartor, request);
測試通過,我所需要的新舊對象,以及操作人,時間,描述等都可以在getSysLog()方法中獲取,至于如何添加就簡單啦,就不寫上了。
6.使用方法很簡單了:在需要添加日志的方法上加上我們的注解@RecordLog就可以了(就像我common類里的update方法一樣)。
1.首先創建一個自定義注解
package com.cstp.custom.interfaces;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RecordLog {
}
2.寫一個過濾方法,這個方法是實現MethodIntherceptor接口的。
package com.cstp.custom.interfaces;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.cstp.jdbc.dao.systemLogDao;
import com.cstp.jdbc.dao.impl.systemLogDaoImpl;
import com.cstp.jdbc.vo.SystemLog;
import com.cstp.jdbc.vo.User;
public class LogAroundInterceptor implements MethodInterceptor {
private systemLogDao systemLogDao = new systemLogDaoImpl();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
Object returnObject;
Object newObj = null;
Object operatObj = null;
HttpServletRequest request = null;
Method method = invocation.getMethod();
if (method.isAnnotationPresent(RecordLog.class)) {
Object[] arguments = invocation.getArguments();
if (null != arguments) {
newObj = arguments[0];//新對象
operatObj = arguments[1];//操作人
if (arguments[2] instanceof HttpServletRequest) {
request = (HttpServletRequest)arguments[2]; //通過session把舊對象傳過來作為新舊對象的比較
}
SystemLog systemLog = getSysLog(getOperaType(method.getName()), newObj, operatObj, request);
System.out.println(systemLog);
systemLogDao.addLog(systemLog);
}
returnObject = invocation.proceed(); // 執行被攔截的方法
} else {
returnObject = invocation.proceed(); // 執行被攔截的方法
}
return returnObject;
}
public int getOperaType(String methodName) {
int type = 0;
if (methodName.startsWith("save") || methodName.startsWith("add") || methodName.startsWith("insert")) {
type = 1;//增
} else if (methodName.startsWith("delete") || methodName.startsWith("del")) {
type = 2;//刪
} else if (methodName.startsWith("update")){
type = 3;//改
}
return type;
}
public SystemLog getSysLog(int type, Object newObj, Object operator,HttpServletRequest request) {
StringBuffer sysContent = new StringBuffer();
SystemLog systemLog = new SystemLog();
Object oldObject = request.getSession().getAttribute("oldUser");
if (type != 1 && type != 2 && type != 3) {
System.out.println("此方法不能被記錄日志");
return null;
}
if (newObj instanceof User) {
User user = (User) newObj;
sysContent.append("用戶:");
if (type == 1) {
sysContent.append(user.getName()).append("被保存.");
} else if (type == 2) {
// sysContent.append(user.getDeleteUser()).append(
// "刪除了用戶:" + user.getUniqueUserName());
}
}
return systemLog;
}
}
3.applicationContext.xml里配置
<bean id="common" class="com.cstp.jdbc.test.Common">
</bean>
<aop:config>
<aop:pointcut id="logPointCuts"
expression="execution(* com.cstp.jdbc.test.*.*(..))" />
<aop:advisor pointcut-ref="logPointCuts" advice-ref="springMethodInterceptor" />
</aop:config>
<bean id="springMethodInterceptor" class="com.cstp.custom.interfaces.LogAroundInterceptor">
</bean>
</beans>
4.我的實體類common
@RecordLog
public void update(User user, User opeartor, HttpServletRequest request) {
UserDao userDao = new UserDaoImpl();
userDao.updateUser(user);
System.out.println("修改成功");
}
5.在servlet測試
UserDao userDao = new UserDaoImpl();
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Common c = (Common) factory.getBean("common");
User user = new User();
user.setId(2);
user.setName("杰");
user.setPassword("123456");
user.setAge(18);
user.setSex("女");
User opeartor = userDao.findAllUserById(3);
User oldUser = userDao.findAllUserById(2);
HttpSession session = request.getSession();
session.setAttribute("oldUser", oldUser);
c.update(user, opeartor, request);
測試通過,我所需要的新舊對象,以及操作人,時間,描述等都可以在getSysLog()方法中獲取,至于如何添加就簡單啦,就不寫上了。
6.使用方法很簡單了:在需要添加日志的方法上加上我們的注解@RecordLog就可以了(就像我common類里的update方法一樣)。
總結
以上是生活随笔為你收集整理的利用spring AOP注解实现日志管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不倒翁原理
- 下一篇: 男弓箭手名字好听,网游弓箭手的名字大全4