Spring Boot切面Aspect Aop思想

Aspect的作用范围

为了降低耦合,每一个类该独自尽量完善一项固定的功能,但是程序中却需要调用很多类,排好顺序去实现功能,这个时候就需要使用切面将其他类来引用到一个类中,也就是说哪里需要我去哪里使用。

拦截规则的注释编写

package com.springboot.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
//声明一个方法,@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
@Retention(RetentionPolicy.RUNTIME)
//1、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
//2、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
//3、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
@Documented
//@Documented注解标记的元素,Javadoc工具会将此注解标记元素的注解信息包含在javadoc中。默认,注解信息不会包含在Javadoc中。示例如下:
//编写拦截规则的注释
public @interface action 
{
	String name();
}

配置文件Aopconfig 的编写

package com.springboot.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
//声明是配置类
@ComponentScan("com.springboot.aop")
//扫描路径
@EnableAspectJAutoProxy
//启动AOP切面
public class Aopconfig 
{
	
}

注释式拦截的add操作的编写

package com.springboot.aop;

import org.springframework.stereotype.Service;

@Service
//编写使用注解的被拦截类
public class DemoAnnotationService 
{
	@action(name="注释式拦截的add操作")
	public void add() 
	{
		System.out.println("注释式拦截的add");
	}
}

方法拦截的add的操作编写

package com.springboot.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoMethodService 
{
	public void add() 
	{
		System.out.println("方法拦截的add");
	}
}

logAspect切面的编写

package com.springboot.aop;

import java.lang.reflect.Method;

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.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;;
@Aspect
@Component
//注解为Spring容器管理的bean
public class LogAspect
{
	@Pointcut("@annotation(com.springboot.aop.action)")
	//声明一个切点
	public void annotationPointCut() {}
	@After("annotationPointCut()")
	//声明一个建言并且使用pointcut的切点
	public void after(JoinPoint joinpoint) 
	{
		MethodSignature signature=(MethodSignature)joinpoint.getSignature();
		Method method=signature.getMethod();
		action antion1=method.getAnnotation(action.class);
		System.out.println("注释式拦截"+antion1.name());
	}
	@Before("execution(* com.springboot.aop.DemoMethodService.*(..))")
	//声明一个建言,使用拦截规则来作为参数。
	public void before(JoinPoint joinpoint) 
	{
		MethodSignature signature=(MethodSignature)joinpoint.getSignature();
		Method method=signature.getMethod();
		System.out.println("方法规则式拦截"+method.getName()+"的操作");
	}
}

Main的编写

package com.springboot.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main 
{
	public static void main(String ager[]) 
	{
		AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Aopconfig.class);
		DemoAnnotationService demoannotation=context.getBean(DemoAnnotationService.class);
		
		DemoMethodService demomethodservice=context.getBean(DemoMethodService.class);
		
		demoannotation.add();
		
		demomethodservice.add();
		
		context.close();
	}
}
//通过反射获取到注解上的属性

运行结果

在这里插入图片描述
打印出add方法,并且将注释的切片方法也打印出来,after,和before方法的位置注意。

原创文章 24 获赞 0 访问量 1235

猜你喜欢

转载自blog.csdn.net/FYM1211/article/details/105856706
今日推荐