09点睛Spring4.1-AOP
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的更多相关文章
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- Spring4之AOP
[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]<一头扎进Spring4>第七讲 源码 [www.dev1 ...
- Spring框架学习09——基于AspectJ的AOP开发
1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...
- 09点睛Spring MVC4.1-异步请求处理(包含兼容浏览器的服务器端推送)
转发地址:https://www.iteye.com/blog/wiselyman-2215852 9.1 异步请求处理 Servlet 3开始支持异步请求处理 Spring MVC 3.2开始支持S ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 17点睛Spring4.1-@Conditional
17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...
随机推荐
- learning java AWT 布局管理器FlowLayout
AWT提供了FlowLayout 从左到右排列所有组件,遇到边界就会折回下一行重新开始. import java.awt.*; public class FlowLayoutTest { publ ...
- Zatree - Zabbix图表展示
Zatree Zatree 是 一个php web的插件,做个展示树:可以提供host group的树形展示和在item里指定关键字查询及数据排序. 下载地址 可以根据zabbix不同版本下载:htt ...
- BZOJ 1406: [AHOI2007]密码箱
二次联通门 : BZOJ 1406: [AHOI2007]密码箱 /* BZOJ 1406: [AHOI2007]密码箱 数论 要求 x^2 ≡ 1 (mod n) 可以转换为 x ^ 2 - k * ...
- 讲解SQL数据库语句
前言 大家好,我是 Vic,今天给大家带来讲解SQL数据库语句的概述,希望你们喜欢 数据库语句 create database teach; use teach; create table `teac ...
- vue-router 的原理
1. hash 修改的时候:history.pushState('名字', null, '/xxx') || location.hash = '/xxx' 回退的时候:window.addEventL ...
- datagrid其中某列需要动态隐藏或显示的mvvm绑定方式,也可以用在其他表格类型控件上
版权归原作者所有. 引用地址 [WPF] HOW TO BIND TO DATA WHEN THE DATACONTEXT IS NOT INHERITED MARCH 21, 2011 THOMAS ...
- spaceclaim脚本(内摆线)
import math #导入数学模块,因为会使用π def x_comp(k,r,t): #定义x坐标的计算函数 return r * (k -1) * math.cos(t) + r * math ...
- 根据数据文件自定义边界条件timeVaryingUniformFixedValue【转载】
转载自:http://blog.sina.com.cn/s/blog_e256415d0101nf9j.html 在OpenFOAM中,可以创建数据文件,自定义边界条件. 下面的例子读取outletP ...
- SpringBoot集成mybatis,同时读取一个数据库中多个数据表
SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...
- CSS工作记录
1:行内元素 设置背景图片(假设 给span) /*span 标签加背景图片 需要设置快级元素 定义高度宽度,当高度宽度很小的时候 需要设置背景图片大小*/ .filex { display: inl ...