AOP切面设计
场景举例
1:判断是否登录
正常编写代码
/**
* 跳转到我的页面
*/
public void toMyAttention() {
// 判断当前用户是否登录
if(LoginHelper.isLogin(this)) {
// 如果登录才跳转,进入我的页面
Intent intent = new Intent(this, WaitReceivingActivity.class);
startActivity(intent);
}else{
//跳转到登录页面,先登录
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
AOP方案:
/**
* 跳转到我的页面
*/
@CheckLogin
public void toMyAttention() {
Intent intent = new Intent(this, WaitReceivingActivity.class);
startActivity(intent);
}
2:防止view连续点击
public class SingleClickUtils {
private final static int SPACE_TIME = 500;//2次点击的间隔时间,单位ms
private static long lastClickTime;
public synchronized static boolean isSingleClick() {
long currentTime = System.currentTimeMillis();
boolean isClick;
if (currentTime - lastClickTime > SPACE_TIME) {
isClick = true;
} else {
isClick = false;
}
lastClickTime = currentTime;
return isClick;
}
}
findViewById(R.id.check_login_btn).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//单击
if (SingleClickUtils.isSingleClick()) {
}
}
});
AOP方案:
//切面类
@Aspect
public class AspectTest{
@Around("execution(* android.view.View.OnClickListener.onClick(..))")
public void onClickLitener(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Log.e(TAG, "OnClick");
if (SingleClickUtils.isSingleClick()) {
proceedingJoinPoint.proceed();
}
}
}
findViewById(R.id.check_login_btn).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
}
});
其他场景
日志 数据校验 统计 权限验证
什么是AOP
AOP是Aspect Oriented Programming的缩写,即『面向切面编程』。它和我们平时接触到的OOP都是编程的不同思想,OOP,即『面向对象编程』,它提倡的是将功能模块化,对象化,而AOP的思想,则不太一样,它提倡的是针对同一类问题的统一处理,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AspectJ
AOP是一种思想, AspectJ是一种实现方案, 可以说是对java的扩展. AspectJ是一种语言, 可以使用AspectJ语法也可以使用注解
基本概念
Aspect 切面:切面是切入点和通知的集合。
PointCut 切入点:切入点是指那些通过使用一些特定的表达式过滤出来的想要切入Advice的连接点。
Advice 通知:通知是向切点中注入的代码实现方法。
Weaving 编织:主要是在编译期使用AJC将切面的代码注入到目标中, 并生成出代码混合过的.class的过程.
实践步骤
1:build.gradle配置
apply plugin: 'com.android.application'
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
}
}
repositories {
mavenCentral()
}
final def log = project.logger
final def variants = project.android.applicationVariants
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return;
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.aopdemo"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.aspectj:aspectjrt:1.8.9'
testCompile 'junit:junit:4.12'
}
因为AspectJ是对java的扩展,而且是完全兼容java的。但是编译时得用Aspect专门的编译器,这里的配置就是使用Aspect的编译器,单独加入aspectj依赖是不行的。
2:创建切面类
用来处理具体逻辑
@Aspect
public class CheckLoginAspectJ {
private static final String TAG = "CheckLogin";
/**
* 找到处理的切点
* * *(..) 可以处理CheckLogin这个类所有的方法
*/
@Pointcut("execution(@com.example.aopdemo.login.CheckLogin * *(..))")
public void executionCheckLogin() {
}
/**
* 处理切面
*
* @param joinPoint
* @return
*/
@Around("executionCheckLogin()")
public Object checkLogin(ProceedingJoinPoint joinPoint) throws Throwable {
Log.i(TAG, "checkLogin: ");
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
CheckLogin checkLogin = signature.getMethod().getAnnotation(CheckLogin.class);
if (checkLogin != null) {
Context context = (Context) joinPoint.getThis();
if (MyApplication.isLogin) {
Log.i(TAG, "checkLogin: 登录成功 ");
return joinPoint.proceed();
} else {
Log.i(TAG, "checkLogin: 请登录");
Toast.makeText(context, "请登录", Toast.LENGTH_SHORT).show();
return null;
}
}
return joinPoint.proceed();
}
}
在Pointcut这里,execution也就是以方法执行时为切点,触发Aspect类。而execution里面的字符串是触发条件,也是具体的切点。我来解释一下参数的构成。“execution(@com.example.aopdemo.login.CheckLogin * *(…))”这个条件是所有加了CheckLogin注解的方法或属性都会是切点,范围比较广。
**:表示是任意包名
…:表示任意类型任意多个参数
+: 表示任意子类
PointCut 切入点:
Advice 通知
创建完Aspect类之后,还需要一个注解类,它的作用是:哪里需要做切点,那么哪里就用注解标注一下,这样方便快捷。
3:创建注解类
@Target(ElementType.METHOD) //可以注解在方法 上
@Retention(RetentionPolicy.RUNTIME) //运行时(执行时)存在
public @interface CheckLogin {
}
4:使用注解
@Target(ElementType.METHOD) //可以注解在方法 上
@Retention(RetentionPolicy.RUNTIME) //运行时(执行时)存在
public @interface CheckLogin {
}
触发流程: