首页

通过Spring事件监听器ApplicationListener实现通用日志组件设计依赖关系图及完整的代码示例(类图)

标签:springframework,ApplicationEvent,ApplicationEventPublisherAware,ApplicationListener,事件监听器,日志组件,设计类图     发布时间:2017-07-04   

一、前言

基于spring的org.springframework.context.ApplicationListener事件监听器、org.springframework.context.ApplicationEvent事件对象、org.springframework.context.ApplicationEventPublisherAware注册事件源三个依赖基类实现通用日志组件,在不同业务模块服务(如UserService用户服务模块中)中按照注册不同事件行为来记录日志。

二、代码示例

1. 如上所述,主要代码类依赖图如下所示,完整的源码下载

通过Spring事件监听器ApplicationListener实现通用日志组件设计依赖关系图及完整的代码示例(类图)

2. SecurityManager类 - 注册发布事件源,代码如下

import org.springframework.context.ApplicationEvent;@b@import org.springframework.context.ApplicationEventPublisher;@b@import org.springframework.context.ApplicationEventPublisherAware;@b@import com.xwood.test.log.entity.ILog;@b@@b@public class SecurityManager implements ApplicationEventPublisherAware {@b@	@b@	private static ApplicationEventPublisher eventPublisher;@b@	@b@	@b@	public void setApplicationEventPublisher(@b@			ApplicationEventPublisher applicationEventPublisher) {@b@		@b@		eventPublisher = applicationEventPublisher;@b@	}@b@	@b@	public static void publishEvent(ApplicationEvent event){@b@		eventPublisher.publishEvent(event);@b@	}@b@	@b@	public static void log(ILog log){@b@		eventPublisher.publishEvent(new LogEvent(log));@b@	}@b@	 @b@}

用户服务类UserService,代码如下

import org.springframework.beans.factory.annotation.Autowired;@b@import com.xwood.test.log.LogHelper;@b@import com.xwood.test.log.entity.User;@b@import com.xwood.test.log.persist.IPersistDao;@b@@b@@b@public class UserService implements IUserService {@b@@b@	private IPersistDao dao;@b@	@b@	public IPersistDao getDao() {@b@		return dao;@b@	}@b@	@Autowired@b@	public void setDao(IPersistDao dao) {@b@		this.dao = dao;@b@	}@b@	@b@	public String create(User u) {@b@		onAdd(u);@b@		return null;@b@	}@b@	@b@	public  void delete(User u){@b@		onDelete(u);@b@	}@b@	@b@	/*----- 发布事件  -----*/@b@	protected void onAdd(User user){@b@		LogHelper.actionLog("add","", user);@b@	}@b@	@b@	protected void onUpdate(User old, User user){@b@		LogHelper.actionLog("update","", user);@b@	}@b@	@b@	protected void onDelete(User user){@b@		LogHelper.actionLog("delete","", user);@b@	}@b@	@b@	 @b@}

LogEvent类 - 事件对象,代码如下

import org.springframework.context.ApplicationEvent;@b@@b@import com.woopa.sf.log.ILog;@b@ @b@public class LogEvent extends ApplicationEvent {@b@	@b@	private static final long serialVersionUID = 8807576919145507907L;@b@@b@	public LogEvent(ILog log) {@b@		super(log);@b@	}@b@}

3. 事件监听器 - ActionLogAppender类,代码如下

import com.xwood.test.log.entity.ActionLog;@b@import com.xwood.test.log.entity.ILog;@b@import com.xwood.test.log.service.IActionLogService;@b@@b@public class ActionLogAppender implements ILogAppender {@b@@b@	private IActionLogService actionLogService;@b@	@b@	public IActionLogService getActionLogService() {@b@		return actionLogService;@b@	}@b@	@b@	public void setActionLogService(IActionLogService actionLogService) {@b@		this.actionLogService = actionLogService;@b@	}@b@	public void append(ILog log) {@b@		actionLogService.save((ActionLog) log);@b@	}@b@@b@	@SuppressWarnings("rawtypes")@b@	public boolean supports(Class logClass) {@b@		return ActionLog.class.isAssignableFrom(logClass);@b@	}@b@@b@}

