spring-AOP实战(最新,实用,简单,爆炸!!!!!)

如果想看概念:spring-AOP理论(最新,实用,简单,爆炸!!!!!)

废话不多说直接开始干

(注:本文前提是你已经倒入了其他启动项目所有需要的spring包,且配置好)

没有的话请去我的github下载(包含aopdemo):下载项目

第一步:倒入AOP相关jar包,本文实用POM

                <spring.version>4.1.1.RELEASE</spring.version>
                <aspectj.version>1.6.11</aspectj.version>
                <!--spring aop包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
                <!--使用AspectJ方式注解需要相应的包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<!--使用AspectJ方式注解需要相应的包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency

第二步:编写切点(本文使用自定义标签)

废话不多说,上手直接干。

自定义一个注解:作为每次切入的点

@Target,这个修饰 用来说明该注解可以被声明在那些元素之前。(详情我等在发篇博客解答)

ElementType.METHOD:说明该注解只能被声明在一个类的方法前。

@Retention,这个修饰用来说明该注解的生命周期

RetentionPolicy.RUNTIME  : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解

import java.lang.annotation.Retention;
@AuthChecker
	@RequestMapping(value = "/aop/http/user_info")
	public String callSomeInterface() {
		return "ok in user_info.";
	}

import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.ElementType.METHOD;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface AuthChecker {}

第三步:编写切面

直接上代码不逼逼,

下面切面是一个验证登录token的小方法,其他方法修改切面内容就可以

@Around,注解是环绕通知

其他通知相同:

@Before,@After等。。。。


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
@Aspect
public class HttpAopAdviseDefine {

	// 定义一个 Pointcut, 使用 切点表达式函数 来描述对哪些 Join point 使用 advise.
	@Pointcut("@annotation(com.shen.utils.AuthChecker)")
	public void pointcut() {
	}

	// 定义 advise
	@Around("pointcut()")
	public Object checkAuth(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("成功进入标签切面");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		// 检查用户所传递的 token 是否合法
		String token = getUserToken(request);
		if (!token.equalsIgnoreCase("123456")) {
			return "erro,no in!";
		}

		return joinPoint.proceed();
	}

	private String getUserToken(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return "";
		}
		for (Cookie cookie : cookies) {
			if (cookie.getName().equalsIgnoreCase("user_token")) {
				return cookie.getValue();
			}
		}
		return "";
	}
}

第三步:使用注解

            

@AuthChecker
	@RequestMapping(value = "/aop/http/user_info")
	public String callSomeInterface() {
		return "ok in user_info.";
	}

http请求就看到结果,被拦截,只有当有参数token=123456的时候才能正确返回

测试效果你们自己来~~稳稳的不叭叭,用的好请点赞


猜你喜欢

转载自blog.csdn.net/weixin_37352094/article/details/80416970
今日推荐