在我们的 web开发中  我们在 对公用的 一些方法 我们需要抽取出来   这样达到 代码的冗余   今天 我利用项目上用的AOP的 实际 应用做了一个整理

首先  xml配置  扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com.leimingtech.platform.core.interceptors" />
    <context:component-scan base-package="com.leimingtech.cms.interceptors" />

</beans>

 
 
 
活动切面示列
 
 
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.leimingtech.core.common.ContentClassify;
import com.leimingtech.core.base.ContextHolderUtils;
import com.leimingtech.core.entity.ContentsEntity;
import com.leimingtech.core.entity.VoteEntity;
import com.leimingtech.core.service.SystemService;
import com.leimingtech.core.service.VoteServiceI;
import com.leimingtech.core.util.StringUtils;
 
/**
 * 投票切面类
 * @author zhangxiaoqiang
 *
 */
@Aspect
@Component
public class VoteInterceptor{
    @Autowired
    private SystemService systemService;
    @Autowired
    private VoteServiceI voteService;
    /*
    *
    *所要切面的 那个方法 类
    *
    */
    @Pointcut("execution(public * com.leimingtech.core.service.ContentsServiceI.saveContent(..))")  
    public void myMethod(){};
    /**
     * 下面用到的是织入点语法, 看文档里面有. 就是指定在该方法前执行
     * 记住下面这种通用的, *表示所有
     * @param map
     */
    @Before("myMethod()&&args(map,..)")
    public void beforeMethod(Map<String,Object> map){
        
    }
    /**
     * 正常执行完后
     * 保存内容之后,保存投票
     * @param map
     */
    @After("myMethod()&&args(map,..)")
    public void after(Map<String,Object> map){
        ContentsEntity contents = (ContentsEntity) map.get("contents");
        VoteEntity vote = (VoteEntity) map.get("vote");
        HttpServletRequest request = ContextHolderUtils.getRequest();
        //内容id
        String contentsId = request.getParameter("contentsId");
        if(StringUtils.isNotEmpty(contentsId)){
            contents= voteService.get(ContentsEntity.class, String.valueOf(contentsId));
        }
        String classify = contents.getClassify();
        if(ContentClassify.CONTENT_VOTE.equals(classify)){
            voteService.saveVote(contents, vote);
        }
    }
    /**
     * 正常执行完后
     */
    @AfterReturning("myMethod()")
    public void afterReturnning(){
        
    }
    /**
     * 抛出异常时才调用
     */
    @AfterThrowing("myMethod()")
    public void afterThrowing(){
        
    }
    

}

 
 
 

最新文章

  1. Linux No volume control GStreamer plugins and/or devices found
  2. CCF 201612-2 火车购票 (暴力)
  3. BZOJ1444 : [Jsoi2009]有趣的游戏
  4. PCB的技巧
  5. CImg 读取jpg, png ,tif 等格式失败解决方案
  6. LINQ 内链接 左链接 右链接
  7. Node.js笔记2
  8. ActionBar点击弹出下拉框操作
  9. git简单教材
  10. H5富文本编辑器之初始化用于编辑的DOM-遁地龙卷风
  11. HDU - 1050
  12. SQL 查找存在某内容的存储过程都有哪些
  13. spark-submit(spark版本2.4.2)
  14. Javascript高级编程学习笔记(72)—— 模拟事件(2)IE事件模拟
  15. 数据库vertica 脚本方式的导入导出
  16. 性能测试二十三:环境部署之Redis安装和配置
  17. HTTP那些事儿
  18. UWP 大爆炸你个锤子
  19. Azure 元数据服务:适用于 Windows VM 的计划事件(预览)
  20. leetcode 二叉搜索树中第K小的元素 python

热门文章

  1. xBIM 使用Linq 来优化查询
  2. 文件锁FileLock
  3. BZOJ 3879: SvT [虚树 后缀树]
  4. 解决xshell评估期已过的问题
  5. 全球(局)唯一标识符GUID的使用
  6. Language Modeling with Gated Convolutional Networks
  7. shared_lock and unique_lock
  8. .NET Core UI框架Avalonia
  9. MSSql Server 批量插入数据优化
  10. ARM平台的虚拟化介绍