在我们的 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(){
}
}
- Java 面向切面编程(Aspect Oriented Programming,AOP)
本文内容 实例 引入 原始方法 装饰者模式 JDK 动态代理和 cglib 代理 直接使用 AOP 框架--AspectWerkz 最近跳槽了,新公司使用了 AOP 相关的技术,于是查点资料,复习一下 ...
- Java Web高级编程(四)
WebSocket 一.WebSocket的产生 用户希望Web页面可以进行交互,用于解决这个问题的技术是JavaScript,现在Web上有许多的可用的JavaScript框架,在使用极少的Java ...
- Java Web高级编程(三)
使用过滤器改进应用程序 一.过滤器的目的 过滤器是可以拦截访问资源的请求.资源的响应或者同时拦截两者的应用组件.过滤器可以检测和修改请求和响应,同时也可以拒绝.重定向或转发请求.javax.servl ...
- Java Web高级编程(二)
使用会话维持状态 一.会话 为了实现关联同一个用户端的多个请求和这些请求之间数据的共享,需要用到会话,会话用于维持请求和请求之间的状态.从服务器的角度,当用户的Web浏览器打开第一个链接到服务器的套接 ...
- Java Web高级编程(一)
Servlet 一.创建Servlet类 在Java EE中,Servlet用来接收和响应终端用户的请求.Servlet是所有Web应用程序的核心类,是唯一既可以直接处理和响应用户请求,也可以将处理工 ...
- java面向切面编程总结-面向切面的本质
面向切面的本质:定义切面类并将切面类的功能织入到目标类中: 实现方式:将切面应用到目标对象从而创建一个新的代理对象的过程.替换: 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Point ...
- java web高级编程 笔记1
chapter1:了解web应用程序 web应用程序主要组件: Servlet 过滤器 监听器 JSP chapter2:各类web容器介绍 略 chapter3:Servlet介绍 Servlet是 ...
- Java Web编程技术学习要点及方向
学习编程技术要点及方向亮点: 传统学习编程技术落后,应跟著潮流,要对业务聚焦处理.要Jar, 不要War:以小为主,以简为宝,集堆而成.去繁取简 Spring Boot,明日之春(future of ...
- Java Web开发模式
一 Java Web开发模式的变迁 1 最初的Java web服务器端编程技术是Servlet,利用Servlet就可以开发出一个Web应用程序. 2 为了解决Servlet缺陷,SUN推出了JSP技 ...
随机推荐
- bzoj 4813: [Cqoi2017]小Q的棋盘 [树形背包dp]
4813: [Cqoi2017]小Q的棋盘 题意: 某poj弱化版?树形背包 据说还可以贪心... #include <iostream> #include <cstdio> ...
- HDU 5996 dingyeye loves stone [阶梯Nim]
dingyeye喜欢和你玩石子游戏. dingyeye有一棵nn个节点的有根树,节点编号为00到n−1n−1,根为00号节点.游戏开始时,第ii个节点上有a[i]a[i]个石子.两位玩家轮流操作,每次 ...
- BZOJ 1770: [Usaco2009 Nov]lights 燈 [高斯消元XOR 搜索]
题意: 经典灯问题,求最少次数 本题数据不水,必须要暴搜自由元的取值啦 想了好久 然而我看到网上的程序都没有用记录now的做法,那样做遇到自由元应该可能会丢解吧...? 我的做法是把自由元保存下来,枚 ...
- 数据分析之pandas教程------数据处理
目录 1 数据合并 1.1 实现数据库表join功能 1.2 实现union功能 2 数据转换 2.1 轴旋转 2.2 数据转换 2.2.1 去重 2.2.2 对某一列运用函数 2.2 ...
- Javascript原型继承容易忽略的错误
编写Javascript的开发者都知道,JS虽然没有类(ES6添加了class语法),但是可以模拟出OOP语言的类和面向对象的概念,比如我们都知道的一句话,Javascript中处处是对象,而面向对象 ...
- python用Django+Celery+Redis 监视程序(一)
C盘创建一个目录就叫DjangoDemo,然后开始在该目录下操作. 1.新建Django工程与应用 运行pip install django 安装django 这里我们建一个名为demo的项目和hom ...
- php 数组变成树状型结构
<? php $stime = microtime(true); $nodes = [ ['id' = > 1, 'pid' = > 0, 'name' = > 'a'], [ ...
- [Python Study Notes]计算cpu使用率
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- php实现的短网址算法分享
这篇文章主要介绍了php实现的短网址算法,理论上支持1,073,741,824个短网址,个人使用足够了,需要的朋友可以参考下 每个网址用6个字符代替,(6^32) 最多可以拥有1,073,741,82 ...
- MySQL数据库基础(MySQL5.7安装、配置)
写在前面: MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQ ...