博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring 4.x】Spring基础配置_AOP
阅读量:3943 次
发布时间:2019-05-24

本文共 4367 字,大约阅读时间需要 14 分钟。

1.名词解释

AOP:面向切面编程,相对于OOP面向对象编程。

Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足。

Spring支持AspectJ的注解式切面编程。

(1)使用@Aspect声明是一个切面

(2)使用@After@Before@Around定义建言(advice),可直接将拦截规则(切点)作为参数
(3)其中@After@Before@Around参数的拦截规则为切点(PointCut),为了使切点复用,可使用@PointCut专门定义拦截规则,然后在@After@Before@Around的参数中调用。
(4)其中符合条件的每一个被拦截处为连接点(JoinPoint)。

2. 示例

下面演示 基于注解拦截 基于方法规则拦截 两种方式,演示一种模拟记录操作的日志系统的实现。其中注解式拦截能够很好地控制要拦截的粒度和获得更丰富的信息,Spring本身在事务处理(@Transcational)和数据缓存(@Cacheable等)上面都使用此种形式的拦截。

(1)添加spring aop支持及AspectJ依赖。

  
 
 
org.springframework
 
spring-aop
 
4.1.6.RELEASE
 
   
 
 
org.aspectj
   
aspectjrt
 
1.8.5
 
 
 
org.aspectj
 
aspectjweaver
 
1.8.5
 

(2)编写拦截规则的注解。

package com.wisely.highlight_spring4.ch1.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)@Documentedpublic @interface Action {
String name();}注解本身是没有功能的,就和xml一样。注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置。注解的功能来自用这个注解的地方。

(3)编写使用注解的被拦截类。

package com.wisely.highlight_spring4.ch1.aop;import org.springframework.stereotype.Service;@Servicepublic class DemoAnnotationService {
@Action(name="注解式拦截的add操作") public void add(){
}}

(4)编写使用方法规则被拦截类。

package com.wisely.highlight_spring4.ch1.aop;import org.springframework.stereotype.Service;@Servicepublic class DemoMethodService {
public void add(){
}}

(5)编写切面。

package com.wisely.highlight_spring4.ch1.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.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;@Aspect //1 声明一个切面@Component //2 让此切面成为Spring容器管理的Bean public class LogAspect {
@Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") //3 声明切点 public void annotationPointCut(){
}; @After("annotationPointCut()") //4 声明一个建言,并使用@PointCut定义的切点。 public void after(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.out.println("注解式拦截 " + action.name()); //5 通过反射可获得注解上的属性,然后做日志记录相关的操作 } @Before("execution(* com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") //6 声明一个建言,此建言直接使用拦截规则作为参数。 public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截,"+method.getName()); }}①通过@Aspect注解声明一个切面。②通过@Component让此切面成为Spring容器管理的Bean。③通过@PointCut注解声明切点。④通过@After注解声明一个建言,并使用@PointCut定义的切点。⑤通过反射可获得注解上的属性,然后做日志记录相关的操作,下面的相同。⑥通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数。

(6)配置类。

package com.wisely.highlight_spring4.ch1.aop;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@ComponentScan("com.wisely.highlight_spring4.ch1.aop")@EnableAspectJAutoProxy //1 开启Spring对AspectJ代理的支持public class AopConfig {
}①使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持。

(7)运行。

package com.wisely.highlight_spring4.ch1.aop;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); //1 DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); context.close(); }}

转载地址:http://rhjwi.baihongyu.com/

你可能感兴趣的文章
虚拟机VMware中实现linux与windows的共享
查看>>
undefined reference问题总结
查看>>
souce insight 3.5 修改背景颜色
查看>>
Linux 关闭/开启图形界面(X-window) 命令
查看>>
debug 打印 开关 设计(for c || C++)
查看>>
vmware中虚拟机和主机ping不通的问题。
查看>>
从“冷却时间”谈产品设计
查看>>
常用shell脚本
查看>>
长网站 转换为 短网址 的原理
查看>>
基于http协议的C语言客户端代码
查看>>
我常用的makefile之产生优秀的.depend文件
查看>>
VMware无法识别USB设备的解决方法 以及 从虚拟机中断开USB设备,使其重新连接到windows主机上
查看>>
linux下C代码、C++代码和命令行方式,完成字符集编码的转换
查看>>
写代码就像写作文
查看>>
常用shell特殊符号变量一览
查看>>
如何做事
查看>>
架构实践 - 1. 架构风格
查看>>
架构实践 - 3. 基于事件系统的demo
查看>>
架构实践 - 4. 架构设计之进程通信(独立构件风格)
查看>>
架构实践 - 5. 基于进程通信的demo
查看>>