{"id":2607,"date":"2024-06-26T08:37:25","date_gmt":"2024-06-26T00:37:25","guid":{"rendered":"https:\/\/usei.cn\/?p=2607"},"modified":"2024-06-26T09:08:16","modified_gmt":"2024-06-26T01:08:16","slug":"java-springboot-%e8%87%aa%e5%ae%9a%e4%b9%89%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/usei.cn\/index.php\/2024\/06\/26\/java-springboot-%e8%87%aa%e5%ae%9a%e4%b9%89%e6%b3%a8%e8%a7%a3\/","title":{"rendered":"Java Springboot \u81ea\u5b9a\u4e49\u6ce8\u89e3"},"content":{"rendered":"\n<p>\u5143\u6ce8\u89e3\u5c31\u662f\u6ce8\u89e3\u7684\u6ce8\u89e3\uff0c\u7528\u6765\u63cf\u8ff0\u6ce8\u89e3\u7684<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>@Retention \u5b9a\u4e49\u8be5\u6ce8\u89e3\u7684\u751f\u547d\u5468\u671f<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>RetentionPolicy.SOURCE \uff1a\u4f5c\u7528\u4e8e\u6e90\u7801\u9636\u6bb5\uff0c\u6bd4\u5982\u5e38\u89c1\u7684 @Override, @SuppressWarnings\uff1b<\/li>\n\n\n\n<li>RetentionPolicy.CLASS \uff1a\u4f5c\u7528\u4e8e\u5b57\u8282\u7801\u9636\u6bb5<\/li>\n\n\n\n<li>RetentionPolicy.RUNTIME \uff1a\u4f5c\u7528\u4e8e\u8fd0\u884c\u9636\u6bb5<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>@Target \u5b9a\u4e49\u8be5\u6ce8\u89e3\u7684\u4f5c\u7528\u8303\u56f4\u00a0<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>ElementType.TYPE \uff1a\u7528\u4e8e\u6ce8\u89e3\u5230\u7c7b\uff0c\u63a5\u53e3\u3001\u679a\u4e3e\u7c7b<\/li>\n\n\n\n<li>ElementType.FIELD\uff1a\u5b57\u6bb5\uff0c\u5305\u62ec\u679a\u4e3e\u7c7b\u5e38\u91cf<\/li>\n\n\n\n<li>ElementType.METHOD\uff1a\u65b9\u6cd5\u58f0\u660e<\/li>\n\n\n\n<li>ElementType.PARAMETER\uff1a\u53c2\u6570\u58f0\u660e<\/li>\n\n\n\n<li>ElementType.CONSTRUCTOR\uff1a\u6784\u9020\u5668\u58f0\u660e<\/li>\n\n\n\n<li>ElementType.LOCAL_VARIABLE \uff1a\u5c40\u90e8\u53d8\u91cf\u58f0\u660e<\/li>\n\n\n\n<li>ElementType.ANNOTATION_TYPE \uff1a\u7528\u4e8e\u6ce8\u89e3\u58f0\u660e\uff0c\u5373\u6ce8\u89e3\u7684\u6ce8\u89e3\uff0c\u5143\u6ce8\u89e3<\/li>\n\n\n\n<li>ElementType.PACKAGE \uff1a\u5305\u58f0\u660e<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5176\u4ed6\u6ce8\u89e3<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@Document \u6ce8\u89e3\u5c06\u751f\u6210\u5230javadoc\u4e2d<\/li>\n\n\n\n<li>@Deprecated\u00a0 \u8868\u793a\u8fc7\u65f6\u7684\u7c7b<\/li>\n\n\n\n<li>@Inherited \u662f\u5426\u5141\u8bb8\u5b50\u7c7b\u7ee7\u627f\u8be5\u6ce8\u89e3<\/li>\n\n\n\n<li>@SuppressWarnings \u7f16\u8bd1\u5668\u5ffd\u7565\u6389\u65e0\u6cd5\u8bc6\u522b\u7684\u8b66\u544a\u540d<\/li>\n\n\n\n<li>@Override \u6807\u6ce8\u7684\u65b9\u6cd5\u91cd\u8f7d\u4e86\u7236\u7c7b\u7684\u65b9\u6cd5<\/li>\n<\/ol>\n\n\n\n<p>\u5b9e\u4f8b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * @author FWG\n *\/\n@Retention(RetentionPolicy.RUNTIME)\n@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})\n@Documented\npublic @interface PayableAnnotation\n{\n    String businessName();\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u83b7\u53d6\u6ce8\u89e3\u503c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@MyAnnotation1(name = \"abc\")\npublic class Demo1 {\n\n    @MyAnnotation1(name = \"xyz\")\n    private Integer age;\n\n    @MyAnnotation2(model = TranscationModel.Read)\n    public void list() {\n        System.out.println(\"list\");\n    }\n\n    @MyAnnotation3(models = {TranscationModel.Read, TranscationModel.Write})\n    public void edit() {\n        System.out.println(\"edit\");\n    }\n\n    public void hello1(@IsNotNull(true) String name) {\n        System.out.println(\"hello:\" + name);\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u83b7\u53d6\u7c7b\u4e0a\u7684\u6ce8\u89e3\nMyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);\nSystem.out.println(annotation1.name());\/\/abc\n\n\/\/ \u83b7\u53d6\u65b9\u6cd5\u4e0a\u7684\u6ce8\u89e3\nMyAnnotation2 myAnnotation2 = Demo1.class.getMethod(\"list\").getAnnotation(MyAnnotation2.class);\nSystem.out.println(myAnnotation2.model());\/\/Read\n\n\/\/ \u83b7\u53d6\u5c5e\u6027\u4e0a\u7684\u6ce8\u89e3\nMyAnnotation1 myAnnotation1 = Demo1.class.getDeclaredField(\"age\").getAnnotation(MyAnnotation1.class);\nSystem.out.println(myAnnotation1.name());\/\/ xyz\n\n\/\/ \u83b7\u53d6\u53c2\u6570\u4e0a\u7684\u6ce8\u89e3\nfor (Parameter parameter : demo3.getClass().getMethod(\"hello1\", String.class).getParameters()) {\n    IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);\n    if(annotation != null){\n        System.out.println(annotation.value());\/\/true\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u4f7f\u7528\u5b9e\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u5b9a\u4e49\u6ce8\u89e3\u63a5\u53e3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.lang.annotation.*;\n\n\/**\n * \u6253\u5370\u51fa\u64cd\u4f5c\u65e5\u5fd7\n * @author \n * @create 2019\/11\/4\n *\/\n\/\/\u5b9a\u4e49\u8be5\u6ce8\u89e3\u7684\u4f5c\u7528\u8303\u56f4\n@Target({ElementType.TYPE, ElementType.METHOD})\n\/\/\u5b9a\u4e49\u8be5\u6ce8\u89e3\u7684\u751f\u547d\u5468\u671f\n@Retention(RetentionPolicy.RUNTIME)\n\/\/\u6ce8\u89e3\u5c06\u751f\u6210\u5230javadoc\u4e2d\n@Documented\npublic @interface SystemLog {\n    \/**\n     * \u65e5\u5fd7\u5185\u5bb9\n     * @return\n     *\/\n    String message() default \"\";\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u5b9e\u73b0\u63a5\u53e3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.example.shiro.config.SystemLog;\nimport lombok.extern.slf4j.Slf4j;\nimport org.aspectj.lang.JoinPoint;\nimport org.aspectj.lang.annotation.*;\nimport org.aspectj.lang.reflect.MethodSignature;\nimport org.springframework.core.annotation.Order;\nimport org.springframework.stereotype.Component;\n\nimport java.lang.reflect.Method;\n\n\/**\n * \u65e5\u5fd7\u62e6\u622a\n * @author \n * @create 2019\/11\/4\n *\/\n@Slf4j\n\/\/\u4f7f\u7528order\u5c5e\u6027\uff0c\u8bbe\u7f6e\u8be5\u7c7b\u5728spring\u5bb9\u5668\u4e2d\u7684\u52a0\u8f7d\u987a\u5e8f\n@Order(10)\n\/\/\u4f5c\u7528\u662f\u628a\u5f53\u524d\u7c7b\u6807\u8bc6\u4e3a\u4e00\u4e2a\u5207\u9762\u4f9b\u5bb9\u5668\u8bfb\u53d6\n@Aspect\n\/\/\u628a\u666e\u901a\u7c7b\u5b9e\u4f8b\u5316\u5230spring\u5bb9\u5668\u4e2d\n@Component\npublic class OuterServiceAop {\n\n    @Pointcut(\"@annotation(com.example.shiro.config.SystemLog)\")\n    public void serviceAop(){}\n\n    @Before(\"serviceAop()\")\n    public void doBefore(JoinPoint joinPoint) {\n        log.info(\"\u524d\u7f6e\u901a\u77e5\");\n    }\n\n    @After(\"serviceAop()\")\n    public void doAfter(JoinPoint joinPoint) {\n        log.info(\"\u540e\u7f6e\u901a\u77e5\");\n    }\n\n    @AfterReturning(pointcut = \"serviceAop()\", returning = \"res\")\n    public void doAfterReturning(JoinPoint joinPoint, Object res) {\n        log.info(\"\u65e5\u5fd7\u8fd4\u56de\u901a\u77e5\");\n        \/\/\u8bf7\u6c42\u65b9\u6cd5\n        String method = joinPoint.getSignature().getDeclaringTypeName() + \".\" + joinPoint.getSignature().getName();\n        log.info(\"method:\"+ method);\n\n        \/\/\u65e5\u5fd7\u63cf\u8ff0\n        MethodSignature signature = (MethodSignature) joinPoint.getSignature();\n        Method method1 = signature.getMethod();\n        SystemLog annotation = method1.getAnnotation(SystemLog.class);\n        String message = annotation.message();\n        log.info(message);\n    }\n\n    @AfterThrowing(pointcut = \"serviceAop()\", throwing = \"e\")\n    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {\n        log.info(\"\u5f02\u5e38\u901a\u77e5\");\n        log.info(\"\u5f02\u5e38\u4fe1\u606f\uff1a\" + e.getMessage());\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728\u9700\u8981\u7684\u7c7b\u6216\u65b9\u6cd5\u4e2d\u589e\u52a0\u6ce8\u89e3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.example.shiro.config.SystemLog;\nimport com.example.shiro.entity.User;\nimport org.apache.shiro.SecurityUtils;\nimport org.apache.shiro.authc.AuthenticationException;\nimport org.apache.shiro.authc.UsernamePasswordToken;\nimport org.apache.shiro.subject.Subject;\nimport org.springframework.web.bind.annotation.PostMapping;\nimport org.springframework.web.bind.annotation.RequestMapping;\n\nimport org.springframework.web.bind.annotation.RestController;\nimport com.example.shiro.common.BaseController;\n\n\/**\n * &lt;p>\n *  \u524d\u7aef\u63a7\u5236\u5668\n * &lt;\/p>\n *\n * @author \n * @since 2019-11-01\n *\/\n@RestController\n@RequestMapping(\"\/user\")\npublic class UserController extends BaseController {\n\n    @PostMapping(\"\/login\")\n    @SystemLog(message = \"\u7528\u6237\u767b\u5f55\")\n    public String login(User user) {\n        ...\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5143\u6ce8\u89e3\u5c31\u662f\u6ce8\u89e3\u7684\u6ce8\u89e3\uff0c\u7528\u6765\u63cf\u8ff0\u6ce8\u89e3\u7684 \u5b9e\u4f8b\u4ee3\u7801 \u83b7\u53d6\u6ce8\u89e3\u503c 3.\u4f7f\u7528\u5b9e\u4f8b 3.1 \u5b9a\u4e49\u6ce8\u89e3\u63a5\u53e3 3.2 \u5b9e\u73b0\u63a5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,75],"tags":[],"class_list":["post-2607","post","type-post","status-publish","format-standard","hentry","category-java","category-springboot"],"_links":{"self":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2607"}],"version-history":[{"count":18,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2607\/revisions"}],"predecessor-version":[{"id":2631,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2607\/revisions\/2631"}],"wp:attachment":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}