Spring基础-AOP
AOP
- AOP:面向切面编程
- Spring 的Aop是为了解耦。弥补OOP的不足。
- Spring支持Aspect J的注解式切面编程
- 使用@Aspect是一个切面
- 使用@After、@Before、@Around定义建言,可直接将拦截规则作为参数。
- @After、@Before、@Around参数的拦截规则则为切点,为了复用,可使用@PointCut定义拦截规则,然后在@After、@Before、@Around的参数中调用
- 符合条件的每一个被拦截处为连接点(JoinPoint)
使用注解的被拦截类
package ch1.aop;
import org.springframework.stereotype.Service;
/**
* 编写使用注解的被拦截类
*/
@Service
public class DemoAnnotationService {
@Action( name="注解式拦截的add操作")
public void add(){}
}
使用方法规则被拦截类
package ch1.aop;
import org.springframework.stereotype.Service;
/**
* 编写使用方法规则被拦截类
*/
@Service
public class DemoMethodService {
public void add(){}
}
切面
package ch1.aop;
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;
import java.lang.reflect.Method;
@Aspect // 通过@Aspect 注解声明一个切面
@Component // 通过@Component 让此切面成为Spring容器管理的Bean
public class LogAspect {
@Pointcut("@annotation(ch1.aop.Action)") // 声明切点
public void annotationPontCut(){};
@After("annotationPontCut()") // 声明一个建言,并使用@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()); //通过反射可获得注解上的属性
}
@Before("execution(* ch1.aop.DemoMethodService.*(..))") // 声明一个建言,直接使用拦截规则作为参数
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截。"+method.getName());
}
}
配置类
package ch1.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("ch1.aop")
@EnableAspectJAutoProxy // 开始Spring对AspectJ的支持
public class AopConfig {
}
运行结果
Spring基础-AOP的更多相关文章
- Spring学习笔记(二)Spring基础AOP、IOC
Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...
- Spring基础——AOP通知
spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...
- Java : Spring基础 AOP
简单的JDK动态代理例子(JDK动态代理是用了接口实现的方式)(ICar是接口, GoogleCar是被代理对象, MyCC是处理方法的类): public class TestCar { publi ...
- Spring基础知识之基于注解的AOP
背景概念: 1)横切关注点:散布在应用中多处的功能称为横切关注点 2)通知(Advice):切面完成的工作.通知定了了切面是什么及何时调用. 5中可以应用的通知: 前置通知(Before):在目标方法 ...
- Spring基础系列--AOP织入逻辑跟踪
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...
- Spring基础篇——Spring的AOP切面编程
一 基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...
- Spring基础系列-AOP源码分析
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...
- Java基础-SSM之Spring的AOP编程
Java基础-SSM之Spring的AOP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法.它是对OOP的 ...
- JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解
在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...
- Spring基础(二)_面向切面(AOP)
面向切面编程 面向切面编程[AOP,Aspect Oriented Programming]:通过预编译方式和运行期间动态代理实现程序功能的统一维护的技术.AOP 是 Spring 框架中的一个重要内 ...
随机推荐
- 学习unigui【25】关于图标
网上有不少介绍. 自己的经验: 是否需要下载文件fontawesome-free-6.5.1-web(),没有研究.说ext_js已经下载配套了. 我很懒,得过且过. 1.下载fontawesome- ...
- BuildAssetBundleOption.DisableWriteTypeTree和粒度问题
- 安装yml 与 wget
一.备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...
- 如何对 Java 的垃圾回收进行调优?
如何对 Java 的垃圾回收进行调优? Java 垃圾回收的调优涉及多个方面,从选择合适的垃圾回收器到调整堆内存的大小.配置 GC 参数等,下面是一些常见的调优方法: 1. 选择合适的垃圾回收器 不同 ...
- mac系统安装GNU-sed
经过网上查资料,发现 由于 mac 系统与 linux 系统的差异,mac自带的sed命令,因为其是基于bsd,所以与常用的gnu不一样,安装gnu-sed 可正常使用: 1.brew install ...
- 备注一下,SolidColorBrush,自定义颜色
new SolidColorBrush((Color)ColorConverter.ConvertFromString("#27212B"))
- Spring基于注解的IOC配置
目录 基于注解的IOC配置 1.用于创建对象的注解 2.用于注入数据的 3.用于改变作用范围的 和生命周期相关 基于注解的IOC配置 曾经XML的配置 <bean id="accoun ...
- SpringMVC框架第一天
目录 SpringMVC的基本概念 三层架构和MVC 三层架构 MVC模型 MVC概述 SpringMVC是什么 SpringMVC在三层架构的位置 SpringMVC的优势 SpringMVC的入门 ...
- Java IO--利用内存流实现转大写的操作
package demo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java ...
- SSM整合2
目录 目录结构 数据库 pom.xml依赖 domain dao层 mapper service层 exception包 contorller层 配置文件 applicationContext.xml ...