package com.dhht.config.articleAdvice;

import com.dhht.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context; import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Map; /**
* @Author: sh
* @Description: ArticleServiceCfg
* @Date: 14:18 2019/9/23
*/
@Slf4j
@Configuration
@ComponentScan({"com.dhht.service.article.impl","com.dhht.service.link.impl","com.dhht.service.filemanage.impl"})
@EnableAspectJAutoProxy
public class RegulationServiceCfg { @Bean
public TemplateEngine getTemplateEngine(){
return new TemplateEngine();
} }
package com.dhht.config.articleAdvice;

import com.dhht.model.Regulation;
import com.dhht.model.User;
import com.dhht.service.PageCacheService;
import com.dhht.service.article.RegulationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @Author: sh
* @Description: ArticleServiceAdvice
* @Date: 14:19 2019/9/23
*/
@Slf4j
@Aspect
@Component
public class RegulationServiceAdvice { static final String insert = "insert";
static final String delete = "delete";
static final String update = "update"; static final String REGULATION_ENTY_K = "regulationEnty";// 实体数据模型名
static final String CACHE_FILE_SUFFIX = ".html";
static final String DETAILS_PATH = "details";//
static final String WECHAT_PATH = "/wechat/"; final String XWZX_LIST = "xwzxList";// 类型数据key-List
final String ZCFG_LIST = "zcfgList";
final String HYBZ_LIST = "hybzList";
final String ZSYD_LIST = "zsydList"; final String GATE_TEMP_0 = "gate_type_0";// 门户模块-模板Id
final String GATE_TEMP_1 = "gate_type_1";
final String GATE_TEMP_2 = "gate_type_2";
final String GATE_TEMP_3 = "gate_type_3"; @Value("${regulation_path}")
String REGULATION_PATH;// 缓存页面磁盘路径 D:/page_cahce/page/ @Value("${regulation_temp_source}")
String REGULATION_TEMP_SOURCE;// new_guide @Value("${regulation_details_temp}")
String REGULATION_DETAILS_TEMP;// news_details @Value("${gateway_temp_source}")
String GATEWAY_TEMP_SOURCE;// gateway @Value("${wechat_gate_temp}")
String WECHAT_GATE_TEMP; @Value("${wechat_gate_detail_temp}")
String WECHAT_GATE_DETAIL_TEMP; @Resource
PageCacheService pageCacheService; @Resource
RegulationService regulationService; /**
* 新增切点
*/
@Pointcut("execution(* com.dhht.service.article.RegulationService.insert(..))")
public void insertPointCut() {
} /**
* 删除切点
*/
@Pointcut("execution(* com.dhht.service.article.RegulationService.delete(..))")
public void deletePointCut() {
} /**
* 更新切点
*/
@Pointcut("execution(* com.dhht.service.article.RegulationService.update(..))")
public void updatePointCut() {
} /**
* 环绕通知
*
* @param joinPoint
* @param returnValue
*/
@AfterReturning(value = "insertPointCut()||deletePointCut()||updatePointCut()", returning = "returnValue")
public void afterReturing(JoinPoint joinPoint, Object returnValue) {
dealTarMethParamAndReturn(joinPoint, returnValue);
} /**
* 处理目标方法的参数与返回值
*
* @return
*/
public synchronized boolean dealTarMethParamAndReturn(JoinPoint joinPoint, Object returnValue) {
try {
String name = joinPoint.getSignature().getName();
if (insert.equals(name)) {
Object[] args = joinPoint.getArgs();
Regulation param_0 = (Regulation) args[0];
User param_1 = (User) args[1];
Regulation result = (Regulation) returnValue;
if (null!=result) {
updateDetatiTempData(result);
geneWechatDetail(result);
updateGuideTempData();
}
}
if (delete.equals(name)) {
Object[] args = joinPoint.getArgs();
Regulation param = (Regulation) args[0];
Regulation result = (Regulation) returnValue;
if (null!=result) {
updateGuideTempData();
String id = result.getId();
File file = new File(REGULATION_PATH + DETAILS_PATH, id + CACHE_FILE_SUFFIX);
if (file.exists()) {// 删除缓存文件
file.delete();
}
File wechatFile = new File(REGULATION_PATH + WECHAT_PATH + DETAILS_PATH, id + CACHE_FILE_SUFFIX);
if (wechatFile.exists()) {// 删除缓存文件
wechatFile.delete();
}
}
}
if (update.equals(name)) {
Object[] args = joinPoint.getArgs();
Regulation param = (Regulation) args[0];
Regulation result = (Regulation) returnValue;
if (null!=result) {
String id = result.getId();
if(!StringUtils.isEmpty(id)){
result = regulationService.selectRegulationById(id);
}
updateDetatiTempData(result);
geneWechatDetail(result);
updateGuideTempData();
}
}
return true;
} catch (Exception e) {
log.info(e.getMessage());
return false;
}
} /**
* 更新到导航模板数据
*/
public void updateGuideTempData(){
if(null==regulationService){
return;
}
List<Regulation> xwzxList = regulationService.queryRegulationAll(0);
List<Regulation> zcfgList = regulationService.queryRegulationAll(1);
List<Regulation> hybzList = regulationService.queryRegulationAll(2);
List<Regulation> zsydList = regulationService.queryRegulationAll(3);
Map<String,Object> valMap = new HashMap<>();
valMap.put(XWZX_LIST,xwzxList);
valMap.put(ZCFG_LIST,zcfgList);
valMap.put(HYBZ_LIST,hybzList);
valMap.put(ZSYD_LIST,zsydList);
pageCacheService.setValToTemplat(valMap,REGULATION_TEMP_SOURCE);// 添加tap
pageCacheService.setValToTemplat(valMap,GATEWAY_TEMP_SOURCE);// 填充门户
geneWechatPage(valMap);// 小程序门户
} /**
* 更新资讯详情模板数据
* @param regulation
*/
public void updateDetatiTempData(Regulation regulation){
Map<String, Object> valsMap = new HashMap<>();
valsMap.put(REGULATION_ENTY_K, regulation);
String id = regulation.getId();
pageCacheService.setVals(valsMap, REGULATION_DETAILS_TEMP, id, REGULATION_PATH + DETAILS_PATH);
} /**
* 更新门户-四模块-模板数据
*/
public void updateGateTypeTempData(List<Regulation> dataList,Integer type){
Map<String,Object> vals = new HashMap<>();
String tempId = null;
if(0==type){
tempId = GATE_TEMP_0;
vals.put(XWZX_LIST,dataList);
}
if(1==type){
tempId = GATE_TEMP_1;
vals.put(ZCFG_LIST,dataList);
}
if(2==type){
tempId = GATE_TEMP_2;
vals.put(HYBZ_LIST,dataList);
}
if(3==type){
tempId = GATE_TEMP_3;
vals.put(ZSYD_LIST,dataList);
}
pageCacheService.setValToTemplat(vals,tempId);
} /**
* 小程序门户列表
* @param valsMap
*/
public void geneWechatPage(Map<String,Object> valsMap){
pageCacheService.setVals(valsMap,WECHAT_GATE_TEMP,WECHAT_GATE_TEMP,REGULATION_PATH+WECHAT_PATH);
} /**
* 小程序资讯详情
* @param regulation
*/
public void geneWechatDetail(Regulation regulation){
Map<String, Object> valsMap = new HashMap<>();
valsMap.put(REGULATION_ENTY_K, regulation);
String id = regulation.getId();
pageCacheService.setVals(valsMap, WECHAT_GATE_DETAIL_TEMP, id, REGULATION_PATH + WECHAT_PATH + DETAILS_PATH);
} }

