{"id":135111,"date":"2024-10-31T07:56:42","date_gmt":"2024-10-30T23:56:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/135111\/"},"modified":"2024-10-31T07:56:42","modified_gmt":"2024-10-30T23:56:42","slug":"spring%e6%a1%86%e6%9e%b6%e6%94%94%e6%88%aa%e6%95%b8%e6%93%9a%e5%ba%ab%e7%95%b0%e5%b8%b8%ef%bc%8c%e6%8f%90%e5%8d%87%e6%87%89%e7%94%a8%e7%a9%a9%e5%ae%9a%e6%80%a7%ef%bc%88spring%e6%94%94%e6%88%aa","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/135111\/","title":{"rendered":"Spring\u6846\u67b6\u6514\u622a\u6578\u64da\u5eab\u7570\u5e38\uff0c\u63d0\u5347\u61c9\u7528\u7a69\u5b9a\u6027\uff08spring\u6514\u622a\u6578\u64da\u5eab\u7570\u5e38\uff09"},"content":{"rendered":"<h1 id=\"spring%e6%a1%86%e6%9e%b6%e6%94%94%e6%88%aa%e6%95%b8%e6%93%9a%e5%ba%ab%e7%95%b0%e5%b8%b8%ef%bc%8c%e6%8f%90%e5%8d%87%e6%87%89%e7%94%a8%e7%a9%a9%e5%ae%9a%e6%80%a7-HZuEOSEVfO\">Spring\u6846\u67b6\u6514\u622a\u6578\u64da\u5eab\u7570\u5e38\uff0c\u63d0\u5347\u61c9\u7528\u7a69\u5b9a\u6027<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Spring\u6846\u67b6\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684Java\u61c9\u7528\u958b\u767c\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6a5f\u5236\u4f86\u6514\u622a\u548c\u8655\u7406\u6578\u64da\u5eab\u7570\u5e38\uff0c\u5f9e\u800c\u63d0\u5347\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Spring\u6846\u67b6\u6514\u622a\u6578\u64da\u5eab\u7570\u5e38\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%95%b0%e5%b8%b8%e7%9a%84%e9%a1%9e%e5%9e%8b-HZuEOSEVfO\">\u6578\u64da\u5eab\u7570\u5e38\u7684\u985e\u578b<\/h2>\n<p>\u5728\u4f7f\u7528\u6578\u64da\u5eab\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u5404\u7a2e\u7570\u5e38\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9023\u63a5\u7570\u5e38\uff1a<\/strong>\u7121\u6cd5\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u53ef\u80fd\u7531\u65bc\u914d\u7f6e\u932f\u8aa4\u6216\u6578\u64da\u5eab\u670d\u52d9\u5668\u6545\u969c\u3002<\/li>\n<li><strong>\u67e5\u8a62\u7570\u5e38\uff1a<\/strong>\u57f7\u884cSQL\u67e5\u8a62\u6642\u51fa\u73fe\u932f\u8aa4\uff0c\u4f8b\u5982\u8a9e\u6cd5\u932f\u8aa4\u6216\u6578\u64da\u985e\u578b\u4e0d\u5339\u914d\u3002<\/li>\n<li><strong>\u4e8b\u52d9\u7570\u5e38\uff1a<\/strong>\u5728\u4e8b\u52d9\u8655\u7406\u904e\u7a0b\u4e2d\u51fa\u73fe\u7684\u7570\u5e38\uff0c\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<h2 id=\"spring%e6%a1%86%e6%9e%b6%e7%9a%84%e7%95%b0%e5%b8%b8%e8%99%95%e7%90%86%e6%a9%9f%e5%88%b6-HZuEOSEVfO\">Spring\u6846\u67b6\u7684\u7570\u5e38\u8655\u7406\u6a5f\u5236<\/h2>\n<p>Spring\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u8655\u7406\u7570\u5e38\uff0c\u7279\u5225\u662f\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8exceptionhandler%e8%a8%bb%e8%a7%a3-HZuEOSEVfO\">1. \u4f7f\u7528@ExceptionHandler\u8a3b\u89e3<\/h3>\n<p>\u5728Spring MVC\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528@ExceptionHandler\u8a3b\u89e3\u4f86\u6514\u622a\u7279\u5b9a\u7684\u7570\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\n@Controller\npublic class MyController {\n\n    @ExceptionHandler(DataAccessException.class)\n    public ResponseEntity handleDatabaseException(DataAccessException ex) {\n        \/\/ \u65e5\u8a8c\u8a18\u9304\u7570\u5e38\n        logger.error(\"\u6578\u64da\u5eab\u7570\u5e38\u767c\u751f: \", ex);\n        return new ResponseEntity(\"\u6578\u64da\u5eab\u64cd\u4f5c\u5931\u6557\", HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u7576Controller\u4e2d\u767c\u751fDataAccessException\u6642\uff0c\u5c07\u6703\u88ab\u9019\u500b\u65b9\u6cd5\u6514\u622a\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u53cb\u597d\u7684\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8aop%ef%bc%88%e9%9d%a2%e5%90%91%e5%88%87%e9%9d%a2%e7%b7%a8%e7%a8%8b%ef%bc%89-HZuEOSEVfO\">2. \u4f7f\u7528AOP\uff08\u9762\u5411\u5207\u9762\u7de8\u7a0b\uff09<\/h3>\n<p>Spring\u7684AOP\u529f\u80fd\u53ef\u4ee5\u7528\u4f86\u6514\u622a\u65b9\u6cd5\u8abf\u7528\uff0c\u9019\u5c0d\u65bc\u7d71\u4e00\u8655\u7406\u7570\u5e38\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684AOP\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n@Aspect\n@Component\npublic class DatabaseExceptionAspect {\n\n    @Around(\"execution(* com.example.service.*.*(..))\")\n    public Object handleDatabaseExceptions(ProceedingJoinPoint joinPoint) throws Throwable {\n        try {\n            return joinPoint.proceed();\n        } catch (DataAccessException ex) {\n            \/\/ \u65e5\u8a8c\u8a18\u9304\u7570\u5e38\n            logger.error(\"\u6578\u64da\u5eab\u7570\u5e38\u767c\u751f: \", ex);\n            throw new CustomDatabaseException(\"\u6578\u64da\u5eab\u64cd\u4f5c\u5931\u6557\", ex);\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6240\u6709\u4f4d\u65bccom.example.service\u5305\u4e2d\u7684\u65b9\u6cd5\u90fd\u6703\u88ab\u6514\u622a\uff0c\u4e26\u5728\u767c\u751fDataAccessException\u6642\u9032\u884c\u8655\u7406\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8transactional%e8%a8%bb%e8%a7%a3-HZuEOSEVfO\">3. \u4f7f\u7528@Transactional\u8a3b\u89e3<\/h3>\n<p>Spring\u7684@Transactional\u8a3b\u89e3\u53ef\u4ee5\u5e6b\u52a9\u7ba1\u7406\u4e8b\u52d9\uff0c\u4e26\u5728\u7570\u5e38\u767c\u751f\u6642\u81ea\u52d5\u56de\u6efe\u4e8b\u52d9\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\n@Service\npublic class UserService {\n\n    @Transactional\n    public void createUser(User user) {\n        \/\/ \u6578\u64da\u5eab\u64cd\u4f5c\n        userRepository.save(user);\n    }\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u5728createUser\u65b9\u6cd5\u4e2d\u767c\u751f\u7570\u5e38\uff0cSpring\u6703\u81ea\u52d5\u56de\u6efe\u4e8b\u52d9\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u4e0d\u6703\u8655\u65bc\u4e0d\u4e00\u81f4\u7684\u72c0\u614b\u3002<\/p>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-HZuEOSEVfO\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li>\u59cb\u7d42\u8a18\u9304\u7570\u5e38\u4fe1\u606f\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u6392\u67e5\u554f\u984c\u3002<\/li>\n<li>\u5c0d\u65bc\u7528\u6236\u53ef\u898b\u7684\u932f\u8aa4\uff0c\u63d0\u4f9b\u53cb\u597d\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u986f\u793a\u5806\u68e7\u8ddf\u8e64\u3002<\/li>\n<li>\u4f7f\u7528\u5168\u5c40\u7570\u5e38\u8655\u7406\u5668\u4f86\u7d71\u4e00\u8655\u7406\u7570\u5e38\uff0c\u6e1b\u5c11\u91cd\u8907\u4ee3\u78bc\u3002<\/li>\n<li>\u5b9a\u671f\u6e2c\u8a66\u7570\u5e38\u8655\u7406\u908f\u8f2f\uff0c\u78ba\u4fdd\u5176\u5728\u5be6\u969b\u904b\u884c\u4e2d\u6709\u6548\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HZuEOSEVfO\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528Spring\u6846\u67b6\u7684\u7570\u5e38\u8655\u7406\u6a5f\u5236\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u6514\u622a\u6578\u64da\u5eab\u7570\u5e38\uff0c\u5f9e\u800c\u63d0\u5347\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002\u7121\u8ad6\u662f\u4f7f\u7528@ExceptionHandler\u3001AOP\u9084\u662f@Transactional\u8a3b\u89e3\uff0c\u9019\u4e9b\u6280\u8853\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u7684\u7570\u5e38\u60c5\u6cc1\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\uff0c\u9019\u4e9b\u63aa\u65bd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u65b9\u6848\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728Spring\u6846\u67b6\u4e2d\u6514\u622a\u6578\u64da\u5eab\u7570\u5e38\uff0c\u63d0\u5347\u61c9\u7528\u7a69\u5b9a\u6027\uff0c\u78ba\u4fdd\u7cfb\u7d71\u904b\u884c\u66f4\u53ef\u9760\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-135111","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=135111"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135111\/revisions"}],"predecessor-version":[{"id":135112,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135111\/revisions\/135112"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=135111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=135111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=135111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}