S2SH配置部署日志切面拦截器

0.为了防止service切面影像hibernate过滤器参数,需声明设定后者初始参数。
<!-- Spring 整合 Hibernate -->
	<filter>
		<filter-name>hibernateFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
			<param-name> flushMode </param-name>  
			<param-value>AUTO </param-value>  
	   </init-param>
		
	</filter>


1.部署切面
applicationContext.xml
	<!-- 定义事务属性 -->
	<aop:config proxy-target-class="true">
		<aop:advisor
			pointcut="execution(java.lang.String com.sunwin.*.service.*.*(..))"
			advice-ref="txAdvice" order="1" />
		<!-- 定义操作日志切入点(Service包下的所有类的以sav,upd,del开头的方法) -->
		<aop:aspect id="logAspect" ref="logInterceptor" order="2">
			<aop:pointcut id="logPointcut"
				expression="execution(* com.sunwin.*.service.*.sav*(..)) || execution(* com.sunwin.*.service.*.del*(..))|| execution(* com.sunwin.*.service.*.upd*(..))" />
			<aop:after pointcut-ref="logPointcut" method="serviceIntercept" />
		</aop:aspect>

	</aop:config>

	<!-- 用户操作日志拦截器类 -->
	<bean id="logInterceptor" class="com.sunwin.interceptor.EventLogInterceptor">
		<property name="baseService" ref="baseService"></property>
		<property name="userAction" ref="userAction"></property>
	</bean>


2.创建日志拦截器类
/**
	 * 拦截操作日志类
	 * @param point
	 * @return
	 * @throws Throwable
	 */
	public Object serviceIntercept(JoinPoint point) throws Throwable{
		//获取操作方法名称
		String methodName = point.getSignature().getName();
		//获取方法参数
		Object[] args =point.getArgs();
		//操作日志信息
		String logUserId=null;
		String logUserName=null;
		String bizType1=GlobalConstants.OPERATE;
		if (methodName.startsWith("sav")) 
		{
			logUserId=(String) userAction.getSession().get(SessionConstants.LOGIN_USER_ID);
			logUserName=(String) userAction.getSession().get(SessionConstants.LOGIN_USER_NAME);
			userAction.setLogType(GlobalConstants.CREATE);
			//保存操作日志
			this.baseService.saveLog(args[0], userAction.getIpAddr(), logUserId, logUserName, userAction.getLogType(), bizType1);
		}
		else if(methodName.startsWith("upd"))
		{
			logUserId=(String) userAction.getSession().get(SessionConstants.LOGIN_USER_ID);
			logUserName=(String) userAction.getSession().get(SessionConstants.LOGIN_USER_NAME);
			userAction.setLogType(GlobalConstants.UPDATE);
			//保存操作日志
			this.baseService.saveLog(args[0], userAction.getIpAddr(), logUserId, logUserName, userAction.getLogType(), bizType1);
		}
		else if(methodName.startsWith("del"))
		{
			logUserId=(String) userAction.getSession().get(SessionConstants.LOGIN_USER_ID);
			logUserName=(String) userAction.getSession().get(SessionConstants.LOGIN_USER_NAME);
			userAction.setLogType(GlobalConstants.DELETE);
			//保存操作日志
			this.baseService.saveLog(args[0], userAction.getIpAddr(), logUserId, logUserName, userAction.getLogType(), bizType1);
		}
		
		return null;
	} 