spring自定义aop的更多相关文章

  1. Spring 自定义注解,结合AOP,配置简单日志注解 (转)

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  2. Spring学习笔记(二)Spring基础AOP、IOC

    Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...

  3. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  4. Spring 自定义注解,配置简单日志注解

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  5. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  6. Spring实现AOP的多种方式

    转载自:https://www.cnblogs.com/best/p/5736422.html:加了一些自己的注释: AOP(Aspect Oriented Programming)面向切面编程,通过 ...

  7. 深入Spring:自定义事务管理

    转自: http://www.jianshu.com/p/5347a462b3a5 前言 上一篇文章讲了Spring的Aop,这里讲一下Spring的事务管理,Spring的事务管理是建立在Aop的基 ...

  8. Spring自定义标签解析与实现

           在Spring Bean注册解析(一)和Spring Bean注册解析(二)中我们讲到,Spring在解析xml文件中的标签的时候会区分当前的标签是四种基本标签(import.alias ...

  9. 使用Spring实现AOP(XML+注解)

    一.Spring对AOP的支持 AOP并不是Spring框架特有的,Spring只是支持AOP编程的框架之一,每一个框架对AOP的支持各有特点,有些AOP能够对方法的参数进行拦截,有些AOP对方法进行 ...

随机推荐

  1. JS ~ Promise.reject()

    概述: Promise.reject(reason)方法返回一个带有拒绝原因reason参数的Promise对象. 语法 Promise.reject(reason); reason :  表示Pro ...

  2. 阿里巴巴技术总监全解中台架构19页ppt

    //初创时,快速上线 单体架构至少撑了3年 //分布式,中间件基座 //平台化,内部是简单服务,对于业务侧就是快速上线 //平台化之后由于多平台协作问题,再次出现问题: 效率仍然不能匹配业务发展之需要 ...

  3. 面试题(6)之 leetcode-001

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  4. Codeforces 437D 贪心+并查集

    这个题目让我想起了上次在湘潭赛的那道跪死了的题.也是最值问题,这个也是,有n个动物园 每个都有权值 然后被m条路径相连接,保证图是连通的,然后求所有的p[i][j]之和.i,j为任意两个zoo,pij ...

  5. 干货分享:Essay写作收集论据的三个方法

    在很多时候,中国留学生写出的Essay在西方学术界看来是存在plagiarism的情况.并不是说咱们写的所有东西都是抄袭,而是思维逻辑和利用证据的方式与西方权威的academic writing不同. ...

  6. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

    题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...

  7. 201771010123汪慧和《面向对象程序设计Java》第十八周实验总结

    一.总复习纲要 1. Java语言特点与开发环境配置(第1章.第2章) 2. Java基本程序结构(第3章) 3. Java面向对象程序结构(第4章.第5章.第6章) 4. 类.类间关系.类图 5. ...

  8. CKeditor上传图片 实现所见即所得界面

    迟了好多天的分享,CKeditor这个编辑器虽然不错,但也真苟啊,搞图片上传这个功能,快给我搞佛系了,话不多说,上代码 1.首先去官网下载一个full的版本,我用的是CKeditor 4.13,解压之 ...

  9. 第22章—开启HTTPS

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  10. 洛谷 P5661 公交换乘(队列)

    题目传送门 解题思路: 暴力模拟. AC代码: #include<iostream> #include<cstdio> #include<queue> using ...