ILogAppender类 

import com.xwood.test.log.entity.ILog;@b@@b@public interface ILogAppender {@b@	 @b@	void append(ILog log);@b@	@b@}

ActionLogAppender类

import com.xwood.test.log.entity.ActionLog;@b@import com.xwood.test.log.entity.ILog;@b@import com.xwood.test.log.service.IActionLogService;@b@@b@public class ActionLogAppender implements ILogAppender {@b@@b@	private IActionLogService actionLogService;@b@	@b@	public IActionLogService getActionLogService() {@b@		return actionLogService;@b@	}@b@	@b@	public void setActionLogService(IActionLogService actionLogService) {@b@		this.actionLogService = actionLogService;@b@	}@b@	public void append(ILog log) {@b@		actionLogService.save((ActionLog) log);@b@	}@b@@b@	@SuppressWarnings("rawtypes")@b@	public boolean supports(Class logClass) {@b@		return ActionLog.class.isAssignableFrom(logClass);@b@	}@b@@b@}

ErrorLogAppender类

import com.xwood.test.log.entity.ErrorLog;@b@import com.xwood.test.log.entity.ILog;@b@import com.xwood.test.log.service.IErrorLogService;@b@@b@public class ErrorLogAppender implements ILogAppender {@b@@b@	private IErrorLogService errorLogService;@b@	@b@	public IErrorLogService getErrorLogService() {@b@		return errorLogService;@b@	}@b@	@b@	public void setErrorLogService(IErrorLogService errorLogService) {@b@		this.errorLogService = errorLogService;@b@	}@b@	public void append(ILog log) {@b@		errorLogService.save((ErrorLog) log);@b@	}@b@@b@	@SuppressWarnings("rawtypes")@b@	public boolean supports(Class logClass) {@b@		return ErrorLog.class.isAssignableFrom(logClass);@b@	}@b@@b@}

ActionLogService类

import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.stereotype.Service;@b@import com.asc.common.persist.IPersistDao;@b@import com.xwood.test.log.entity.ActionLog;@b@ @b@@Service@b@public class ActionLogService implements IActionLogService {@b@	@b@	private IPersistDao dao;@b@	@b@	public IPersistDao getDao() {@b@		return dao;@b@	}@b@	@Autowired@b@	public void setDao(IPersistDao dao) {@b@		this.dao = dao;@b@	}@b@	 @b@	public void save(ActionLog log) {@b@		dao.save(log);@b@	}@b@	 @b@}

4. Spring上线文配置文件如下

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@		xmlns:context="http://www.springframework.org/schema/context"@b@		xmlns:aop="http://www.springframework.org/schema/aop"@b@		xmlns:mvc="http://www.springframework.org/schema/mvc"@b@		xsi:schemaLocation="@b@			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd@b@			http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd@b@			http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd@b@			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">@b@		@b@		<!-- 自动扫描Action加入Spring上下文 Scope:prototype -->@b@		<context:component-scan base-package="com.xwood.test" scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver"/>@b@		@b@		<bean id="actionLogAppender" class="com.xwood.test.log.registry.ActionLogAppender" autowire="byName" />@b@		<bean id="errorLogAppender" class="com.xwood.test.log.registry.ErrorLogAppender" autowire="byName" />@b@		@b@	 	<bean class="com.xwood.test.log.registry.LogAppenderRegistry">@b@	 		<property name="logAppenders">@b@	 			<list>@b@	 				<ref bean="actionLogAppender"/>@b@	 				<ref bean="errorLogAppender"/>@b@	 			</list>@b@	 		</property>@b@	 	</bean>@b@	 	@b@	 	<bean id="securityManager" class="com.xwood.test.log.SecurityManager" autowire="byName"/>@b@ 	@b@		 @b@</beans>
<<热门下载>>