使用Spring AOP实现业务依赖解耦
Spring IOC用于解决对象依赖之间的解耦,而Spring AOP则用于解决业务依赖之间的解耦;
统一在一个地方定义【通用功能】,通过声明的方式定义这些通用的功能以何种【方式】【织入】到某些【特定应用】里去,并且【不需要修改】特定应用的代码;
-1通用功能:<aop:aspect>如日志、安全或事务,具体的方法动作称为Advice;
-2方式:<aop:before|after-returning|around>如方法调用、字段修改和抛出异常,Spring AOP仅支持方法调用(method execution join point);
-3织入:Weaving时期:编译期,类加载期和运行期,Spring AOP仅支持运行期植入;
-4 特定应用:<aop:pointcut>,匹配连接点,也就是指定aspect适配的目标方法;
-5不需要修改:动态为目标类创建代理对象,不需要修改业务代码本身;
AspectJ的功能比Spring AOP更加全面,如果需要可以在Spring中加载AspectJ的功能模块;使用Spring AOP除了常规Spring的jar包外还需要引入aspectjweaver-[xxx].jar;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">
<!-- Config for IOC -->
<context:component-scan base-package="com.active.leo.helloworld" />
<context:property-placeholder location="classpath:service.properties" />
<import resource="webmvc-config.xml" /> <!-- Config for AOP -->
<aop:aspectj-autoproxy />
<bean id="audience" class="com.active.leo.helloworld.Audience" /> <aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance"
expression="execution(* com.active.leo.helloorld.Performer.perform(..))" /> <aop:before pointcut-ref="performance" method="takeSeats" />
<aop:before pointcut-ref="performance" method="turnOffCellPhones" />
<aop:after-returning pointcut-ref="performance" method="applaud" />
<aop:after-throwing pointcut-ref="performance" method="demandRefund" />
</aop:aspect>
</aop:config>
</bean>
</beans>
-1 <aop:aspct>表示定义一个切面,并且与名为audience的切面实现类关联;
-2 <aop:pointcut>表示定义一个名为performance的切点,并与需要进行AOP的目标业务方法绑定;execution的目标方法匹配的pattern:
execution([modifiers?] [return-type] [declaring –type?] [func-name]([param-name]) [throw-exp?] )
其他的还有within(表示被某个注解标注的所有类), this, target和args;
-3 <aop:before>表示调用目标业务perform方法之前触发AOP,perform方法一定会执行;
-4 <aop:after-returning>表示调用perform方法触发正常结束之后触发AOP;
-5 <aop:after-throwing>表示调用perform方法抛出异常后触发AOP;
-6 <aop:around>合并before和after-returning,PreceedingJoinPoint.proceed()为目标动作;可以控制执行流程,可根据情况决定是否执行perform方法;
如果目标动作有参数,可以借助arg/arg-names在切面方法中获取;
通过注解实现Spring AOP
首先要创建一个用于config的Java Bean,
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {}
然后通过@Aspect,@Pointcut,@Before,@AfterReturning,@Around等实现;所有使用AspectJ标注的前提条件是JavaBean可以被ClassLoader发现,所以需要额外添加@Component用于被IOC容器发现;被申明为Aspect的JavaBean不能用于其他Aspect的auto-proxying;
一般的Aspect的生命周期都是singleton,当然也可以设置成perflow, perthis, pertypewithin, pertarget的不同周期;实现org.springFramework.core.Ordered接口,重写getOrder()方法可以控制当前advice在目标方法周边的执行有限顺序;
public class App {
public void func1() {}
public void func2() {}
} @Aspect
public class SpringAopAspectDemo implements Ordered { @Pointcut("execution(* com.test.spring.aspectj.App.func1(..))")
public void pointcut4Func1() {} @Pointcut("execution(* com.test.spring.aspectj.App.func2(..))")
public void pointcut4Func2() {} @Around("pointcut4Func1()")
public Object doDevice(ProceedingJoinPoint pjp) throws Throwable {
//something needs to be done in aspect.
}
}
使用@Interface实现自定义注解
通过@Interface自定义注解,并结合spring AOP可以实现针对request的权限校验,
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionRequired {
boolean isReqiureAuth default false;
boolean isRequireAuzh default false;
} >>>>>>>>>>>>> @Pointcut("execution(* com.ychen.application.*.*(..)) && "
+ "@annotation(requiredPermission)")
public void pointController(RequiredPermission requiredPermission) {
// nothing
} @Around("pointController(requiredPermission)")
public Object applySecurityCheck(ProceedingJoinPoint point,
RequiredPermission requiredPermission) throws Throwable {
// auth check
}
Spring AOP使用JDK Dynamic Proxy和CGLIB对目标类进行代理:
#1 JDK Dynamic Proxy方式使用Java Reflection技术,实现InvocationHandler接口,因此要求目标类有一个Interface,并且目标方法需要在此Interface中申明,动态创建一个实现了Interface的类并在该类中调用目标类的方法;
public class PerformanceMonitorProxy implements InvocationHandler { private Object target;
public ServiceWithPerformanceMonitorProxy(Object target) {
this.target = target;
}
public static Object newProxyInstance(Object target) {
Class clazz = target.getClass();
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(),
new ServiceWithPerformanceMonitorProxy(target));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//do something before target function invocation
PerformanceMonitor.begin(method.getName());
Object result = method.invoke(target, args);
//do something after target function invocation
PerformanceMonitor.end(method.getName());
return result;
}
}
#2 CGLIB使用字节码技术,实现MethodInterceptor接口,动态生成一个目标类的子类,通过over-write去覆盖目标方法的执行,并在子类方法中调用目标方法的前后进行AOP;
public class CGlibProxy implements MethodInterceptor {
private Enhancer enhancer = new Enhancer();
public Object getProxy(Class clazz) {
enhancer.setSuperclass(clazz);
// 代理执行时会回调此this持有的intercept方法,以实现代码织入
enhancer.setCallback(this);
return enhancer.create();
} @Override
public Object intercept(Object target, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
PerformanceMonitor.begin(method.getName());
Object result = methodProxy.invokeSuper(target, args);
// 下面这样是无法执行原有方法的,因为这里的target并不是原有类的实例,而是代理类的实例
// target :
// com.dianping.aop.AdminServiceImpl$$EnhancerByCGLIB$$225da297@16dd5a9d
// Object result = method.invoke(target, args);
PerformanceMonitor.end(method.getName());
return result;
}
}
Spring一般首选JDK Dynamic Proxy进行代理,如果遇到没有实现Interface的情况则使用CGLIB,当然可以通过下属设置强制使用CGLIB;
<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>
使用Spring AOP实现业务依赖解耦的更多相关文章
- 转:spring aop 拦截业务方法,实现权限控制
难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...
- spring aop 拦截业务方法,实现权限控制
难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...
- Spring AOP基于注解的“零配置”方式实现
为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...
- 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~
Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
- spring Aop 注解
个人理解: spring Aop 是什么:面向切面编程,类似于自定义拦截操作,支持拦截之前操作@Before,拦截之后操作@After,拦截环绕操作@Around. 什么情况下使用spring Aop ...
- Spring aop+自定义注解统一记录用户行为日志
写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...
- Spring AOP源码分析(二):AOP的三种配置方式与内部解析实现
AOP配置 在应用代码中,可以通过在spring的XML配置文件applicationContext.xml或者基于注解方式来配置AOP.AOP配置的核心元素为:pointcut,advisor,as ...
- 曹工说Spring Boot源码(22)-- 你说我Spring Aop依赖AspectJ,我依赖它什么了
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
随机推荐
- PJzhang:尽快发现并下架那些侵犯公司权利的假冒APP
猫宁!!! 参考链接:https://www.freebuf.com/articles/paper/203358.html http://www.cac.gov.cn/2019-01/25/c_112 ...
- TensorFlow数据集(二)——数据集的高层操作
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 一个使用数据集进行训练和测试的完整例子. #!/usr/bin/env python # -*- coding: ...
- SpringMVC注解校验
spring注解式参数校验 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jinzhencs/article/details/5168283 ...
- C#静态类、静态构造函数,类与结构体的比较
一.静态类 静态类是不能实例化的,我们直接使用它的属性与方法,静态类最大的特点就是共享. 探究 public static class StaticTestClass{ public stati ...
- 大数加法 HDU 1002
#include <iostream> #include <cstring> #include <string> #include <cstdio> # ...
- centos安装openldap过程
1.下载软件如下,db是数据库 2.首先安装数据库db # tar xf db-4.8.30.tar.gz # cd db-4.8.30 # cd build_unix/ (# ../dist/con ...
- 115 Distinct Subsequences 不同子序列
给定一个字符串 S 和一个字符串 T,求 S 的不同的子序列中 T 出现的个数.一个字符串的一个子序列是指:通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(譬如," ...
- MDX之Case When用法
with member [Measures].[终端销售数量总计] as sum(ytd([日期].[年月].CurrentMember),[Measures].[终端销售数量]) member [M ...
- [转]Cordova android框架详解
本文转自:http://www.cnblogs.com/hubcarl/p/4202784.html 一.Cordova 核心java类说明 CordovaActivity:Cordova Activ ...
- go查询mysql到list<map>
func selects() { db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test?cha ...