9.1 AOP

  • AOP可以了让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,这样使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上;
  • 下面演示一个日志系统的实现,简单但不失表达AOP的核心内容
    • 演示通过注解拦截和通过方法规则拦截;
  • 一些小术语
    • JoinPoint:你需要拦截的代码位置(代码里已标识)
    • Pointcut:符合某个条件后需要执行的代码位置(代码里已标识)

9.2 示例

采取2种截获方式:拦截注解和拦截方法

9.2.1 增加aspectj依赖到maven

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>

9.2.2 编写拦截规则的注解

package com.wisely.aop;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name(); }

9.2.3 编写需要拦截的2个测试bean

  • 使用注解
package com.wisely.aop;

import org.springframework.stereotype.Service;

@Service
public class Demo1Service {
@Action(name="demo1,add操作")
public void add(){} //JoinPoint
@Action(name="demo1,remove操作")
public void remove(){}//JoinPoint
@Action(name="demo1,update操作")
public void update(){}//JoinPoint
@Action(name="demo1,query操作")
public void query(){}//JoinPoint }
  • 使用方法规则
package com.wisely.aop;

import org.springframework.stereotype.Service;

@Service
public class Demo2Service {
public void add(){}//JoinPoint
public void remove(){}//JoinPoint
public void update(){}//JoinPoint
public void query(){}//JoinPoint
}

9.2.4 编写切面

package com.wisely.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogAspect {
@After("@annotation(com.wisely.aop.Action)") //此处为pointcut
public void after(JoinPoint joinPoint) {
//每一个符合表达式条件的位置为joinPoint
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println(action.name());
//获得操作内容后可插入数据库中
} @Before("execution(* com.wisely.aop.Demo2Service.*(..))") //此处为pointcut
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("demo2,"+method.getName()); }
}

9.2.5 测试

package com.wisely.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@EnableAspectJAutoProxy //开启对AspectJ的@Aspect注解的支持,别忘了
public class Main { public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.aop");
Demo1Service d1s = context.getBean(Demo1Service.class);
d1s.add();
d1s.remove();
d1s.update();
d1s.query(); Demo2Service d2s = context.getBean(Demo2Service.class);
d2s.add();
d2s.remove();
d2s.update();
d2s.query();
context.close();
} }

输出结果

demo1,add操作
demo1,remove操作
demo1,update操作
demo1,query操作
demo2,add
demo2,remove
demo2,update
demo2,query

09点睛Spring4.1-AOP的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. Spring4之AOP

    [www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]<一头扎进Spring4>第七讲 源码 [www.dev1 ...

  4. Spring框架学习09——基于AspectJ的AOP开发

    1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...

  5. 09点睛Spring MVC4.1-异步请求处理(包含兼容浏览器的服务器端推送)

    转发地址:https://www.iteye.com/blog/wiselyman-2215852 9.1 异步请求处理 Servlet 3开始支持异步请求处理 Spring MVC 3.2开始支持S ...

  6. 15点睛Spring4.1-TaskExecutor

    转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...

  7. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  8. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  9. 17点睛Spring4.1-@Conditional

    17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...

随机推荐

  1. BZOJ 3625:小朋友和二叉树 多项式开根+多项式求逆+生成函数

    生成函数这个东西太好用了~ code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s&q ...

  2. 浏览器URL中“#” “?” &“”作用

    1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http://twitter.com/username改版后,就变 ...

  3. PHP的3种发送HTTP请求的方式

    1.CURL方式发送数据及上传文件 <?php class IndexController extends ControllerBase { public function indexActio ...

  4. Codeforces 1137F Matches Are Not a Child's Play [LCT]

    Codeforces 很好,通过这题对LCT的理解又深了一层. 思路 (有人说这是套路题,然而我没有见过/kk) 首先发现,删点可以从根那里往下删,非常难受,所以把权值最大的点提为根. 然后考虑\(x ...

  5. mobx中的数组需要注意的地方

    mobx中如果将数组作为可观察. 可以通过添加修饰符observable或者调用observable方法. 很多的时候, 我们将此修饰为可观察的对象后, 就随处可用了. 比如,采用 map  forE ...

  6. GoCN每日新闻(2019-09-25)

    GoCN每日新闻(2019-09-25) 1. Go module 再回顾 https://colobu.com/2019/09/23/review-go-module-again/2. 如何灵活地进 ...

  7. 《挑战30天C++入门极限》C++的iostream标准库介绍(3)

        C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...

  8. Java通过JDBC连接MySQL数据库(一)

    JDBC JAVA Database Connectivity java 数据库连接 为什么会出现JDBC SUN公司提供的一种数据库访问规则.规范, 由于数据库种类较多,并且java语言使用比较广泛 ...

  9. 容易被忽视的python装饰器的特性

    今天发现了装饰器的另一种用法,下面就先上代码: data_list = [] def data_item(func): data_list.append(func) return func @data ...

  10. ubuntu之路——day9.3 softmax regression激活函数

    Softmax 用于在深度学习中处理多分类(C > 2)问题,分类器最后的输出单元需要Softmax 函数进行数值处理.关于Softmax 函数的定义如下所示: 其中vi表示 vi = z[L] ...