プロジェクトの基本的な構成は、記事を参照してくださいSpringBoot MyEclipseの新しいプロジェクトを使用して、はじめSpringBoot、新しい使用のMyEclipse SpringBootプロジェクトは、バージョン2.2.1にアップグレードし、この例のspringbootすることができます。
1.のpom.xmlはAOPサポートを追加します
<!-- 引入aop切面支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
カスタムアノテーションを作成します。2.
package com.qfx.common.annotation;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface LoginAnno {
}
メタ注釈の解釈:
java.lang.annotationのクラスは、他のノート(カスタム注釈での使用のメタアノテーションする必要があります)に4元のノート、特別な注意事項を提供します。
@Documented -ノートはのJavaDocに含まれるかどうか
@Retention -何注釈使用時に
何のためのノート- @Target
サブクラスコメントを継承できるようにするかどうか- @Inheritedを
コメントを解析し、カスタムを作成します3。
package com.qfx.common.annotation;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* <h5>描述:通过@Aspect注解使该类成为切面类</h5>
*/
@Aspect
@Component
public class LoginAnnoImpl {
@Pointcut("@annotation(com.qfx.common.annotation.LoginAnno)")
private void cut() {
}
/**
* <h5>功能:前置通知</h5>
*/
@Before("cut()")
public void before() {
System.out.println("自定义注解生效了");
}
}
このように準備上のカスタム注釈が終了し、呼び出しで見てみましょう
4.カスタム注釈
package com.qfx.common.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qfx.common.annotation.LoginAnno;
@RestController
@RequestMapping("login")
public class LoginController {
@RequestMapping("reg")
public String reg(String userName) {
return "用户[" + userName +"]注册成功~!";
}
@RequestMapping("login")
@LoginAnno
public String login(String userName) {
return "欢迎您:" + userName;
}
}
4.完全なプロジェクト構造