需求:

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

解决方案:

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. bash配色

    Table of Contents PS1格式 基本格式 其它可能的格式 PS1配色方案 配置文件 bash的命令提示符和终端外观由环境变量PS1定义 PS1格式 基本格式 \u 显示当前用户名 \h ...

  2. 电脑小白和ta的小白电脑——MySQL数据库

    数据库我选择了MySQL,因为据说MySQL是最流行的关系型数据库管理系统,在WEB应用方面 MySQL 是最好的RDBMS之一了,而且,免费呀! MySQL数据库开发环境的配置 (一)下载MySQL ...

  3. 在servlet中跳转问题

    跳转有重定向和转发 1重定向 2转发

  4. 使用idea启动springMVC+Hibernate其他项目

    打开项目后打开Project Structure 点开左边的Libraries 加入依赖包 点开左边的Moudules 选中项目 新建Web,Spring,Hibernate三项 Hibernate添 ...

  5. c++数组传参

    最近感觉老是碰到数组传参的问题,特别是二维的数组,每次报错都感觉头疼,烦躁:这里必须总结一下了,先把暂时能解决的问题写在这吧,以便以后碰到查看! 先看一个一维数组传参:这样用数组传参是很不安全的:会输 ...

  6. python笔记21-内置函数

    # print(all([1,2,3,4]))#判断可迭代的对象里面的值是否都为真# print(any([0,0,0,0,0]))#判断可迭代的对象里面的值是否有一个为真# print(bin(10 ...

  7. 简单的bootstarp项目实例

    ===========index.html==============<!DOCTYPE html> <html> <head> <meta charset= ...

  8. Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks 阅读笔记

    Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks (使用循环一致的对抗网络的非配对图像-图 ...

  9. Tkinter模块:Grid几何管理器

    Tkinter模块是Python的标准库模块之一,也是使用Python语言进行图形化用户界面(GUI)开发的基础. 本文介绍一下Tkinter模块的Grid几何管理器. 使用VB.MFC进行GUI开发 ...

  10. ftp 发布配置

    地址:ftp://192.168.26.128/ 存放文件夹:jenkins