事务方法里如何单独添加日志

来源:互联网 发布:2017淘宝男装店铺排行 编辑:程序博客网 时间:2024/06/01 09:07
ps:麦库记事上传不了代码,只能在这写了微笑
遇到的问题:项目中service层一个方法里有很多个添加修改方法,所以把最外层的方法放在事务里,就可以实现全部的回滚操作了。但是问题出现了,如果我想单独生成一个不在这个事务里的日志记录该怎么操作?
解决方法三种都亲测有效:
1,在service层的上一层,无论是impl层还是action在日志表里添加记录,这种代码看起来不是那么整洁,但是最简单粗暴
2,spring环绕通知实现日志保存

applicationContext.xml配置(我只定位到具体的文件夹,缺点就是哪天别的同事不知道存在切面的情况下在这个类写代码,那就坑死他了,哈哈,不过可以定位到具体的类和具体的方法) expression="execution (* com.hrd.appl.biz.intf.api.impl.appl.*.*(..))"。 其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。

<aop:aspectj-autoproxy />    <aop:config><aop:aspect id="applicationLogService" ref="applicationLogService">  切面     <aop:pointcut id="applicationLogPointcut" expression="execution (* com.hrd.appl.biz.intf.api.impl.appl.*.*(..))"/>切入点,配置在impl包下所有的类在调用之前都会被拦截           <aop:around pointcut-ref="applicationLogPointcut" method="around"/>                  环绕通知,调用前后</aop:aspect>   </aop:config> 

切面层配置

@Service("applicationLogService")public class ApplicationLogService {private static final Logger logger = LoggerFactory.getLogger(ApplicationLogService.class);@Resourceprivate UserAccountApplLogDao userAccountApplLogDao;public Object around(ProceedingJoinPoint pjp) throws Throwable {JiSiApplAppt js = new JiSiApplAppt();for (int i = 0; i < pjp.getArgs().length; i++) {List<Object> li = Arrays.asList(pjp.getArgs()[i]);for (int j = 0; j < li.size(); j++) {js = (JiSiApplAppt) li.get(j);}}UserAccountApplLog userAccountApplLog = new UserAccountApplLog();Object retVal = pjp.proceed();ReturnParam retParam = JsonUtils.toObject(retVal.toString(), ReturnParam.class);userAccountApplLog.setRemark(retParam.getRetInfo());if (ReturnParam.SUCCESSCODE.equals(retParam.getRetCode())) {userAccountApplLog.setStatus("succ");} else {userAccountApplLog.setStatus("err");}userAccountApplLog.setReturnObj("Request:" + js.toString() + ";Response:" + retParam.toString());// userAccountApplLogDao.insert(userAccountApplLog);return retVal;}}

3,设置不同的事务级别
springmvc配置文件

<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource" ref="dataSource"></property> </bean> <!-- 拦截器方式配置事物 --><!--DAO数据--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>//隔离级别<tx:method name="save*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="merge*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="create*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="publish*" propagation="REQUIRED" /><tx:method name="invalidate*" propagation="REQUIRED" /><tx:method name="newtrans*" propagation="REQUIRES_NEW" /><tx:method name="executeEngine*" propagation="REQUIRED" /><tx:method name="batchCreate*" propagation="REQUIRED" /><tx:method name="forward*" propagation="REQUIRED" /><tx:method name="agree*" propagation="REQUIRED" /><tx:method name="refuse*" propagation="REQUIRED" /><tx:method name="complete*" propagation="REQUIRED" /><tx:method name="cancel*" propagation="REQUIRED" /><tx:method name="forward*" propagation="REQUIRED" /><tx:method name="file*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="deploy*" propagation="REQUIRED" /><tx:method name="find*" propagation="REQUIRED" /></tx:attributes></tx:advice>


service 方法

public ReturnParam<Object> saveApplAppt(JiSiApplAppt param) {UserAccountApplLog userAccountApplLog = new UserAccountApplLog();ReturnParam<Object> revParam = new ReturnParam<Object>();LcAppl appl = param.getAppl();userAccountApplLog.setReturnObj(param.toString());userAccountApplLog.setCooprCode(param.getAppl().getCooprCode());userAccountApplLog.setTransSeq(param.getTransSeq());userAccountApplLog.setUserId(param.getAppl().getUserId());userAccountApplLog.setManageCde(param.getAppl().getManageCde());userAccountApplLog.setAddtime(String.valueOf(DateUtils.getNowTime()));int id = userAccountApplDao.newtransinsert(userAccountApplLog);revParam = this.validate(appl, revParam);if (!ReturnParam.SUCCESSCODE.equals(revParam.getRetCode())) {userAccountApplLog.setRemark(revParam.getRetInfo());userAccountApplLog.setStatus("err");userAccountApplDao.newtransupdate(userAccountApplLog);return revParam;}return revParam;}




原创粉丝点击