3.反射方法智能解析pojo对象(或对象列表)中定义的表名anotation:table_id、主键值(包括联合主键,逗号分隔)
/**
	 * 保存操作日志
	 * 
	 * @param obj
	 * @param logIp
	 * @param logUserId
	 * @param logUserName
	 * @param logType
	 * @param bizType1
	 */
	public void saveLog(Object obj, String logIp, String logUserId,
			String logUserName, String logType, String bizType1) {

		try {
			// 初始化log表
			CahsmsLog log = new CahsmsLog();
			
			if (null != obj) // 业务操作
			{

				Object logRecordPk = "";//主键值

				if (obj instanceof List) 
				{
					List logList = new ArrayList<CahsmsLog>();
					List list = (List) obj;
					// 获取物理表名
					AbstractEntityPersister classMetadata = (SingleTableEntityPersister) this.logDao
							.getSessionFactory().getClassMetadata(
									list.get(0).getClass());
					
					for (int i = 0; i < list.size(); i++) {
						log=new CahsmsLog();
						if (classMetadata != null) {
							String logTable = classMetadata.getTableName();
							log.setLogTable(logTable);
						}
						// 获取主键值
						logRecordPk = this.logDao.getSessionFactory()
								.getCurrentSession().getIdentifier(list.get(i));
						if(logRecordPk instanceof String )//非联合主键
						{
							log.setLogRecordPk((String)logRecordPk);
						}
						else//联合主键
						{
							Field[] field = logRecordPk.getClass().getDeclaredFields();
							String logRecordPkValue="";//主键值
							for(int j=0;j<field.length;j++)
							{
								String attributeName = field[j].getName(); //获取属性的名字
								String methodName=attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);

								Method m = logRecordPk.getClass().getMethod("get"+methodName);
								String attributeValue = (String) m.invoke(logRecordPk); //调用getter方法获取属性值
								logRecordPkValue+=attributeName+":"+attributeValue;
								if(j<field.length-1)
								{
									logRecordPkValue+=",";
								}
							}
							log.setLogRecordPk(logRecordPkValue);
						}
						log.setLogId(DataTypeUtil
								.getDateTimeStemp(GlobalConstants.PREFIX_LOG
										)+ i);// 日志id
						log.setLogDate(new Date(new Date().getTime()));// 获取操作时间
						log.setLogUserId(logUserId);// 登陆用户id
						log.setLogUserName(logUserName);// 登陆用户姓名
						log.setLogIp(logIp);// ip地址
						log.setLogType(logType);// 操作类型(C:Create;R:
												// Read;U:Update;D:Delete)
						log.setBizType1(bizType1);// 业务大类(1:登陆;2:业务操作;3:退出)
						logList.add(log);

					}
					this.logDao.getHibernateTemplate().getSessionFactory()
							.getCurrentSession().setFlushMode(FlushMode.AUTO);
					this.logDao.saveList(logList);
					this.logDao.getHibernateTemplate().getSessionFactory()
							.getCurrentSession().flush();

				} 
				else 
				{
					// 获取物理表名
					AbstractEntityPersister classMetadata = (SingleTableEntityPersister) this.logDao
							.getSessionFactory().getClassMetadata(
									obj.getClass());
					if (classMetadata != null) {
						String logTable = classMetadata.getTableName();
						log.setLogTable(logTable);
					}
					// 获取主键值
					logRecordPk = this.logDao.getSessionFactory()
							.getCurrentSession().getIdentifier(obj);
					if(logRecordPk instanceof String )//非联合主键
					{
						log.setLogRecordPk((String)logRecordPk);
					}
					else//联合主键
					{
						Field[] field = logRecordPk.getClass().getDeclaredFields();
						String logRecordPkValue="";//主键值
						for(int j=0;j<field.length;j++)
						{
							String attributeName = field[j].getName(); //获取属性的名字
							String methodName=attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);//设置属性值的方法名
							Method m = logRecordPk.getClass().getMethod("get"+methodName);
							String attributeValue = (String) m.invoke(logRecordPk); //调用getter方法获取属性值
							logRecordPkValue+=attributeName+":"+attributeValue;
							if(j<field.length-1)
							{
								logRecordPkValue+=",";
							}
						}
						log.setLogRecordPk(logRecordPkValue);
					}
					
					log.setLogId(DataTypeUtil
							.getDateTimeStemp(GlobalConstants.PREFIX_LOG));// 日志id
					log.setLogDate(new Date(new Date().getTime()));// 获取操作时间
					log.setLogUserId(logUserId);// 登陆用户id
					log.setLogUserName(logUserName);// 登陆用户姓名
					log.setLogIp(logIp);// ip地址
					log.setLogType(logType);// 操作类型(C:Create;R:
											// Read;U:Update;D:Delete)
					log.setBizType1(bizType1);// 业务大类(1:登陆;2:业务操作;3:退出)
					this.logDao.getHibernateTemplate().getSessionFactory()
							.getCurrentSession().setFlushMode(FlushMode.AUTO);
					this.logDao.save(log);
					this.logDao.getHibernateTemplate().getSessionFactory()
							.getCurrentSession().flush();
				}

			}
			else//登录和退出操作
			{
				log.setLogId(DataTypeUtil
						.getDateTimeStemp(GlobalConstants.PREFIX_LOG));// 日志id
				log.setLogDate(new Date(new Date().getTime()));// 获取操作时间
				log.setLogUserId(logUserId);// 登陆用户id
				log.setLogUserName(logUserName);// 登陆用户姓名
				log.setLogIp(logIp);// ip地址
				log.setLogType(logType);// 操作类型(C:Create;R:
										// Read;U:Update;D:Delete)
				log.setBizType1(bizType1);// 业务大类(1:登陆;2:业务操作;3:退出)
				this.logDao.getHibernateTemplate().getSessionFactory()
						.getCurrentSession().setFlushMode(FlushMode.AUTO);
				this.logDao.save(log);
				this.logDao.getHibernateTemplate().getSessionFactory()
						.getCurrentSession().flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

你可能感兴趣的:(AOP,log,s2sh,intercept)