BeforeAop.java 1.9 KB
Newer Older
Z
zhuangqian 已提交
1
/**
Z
update  
zhuangqian 已提交
2
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
Z
zhuangqian 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
S
smallchill 已提交
16
package org.springblade.core.aop;
Z
zhuangqian 已提交
17 18 19 20 21 22

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
S
smallchill 已提交
23 24 25
import org.springblade.core.annotation.Before;
import org.springblade.core.intercept.Interceptor;
import org.springblade.core.toolbox.kit.HttpKit;
Z
zhuangqian 已提交
26 27
import org.springframework.stereotype.Component;

S
smallchill 已提交
28 29
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
Z
zhuangqian 已提交
30 31 32

/**
 * before拦截
33
 * @author zhuangqian
Z
zhuangqian 已提交
34 35 36 37 38
 */
@Aspect
@Component
public class BeforeAop {

S
smallchill 已提交
39
	@Pointcut(value = "@annotation(org.springblade.core.annotation.Before)")
Z
zhuangqian 已提交
40 41 42 43 44 45
	private void cutBefore() {

	}

	@Around("cutBefore()")
	public Object doBefore(ProceedingJoinPoint point) throws Throwable {
Z
zhuangqian 已提交
46
		HttpServletRequest request = HttpKit.getRequest();
Z
zhuangqian 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
		MethodSignature ms = (MethodSignature) point.getSignature();
		Method method = ms.getMethod();
		Object[] args = point.getArgs();
		Class<?> clazz = point.getTarget().getClass();
		Before before = method.getAnnotation(Before.class);
		Interceptor ic = before.value().newInstance();
		Object result = ic.intercept(new Invocation(clazz, method, args, request));
		if (null == result) {
			return point.proceed();
		} else {
			return result;
		}
	}
}