需求:

例如我们需要有一个类中每个方法执行前都需要做一个权限校验,必须是有特定权限的账号才能完成该方法的操作。

解决方案:

1.使用父类继承方式,书写该类的父类,然后在父类中定义一个checkPri的权限校验方法,然后子类(就是我的目标需求子类)每个方法调用这个父类方法,完成权限校验。

弊端:这是纵向完成形式,如果我哪天不需要校验了,首先要取消继承,然后还要每个方法都把父类方法删掉。。。

2.相对于第一种方式,如果采用aop横向切割的方式做,它相当于一个组件作用在方法中,方法中不需要做任何改变,有种“润物细无声”的感觉,完成扩展功能!

简易aop测试搭建(xml形式):

 <dependencies>
<!-- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>-->
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--spring aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency> <dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!--test spring 于junit整合-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
</dependency> </dependencies>
//切入点和通知     权限校验或者日志封装
public class Aspect { public void checkPri(){
System.out.println("权限校验!!");
} public void logPrint(){
System.out.println("log 打印!!");
} }
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描包-->
<context:component-scan base-package="aop.*"></context:component-scan> <!-- 在配置文件中开启注解的AOP的开发============ -->
<aop:aspectj-autoproxy/> <!-- 配置目标类================ -->
<!--<bean id="productDao" class="aop.dao.ProductDaoImpl">
</bean>--> <!-- 配置切面类================ -->
<bean id="myAspect" class="aop.Aspect.Aspect"></bean> <!--通过aop配置对目标类增强-->
<aop:config>
<!--切入点 配置哪些类那些方法被增强-->
<aop:pointcut id="p1" expression="execution(* aop.dao.ProductDaoImpl.save(..))"></aop:pointcut>
<aop:pointcut id="p2" expression="execution(* aop.dao.ProductDaoImpl.delete(..))"></aop:pointcut>
<aop:pointcut id="p3" expression="execution(* aop.dao.ProductDaoImpl.update(..))"></aop:pointcut>
<aop:pointcut id="p4" expression="execution(* aop.dao.ProductDaoImpl.query(..))"></aop:pointcut>
<!--通知 配置通知类-->
<aop:aspect ref="myAspect" >
<!--前置通知-->
<aop:before method="checkPri" pointcut-ref="p1"></aop:before>
<aop:after-returning method="logPrint" pointcut-ref="p2" returning="result"></aop:after-returning>
<aop:around method="watch" pointcut-ref="p3"></aop:around>
<aop:after-throwing method="afterAfterThrowing" pointcut-ref="p4" throwing="ex"></aop:after-throwing>
<aop:after method="after" pointcut-ref="p2" ></aop:after>
</aop:aspect>
</aop:config>
</beans>
package aop.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; //切入点和通知 权限校验或者日志封装
public class Aspect { //前置
public void checkPri(JoinPoint joinPoint){
System.out.println("权限校验!!"+joinPoint);
//return;//不起作用!
} //后置通知
public void logPrint(Object result){//参数名称必须与配置文件的returning一致
System.out.println("log 打印!!");
//后置通知可以获得方法返回值
System.out.println("获得结果数值"+result);
} //环绕通知
public Object watch(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("前置通知。。。。");
Object proceed = joinPoint.proceed();//这边可以控制目标对象切入点方法是否执行
System.out.println("后置通知。。。。");
return proceed;
} //异常抛出通知
public void afterAfterThrowing(Throwable ex){
System.out.println("异常通知!");
ex.printStackTrace();
} //最终通知
public void after(){
System.out.println("最终通知!");
}
}

这边要获得joinpoint 注意导入jar包要正确,不然会报错!!!

spring-aop思想实践demo的更多相关文章

  1. Spring AOP应用实例demo

    AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...

  2. 深入理解Spring AOP思想

    什么是AOP?AOP解决了什么问题? 在传统的开发模式中,以下层次的是非常常见的一种,业务层每一个方法都要有重复的事务代码 如何改善这个问题? AOP希望将A.B 这些分散在各个业务逻辑中的相同代码, ...

  3. 【AOP】Spring AOP基础 + 实践 完整记录

    Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...

  4. SSH框架系列:Spring AOP应用记录日志Demo

    分类: [java]2013-12-10 18:53 724人阅读 评论(0) 收藏 举报 1.简介 Spring 中的AOP为Aspect Oriented Programming的缩写,面向切面编 ...

  5. Spring aop 小例子demo

    由于最近的服务项目提供接口有一个需求,所有操作都必须检查操作的服务可用,所以感觉Aop特别适合实施.完成学习的小例子. 关于spring-Aop原理:http://m.oschina.net/blog ...

  6. spring Aop的一个demo

    面向切面是什么我就不说了. 上代码: package com.foreveross.service.weixin.test; import java.lang.annotation.Documente ...

  7. spring aop 的一个demo(未完,待完善)

    假设我们有这样的一个场景 : 对于一个类的众多方法,有些方法需要从缓存读取数据,有些则需要直接从数据库读取数据.怎样实现呢? 实现方案有多种.下面我说下常见的几种实现方案 : 1.直接采用spring ...

  8. Spring AOP注解配置demo

    https://blog.csdn.net/yhl_jxy/article/details/78815636#commentBox

  9. spring aop思想

随机推荐

  1. 洛谷P1993 小K的农场

    思路是差分约束+dfs版SPFA. 首先来思考差分约束的过程,将题目给出的式子进行转化: 农场a比农场b至少多种植了c个单位的作物, SPFA我们考虑跑最短路,那么要让SPFA中满足的式子就是if(d ...

  2. 辗转相除法(GCD)求左旋转字符串

    本文写于2017-01-18,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6297874.html 今天在牛客网上做了一道题,题意就是 ...

  3. 固定div的位置——不随窗口大小改变为改变位置

    百度首页示例: 我给二维码,和下面文本固定位置 这时html代码 <div id="bar_code"> <div class="img_put&quo ...

  4. java基础3(二)

    基础加强 类加载器 A.类加载器获取classpath下任意内容 注意: 可以通过ClassLoader可以获取classpath下的所有内容. SpringMVC 1.整体架构 流程图 2.流程细节 ...

  5. jsapi 调起微信支付的的踩坑

    问题: 公众微信号调起微信支付的时候,有的时候调起支付成功,有的时候调起支付失败.利用抓包工具抓取数据显示授权和调用后台的微信预支付订单接口都成功并且都返回正确的数据.但是调起支付的时候传入的data ...

  6. 分页 工具类 前后台代码 Java JavaScript (ajax) 实现 讲解

    [博客园cnblogs笔者m-yb原创, 转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708]http ...

  7. 简单的ALV示例

    在这里也推荐一条链接,很适合初学者:https://blog.csdn.net/Kang_xiong/article/details/64922576 这是一个特别基础的示例,适合没有任何ABAP基础 ...

  8. web前端的前景

    随着时代的发展,现在从事IT方向的人有很多,所以励志要成为前端开发工程师的人有很多.当然也有很多人在犹豫不知道该从事哪个方向,我今天就是来给大家分析一下Web前端开发的前景.包括工作内容,发展前景和薪 ...

  9. logging dictconfig

    #coding: utf-8 import logging import logging.config   class SingleLevelFilter(object):     def __ini ...

  10. vue生命周期和钩子函数

    new Vue 创建vue实例 init events & liftcycle 开始初始化 beforeCreate 组件刚被创建,组件属性计算之前,如data属性等 init injecti ...