Getting Started with Spring AOP Aspects

 

 

Java annotations

 

 

The first indicates that the method of the parent class is overridden

The second means that the method has expired (but it can still be used, and a warning will appear)

The third means use the expired method to ignore the warning

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Aop

 

Requirements such as certain methods require administrator privileges to run.

  1. Hard-code in a method that can only be run by administrators to determine whether the current user is an administrator (intrusive high)
  2. Use aop for faceted interception of specific methods (low intrusiveness)

 

Define the aspect to intercept before  defining the method annotated with adminOnly and then insert the code @before before running the method

 

 

package com.Springboot.myBoot.zujie;

 

import java.lang.annotation.Documented;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

 

import javax.lang.model.element.Element;

import javax.persistence.Inheritance;

 

import org.hibernate.validator.internal.xml.ElementType;

import org.mockito.Incubating;

 

 

@Target({java.lang.annotation.ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Inheritance

@Documented

public @interface Desciption {

String value();  //可以不加

 

}

 

 

 

 

定义注解

 

package com.Springboot.myBoot.zujie;

 

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.stereotype.Component;

 

 

@Aspect

@Component

public class qiemian {

/*@Component  @component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>*/

 

@Pointcut("@annotation(Desciption)")

public void zdy(){

 

}

 

@Before("zdy()")

public void Front(){

System.out.println("我在特定的方法之前运行");

}

 

@After("zdy()")

public void behind(){

System.out.println("我在特定的方法之前运行");

}

}

 

 

@RequestMapping("/qiemian")

@Desciption("zdy")

public String zdy(){

     System.out.println("我是一个切面方法  我加了自定义注解");

     return "success";

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

一个参数 long类型

 

 

多个参数 long类型开头

 

 

两个参数 long类型开头和String

 

 

 

 

类级别的需要将注解的范围修改。Type

 

 

 

问号部分可以省略

 

 

 

 

        

 

获取参数

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370197&siteId=291194637