Spring -aop 實現系統日誌
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);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.