{"id":10021046,"date":"2025-10-23T20:25:27","date_gmt":"2025-10-23T12:25:27","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021046"},"modified":"2025-10-23T20:25:27","modified_gmt":"2025-10-23T12:25:27","slug":"autowired-%e7%9a%84bug%e8%ae%a9%e6%88%91%e4%bb%ac%e7%99%bd%e5%bf%99%e4%b8%89%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021046","title":{"rendered":"@Autowired \u7684Bug\u8ba9\u6211\u4eec\u767d\u5fd9\u4e09\u5929"},"content":{"rendered":"<p>\u51cc\u6668\u4e24\u70b9\uff0c\u652f\u4ed8\u670d\u52a1\u7684\u544a\u8b66\u50cf\u96ea\u5d29\u4e00\u6837\u7838\u6765\uff0c\u4f60\u5728\u63a7\u5236\u53f0\u548c\u6808\u8ddf\u8e2a\u95f4\u75af\u72c2\u7a7f\u68ad\uff0c\u5374\u59cb\u7ec8\u60f3\u4e0d\u660e\u767d\uff1aSpring \u7684\u4f9d\u8d56\u6ce8\u5165\uff0c\u600e\u4e48\u4f1a\u5728\u751f\u4ea7\u91cc\u7a81\u7136\u201c\u5931\u624b\u201d\uff1f\u6211\u6700\u8fd1\u8bfb\u5230\u4e00\u7bc7\u4e8b\u6545\u590d\u76d8\uff0c\u8bb2\u7684\u662f\u4e24\u4e2a\u770b\u4f3c\u65e0\u5bb3\u7684\u6539\u52a8\u5982\u4f55\u5728\u751f\u4ea7\u73af\u5883\u8054\u624b\u628a\u7cfb\u7edf\u51fb\u7a7f\uff0c\u5206\u6790\u6df1\u5165\u3001\u542f\u53d1\u5f88\u5927\u3002\u4e8e\u662f\u6211\u628a\u5b83\u5b8c\u6574\u7ffb\u8bd1\u51fa\u6765\uff0c\u5206\u4eab\u7ed9\u5927\u5bb6\uff0c\u5e0c\u671b\u80fd\u5e2e\u4f60\u5c11\u8d70\u5f2f\u8def\u3002<\/p>\n<blockquote><p>\u4ee5\u4e0b\u5185\u5bb9\u7ffb\u8bd1\u81ea\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=zAle3Iy6fQSWFZ8xNFgIKA%3D%3D.wvMgfkgiFLpTBO9rVzPPdXQa%2Fm3BG9saewl7PUt%2BO77wq8hNlT8AIFKxFxyF3cZ%2FAFX8gead3FJhUuhId7JZSbziWIEKxk95qaCfHC0poVK2JnJAmo6qnI4dBi7zIy0C\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/medium.com\/javarevisited\/the-autowired-bug-that-cost-&#8230;<\/a><\/p><\/blockquote>\n<p>\u4e24\u4e2a\u201c\u770b\u4f3c\u65e0\u5bb3\u201d\u7684 PR \u5982\u4f55\u5728\u51cc\u6668 2 \u70b9\u8054\u624b\u51fb\u788e\u751f\u4ea7\u73af\u5883\u7684\u4f9d\u8d56\u6ce8\u5165\u3002<\/p>\n<p>\u6211\u4eec\u7684\u9996\u5e2d\u67b6\u6784\u5e08\u5728\u4e00\u4e2a\u6784\u9020\u5668\u4e0a\u52a0\u4e86 <code>@Autowired<\/code>\uff0c\u5e94\u7528\u80fd\u7f16\u8bd1\u3002\u6d4b\u8bd5\u901a\u8fc7\u3002\u4ee3\u7801\u8bc4\u5ba1\u4e5f\u8fc7\u4e86\u3002<\/p>\n<p>\u7136\u540e\u51cc\u6668\u4e24\u70b9\uff0c\u751f\u4ea7\u70b8\u4e86\uff0c<code>NullPointerException<\/code> \u5230\u5904\u90fd\u662f\u3002<\/p>\n<h2>\u6545\u4e8b\u4ece\u4e00\u6b21\u201c\u7b80\u5355\u201d\u7684\u91cd\u6784\u5f00\u59cb<\/h2>\n<p>\u5982\u679c\u4f60\u66fe\u5728\u5468\u4e94\u5408\u5e76\u8fc7\u4e00\u4e2a\u201c\u5f88\u5b89\u5168\u201d\u7684\u91cd\u6784\uff0c\u4f60\u5927\u6982\u77e5\u9053\u6545\u4e8b\u600e\u4e48\u53d1\u5c55\u3002<\/p>\n<p>\u6211\u4eec\u5728\u652f\u4ed8\u670d\u52a1\u91cc\u6e05\u7406\u6280\u672f\u503a\u3002\u6ca1\u5565\u82b1\u54e8\u7684\u2014\u2014\u628a\u4e00\u4e2a\u5de8\u578b\u7c7b\u62c6\u6210\u66f4\u5c0f\u3001\u66f4\u53ef\u6d4b\u7684\u7ec4\u4ef6\u800c\u5df2\u3002<\/p>\n<pre><code class=\"java\">@Service\npublic class PaymentProcessor {\n    \n    @Autowired\n    private PaymentGateway gateway;\n    \n    @Autowired\n    private FraudDetector fraudDetector;\n    \n    @Autowired\n    private NotificationService notificationService;\n    \n    \/\/ 847 \u884c\u4e1a\u52a1\u903b\u8f91...\n}<\/code><\/pre>\n<p>\u6211\u4eec\u7684\u67b6\u6784\u5e08\uff0c\u5c31\u53eb\u4ed6 Dave\uff0c\u51b3\u5b9a\u6539\u7528\u6784\u9020\u5668\u6ce8\u5165\u3002\u201c\u6700\u4f73\u5b9e\u8df5\u201d\uff0c\u4ed6\u8bf4\u3002\u5e72\u51c0\u3001\u4e0d\u53ef\u53d8\u3001\u6613\u6d4b\u8bd5\u3002<\/p>\n<p>\u542c\u8d77\u6765\u5f88\u5408\u7406\uff0c\u5bf9\u5427\uff1f<\/p>\n<pre><code class=\"java\">@Service\npublic class PaymentProcessor {\n    \n    private final PaymentGateway gateway;\n    private final FraudDetector fraudDetector;\n    private final NotificationService notificationService;\n    \n    @Autowired\n    public PaymentProcessor(\n        PaymentGateway gateway,\n        FraudDetector fraudDetector,\n        NotificationService notificationService\n    ) {\n        this.gateway = gateway;\n        this.fraudDetector = fraudDetector;\n        this.notificationService = notificationService;\n    }\n    \n    \/\/ \u4e1a\u52a1\u903b\u8f91...\n}<\/code><\/pre>\n<p>\u5b8c\u7f8e\u3002<code>final<\/code> \u5b57\u6bb5\u3001\u6784\u9020\u5668\u6ce8\u5165\uff0c\u8ddf\u6bcf\u7bc7 Spring Boot \u6559\u7a0b\u6559\u7684\u4e00\u6837\u3002<\/p>\n<p>\u5468\u56db\u53d1\u7248\u3002\u5468\u4e94\u98ce\u5e73\u6d6a\u9759\u3002\u5468\u672b\u4e5f\u5f88\u5b89\u7a33\u3002<\/p>\n<p>\u5468\u4e00\u6e05\u6668\uff0c\u5730\u72f1\u4e4b\u95e8\u6253\u5f00\u3002<\/p>\n<h2>\u51cc\u6668\u4e24\u70b9\uff1a\u4e00\u5207\u5f00\u59cb\u5d29\u574f<\/h2>\n<p>\u6211\u4eec Slack \u7684 #incidents \u9891\u9053\u50cf\u5723\u8bde\u6811\u4e00\u6837\u4eae\u4e86\u8d77\u6765\u3002<\/p>\n<pre><code class=\"java\">PagerDuty: \ud83d\udea8 CRITICAL: Payment Service - Error rate 34%\nDataDog: Payment processing failures spiking\nAWS CloudWatch: 500 errors on \/api\/payments\/process<\/code><\/pre>\n<p>\u6211\u662f\u5f53\u5468\u503c\u73ed\u5de5\u7a0b\u5e08\u3002\u771f\u201c\u5e78\u8fd0\u201d\u3002<\/p>\n<p>\u65e5\u5fd7\u50cf\u5669\u68a6\uff1a<\/p>\n<pre><code class=\"java\">java.lang.NullPointerException: Cannot invoke &quot;FraudDetector.check()&quot; because &quot;this.fraudDetector&quot; is null\n    at PaymentProcessor.processPayment(PaymentProcessor.java:67)\n    at PaymentController.createPayment(PaymentController.java:45)<\/code><\/pre>\n<p>\u7b49\u7b49\uff0c\u5565\uff1f<\/p>\n<p><code>fraudDetector<\/code> \u662f null\uff1f\u53ef\u5b83\u662f <code>@Autowired<\/code> \u7684\u4f9d\u8d56\u554a\u3002Spring \u4e0d\u5e94\u8be5\u7ed9\u5b83\u6ce8\u5165\u5417\uff1f\u8fd9\u4e0d\u5c31\u662f Spring \u7684\u5de5\u4f5c\u561b\u3002<\/p>\n<p>\u6211\u68c0\u67e5\u4e86 Bean \u914d\u7f6e\u3002<code>FraudDetector<\/code> \u7684 Bean \u5b58\u5728\u3001\u5df2\u6ce8\u518c\uff0c\u5176\u4ed6\u670d\u52a1\u7528\u5b83\u4e5f\u90fd\u6ca1\u95ee\u9898\u3002<\/p>\n<p>\u6211\u91cd\u542f\u4e86\u670d\u52a1\u3002\u8fd8\u662f\u4e00\u6837\u7684\u9519\u8bef\u3002<\/p>\n<p>\u6211\u770b\u4e86\u4e0b\u6784\u9020\u5668\u3002<code>@Autowired<\/code> \u6ce8\u89e3\u4e5f\u5728\u3002<\/p>\n<p>\u8fd9\u5230\u5e95\u600e\u4e48\u56de\u4e8b\uff1f<\/p>\n<h2>\u4e8b\u60c5\u5e76\u4e0d\u968f\u673a<\/h2>\n<p>\u7136\u540e\u4e8b\u60c5\u5f00\u59cb\u53d8\u5f97\u8be1\u5f02\u3002<\/p>\n<p>\u6709\u4e9b\u652f\u4ed8\u6210\u529f\u3002\u5927\u591a\u6570\u5931\u8d25\u3002<\/p>\n<p>\u4f46\u5e76\u975e\u968f\u673a\u3002\u5b83\u6709\u89c4\u5f8b\uff1a<\/p>\n<ul>\n<li>\u5c0f\u989d\u652f\u4ed8\uff08&lt;$100\uff09\uff1a\u6210\u529f<\/li>\n<li>\u5927\u989d\u652f\u4ed8\uff08&gt;$100\uff09\uff1a\u629b <code>NullPointerException<\/code><\/li>\n<\/ul>\n<p>\u8fd9\u5b8c\u5168\u8bf4\u4e0d\u901a\u3002\u652f\u4ed8\u91d1\u989d\u4e0d\u5e94\u8be5\u5f71\u54cd\u4f9d\u8d56\u6ce8\u5165\u3002\u8fd9\u4e0d\u662f Spring \u7684\u5de5\u4f5c\u65b9\u5f0f\u3002<\/p>\n<p>\u6211\u5230\u5904\u52a0\u4e86\u8c03\u8bd5\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"java\">@Autowired\npublic PaymentProcessor(\n    PaymentGateway gateway,\n    FraudDetector fraudDetector,\n    NotificationService notificationService\n) {\n    System.out.println(&quot;Constructor called!&quot;);\n    System.out.println(&quot;Gateway: &quot; + gateway);\n    System.out.println(&quot;FraudDetector: &quot; + fraudDetector);\n    System.out.println(&quot;NotificationService: &quot; + notificationService);\n    \n    this.gateway = gateway;\n    this.fraudDetector = fraudDetector;\n    this.notificationService = notificationService;\n}<\/code><\/pre>\n<p>\u65e5\u5fd7\u663e\u793a\u6784\u9020\u5668\u5728\u542f\u52a8\u65f6\u53ea\u88ab\u8c03\u7528\u4e86\u4e00\u6b21\u3002\u6240\u6709\u4f9d\u8d56\u90fd\u6ce8\u5165\u6b63\u786e\u3002<\/p>\n<p>\u90a3\u8fd0\u884c\u671f\u4e3a\u4ec0\u4e48 <code>fraudDetector<\/code> \u4f1a\u662f null\uff1f<\/p>\n<h2>\u5468\u4e8c\u65e9\u6668\uff1aGit Blame \u63ed\u6653\u8c1c\u5e95<\/h2>\n<p>\u5468\u4e8c\u65e9\u4e0a\uff0cDave \u6765\u4e86\u3002\u6211\u7ed9\u4ed6\u770b\u65e5\u5fd7\u3002\u4ed6\u5f88\u56f0\u60d1\u3002<\/p>\n<p>\u201c\u4e0d\u53ef\u80fd\uff0c\u201d\u4ed6\u8bf4\uff0c\u201c\u6784\u9020\u5668\u6ce8\u5165\u53ef\u4ee5\u4fdd\u8bc1\u4e0d\u53ef\u53d8\u3002\u201d<\/p>\n<p>\u6211\u6253\u5f00 git diff\u3002\u4ed6\u7684\u91cd\u6784 PR\u2014\u2014\u6539\u4e86 47 \u4e2a\u6587\u4ef6\u3002<\/p>\n<p>\u7136\u540e\u6211\u770b\u5230\u4e86\u2014\u2014\u57cb\u5728 diff \u4e2d\u95f4\u7684\u90a3\u4e00\u884c\u3002<\/p>\n<pre><code class=\"java\">@Service\n@Scope(&quot;prototype&quot;)  \/\/ \u5c31\u662f\u8fd9\u4e00\u884c\npublic class PaymentProcessor {\n    \/\/ ...\n}<\/code><\/pre>\n<p>\u6709\u4eba\u7ed9\u8fd9\u4e2a\u7c7b\u52a0\u4e86 <code>@Scope(&quot;prototype&quot;)<\/code>\u3002<\/p>\n<p>\u4e0d\u662f Dave\u3002\u662f\u4e0a\u5468\u53e6\u4e00\u4e2a PR\u3002\u4e00\u4e2a\u201c\u6027\u80fd\u4f18\u5316\u201d\u2014\u2014\u4e00\u4f4d\u521d\u7ea7\u540c\u4e8b\u4ee5\u4e3a\u6bcf\u6b21\u521b\u5efa\u65b0\u5b9e\u4f8b\u53ef\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u4e24\u4e2a PR \u5206\u522b\u5408\u5e76\u3002\u6ca1\u6709\u51b2\u7a81\u3002\u8bc4\u5ba1\u90fd\u901a\u8fc7\u3002\u5404\u81ea\u770b\u4e5f\u90fd\u5408\u7406\u3002<\/p>\n<p>\u4f46\u653e\u5728\u4e00\u8d77\uff1f\u707e\u96be\u3002<\/p>\n<h2>\u6280\u672f\u62c6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u70b8<\/h2>\n<p>\u6765\u89e3\u91ca\u4e00\u4e0b <code>@Scope(&quot;prototype&quot;)<\/code> \u5230\u5e95\u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u8303\u56f4\uff08Scope\uff09<\/th>\n<th>\u5b9e\u4f8b\u751f\u547d\u5468\u671f<\/th>\n<th>\u5e38\u89c1\u9677\u9631<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Singleton<\/td>\n<td>\u542f\u52a8\u65f6\u521b\u5efa\u4e00\u6b21<\/td>\n<td>\u4e0e\u6784\u9020\u5668\u4f9d\u8d56\u6ce8\u5165\u7ec4\u5408\u5b89\u5168<\/td>\n<\/tr>\n<tr>\n<td>Prototype<\/td>\n<td>\u6bcf\u6b21\u8bf7\u6c42\u521b\u5efa\u4e00\u4e2a<\/td>\n<td>\u4e0e\u5b57\u6bb5\u8bbf\u95ee\u7ec4\u5408\u4f1a\u51fa\u73b0\u95ee\u9898<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u666e\u901a Spring Bean\uff08\u5355\u4f8b\uff09\uff1a<\/p>\n<ol>\n<li>Spring \u5728\u542f\u52a8\u65f6\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b<\/li>\n<li>\u6784\u9020\u5668\u53ea\u6267\u884c\u4e00\u6b21<\/li>\n<li>\u4f9d\u8d56\u53ea\u6ce8\u5165\u4e00\u6b21<\/li>\n<li>\u8be5\u5b9e\u4f8b\u670d\u52a1\u6240\u6709\u8bf7\u6c42<\/li>\n<\/ol>\n<p>\u539f\u578b\u8303\u56f4\uff08prototype\uff09\u7684 Bean\uff1a<\/p>\n<ol>\n<li>\u6bcf\u6b21\u83b7\u53d6\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u5b9e\u4f8b<\/li>\n<li>\u6784\u9020\u5668\u6bcf\u6b21\u90fd\u4f1a\u6267\u884c<\/li>\n<li>\u4f9d\u8d56\u4e5f\u5e94\u8be5\u6bcf\u6b21\u90fd\u88ab\u6ce8\u5165<\/li>\n<\/ol>\n<p>\u4f46\u5173\u952e\u70b9\u5728\u8fd9\u91cc\uff1a<\/p>\n<p>\u5f53\u5176\u4ed6\u670d\u52a1\uff08\u6bd4\u5982\u6211\u4eec\u7684 <code>PaymentController<\/code>\uff09\u901a\u8fc7 <code>@Autowired<\/code> \u6ce8\u5165 <code>PaymentProcessor<\/code> \u65f6\uff0cSpring \u6ce8\u5165\u7684\u4e0d\u662f\u6bcf\u6b21\u90fd\u521b\u5efa\u7684\u65b0\u5b9e\u4f8b\uff0c\u800c\u662f\u4e00\u4e2a<strong>\u4ee3\u7406\uff08proxy\uff09<\/strong>\u3002<\/p>\n<p>\u8fd9\u4e2a\u4ee3\u7406\u4f1a\u5728\u201c\u65b9\u6cd5\u8c03\u7528\u201d\u65f6\u521b\u5efa\u65b0\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u5728\u201c\u5b57\u6bb5\u8bbf\u95ee\u201d\u65f6\u521b\u5efa\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">@RestController\npublic class PaymentController {\n    \n    @Autowired\n    private PaymentProcessor processor;\n    \n    public void handlePayment(Payment payment) {\n        processor.processPayment(payment);  \/\/ \u5728\u8fd9\u91cc\u89e6\u53d1\u521b\u5efa\u65b0\u5b9e\u4f8b\n    }\n}<\/code><\/pre>\n<p>\u5de5\u4f5c\u6b63\u5e38\u3002\u65b9\u6cd5\u8c03\u7528\u4f1a\u89e6\u53d1\u5b9e\u4f8b\u521b\u5efa\u3002<\/p>\n<p>\u4f46\u6211\u4eec\u8fd8\u6709\u7b2c\u4e8c\u6761\u4ee3\u7801\u8def\u5f84\uff1a<\/p>\n<pre><code class=\"java\">@Component\npublic class ScheduledPaymentJob {\n    \n    @Autowired\n    private PaymentProcessor processor;\n    \n    @Scheduled(fixedRate = 60000)\n    public void processScheduledPayments() {\n        List&lt;Payment&gt; pending = getPendingPayments();\n        \n        for (Payment p : pending) {\n            \/\/ \u76f4\u63a5\u5b57\u6bb5\u8bbf\u95ee\uff01\n            if (processor.fraudDetector.isHighRisk(p)) {\n                processor.processManually(p);\n            } else {\n                processor.processPayment(p);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u770b\u51fa\u95ee\u9898\u4e86\u5417\uff1f<\/p>\n<p><code>processor.fraudDetector<\/code> \u662f\u76f4\u63a5\u5b57\u6bb5\u8bbf\u95ee\uff0c\u4e0d\u662f\u65b9\u6cd5\u8c03\u7528\u3002\u4ee3\u7406\u4e0d\u4f1a\u62e6\u622a\u5b83\u3002\u4e0d\u4f1a\u521b\u5efa\u65b0\u5b9e\u4f8b\u3002\u5b57\u6bb5\u81ea\u7136\u5c31\u662f null\u3002<\/p>\n<p>\u5c0f\u989d\u652f\u4ed8\u8d70\u63a7\u5236\u5668\u8def\u5f84\uff08\u65b9\u6cd5\u8c03\u7528 = \u6b63\u5e38\uff09\u3002<\/p>\n<p>\u5927\u989d\u652f\u4ed8\u89e6\u53d1\u4e86\u5b9a\u65f6\u4efb\u52a1\u91cc\u7684\u4eba\u5de5\u590d\u6838\u8def\u5f84\uff08\u5b57\u6bb5\u8bbf\u95ee = <code>NullPointerException<\/code>\uff09\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5b83\u4e0d\u662f\u968f\u673a\u7684\u3002\u5b83\u975e\u5e38\u201c\u5408\u60c5\u5408\u7406\u201d\uff0c\u53ea\u4e0d\u8fc7\u5f88\u9690\u853d\u3002<\/p>\n<h2>\u5468\u4e09\uff1a\u5e76\u4e0d\u7b80\u5355\u7684\u201c\u4fee\u590d\u201d<\/h2>\n<p>\u7b2c\u4e00\u53cd\u5e94\uff1a\u79fb\u9664 <code>@Scope(&quot;prototype&quot;)<\/code>\uff0c\u6539\u56de\u5355\u4f8b\u3002<\/p>\n<p>\u95ee\u9898\uff1a\u521d\u7ea7\u540c\u4e8b\u52a0\u5b83\u662f\u6709\u7406\u7531\u7684\u3002\u6211\u4eec\u4e4b\u524d\u770b\u5230\u8fc7\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u3002\u79fb\u9664\u5b83\u53ef\u80fd\u628a\u65e7\u95ee\u9898\u5e26\u56de\u6765\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u60f3\u6cd5\uff1a\u4fdd\u7559 prototype\uff0c\u4f46\u4fee\u4ee3\u7406\u884c\u4e3a\u3002<\/p>\n<p>\u95ee\u9898\uff1a\u4f60\u65e0\u6cd5\u7528 prototype Bean \u4fee\u590d\u4ee3\u7406\u7684\u62e6\u622a\u884c\u4e3a\u3002\u8fd9\u662f Spring \u7684\u57fa\u672c\u9650\u5236\u3002<\/p>\n<p>\u7b2c\u4e09\u4e2a\u60f3\u6cd5\uff1a\u539f\u578b Bean \u4e0d\u7528 <code>@Autowired<\/code>\uff0c\u6539\u6210\u624b\u52a8\u4ece <code>ApplicationContext.getBean()<\/code> \u83b7\u53d6\u3002<\/p>\n<pre><code class=\"java\">@Component\npublic class ScheduledPaymentJob {\n    \n    @Autowired\n    private ApplicationContext context;\n    \n    @Scheduled(fixedRate = 60000)\n    public void processScheduledPayments() {\n        List&lt;Payment&gt; pending = getPendingPayments();\n        \n        for (Payment p : pending) {\n            \/\/ \u6bcf\u6b21\u83b7\u53d6\u4e00\u4e2a\u5168\u65b0\u5b9e\u4f8b\n            PaymentProcessor processor = context.getBean(PaymentProcessor.class);\n            \n            if (processor.getFraudDetector().isHighRisk(p)) {\n                processor.processManually(p);\n            } else {\n                processor.processPayment(p);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u95ee\u9898\uff1a\u8fd9\u592a\u4e11\u4e86\u3002\u624b\u52a8\u67e5\u627e Bean \u8fdd\u80cc\u4e86\u4f9d\u8d56\u6ce8\u5165\u7684\u521d\u8877\u3002<\/p>\n<p>\u7b2c\u56db\u4e2a\u60f3\u6cd5\uff08\u6700\u7ec8\u6709\u6548\u7684\u90a3\u4e2a\uff09\uff1a<\/p>\n<p>\u505c\u6b62\u4f7f\u7528 prototype \u8303\u56f4\u3002\u53bb\u4fee\u771f\u6b63\u7684\u5185\u5b58\u95ee\u9898\u3002<\/p>\n<p>\u7ed3\u679c\u53d1\u73b0\uff0c\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u53ea\u662f\u8bef\u89e3\u3002\u521d\u7ea7\u540c\u4e8b\u770b\u5230\u5806\u5185\u5b58\u4f7f\u7528\u4e0a\u5347\uff0c\u5c31\u4ee5\u4e3a\u662f\u6cc4\u6f0f\u3002\u5176\u5b9e\u4e0d\u662f\uff0c\u662f G1GC \u4e0b JVM \u7684\u6b63\u5e38\u884c\u4e3a\u3002<\/p>\n<p>\u6211\u4eec\u79fb\u9664\u4e86 <code>@Scope(&quot;prototype&quot;)<\/code>\u3002\u5185\u5b58\u4f7f\u7528\u4fdd\u6301\u7a33\u5b9a\u3002\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n<h2>\u771f\u6b63\u7684\u6559\u8bad<\/h2>\n<p>\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u6211\u4eec\u5728\u4e0d\u7406\u89e3\u7684\u60c5\u51b5\u4e0b\u4fe1\u4efb\u4e86\u6ce8\u89e3\u3002<code>@Scope(&quot;prototype&quot;)<\/code> \u770b\u8d77\u6765\u65e0\u5bb3\uff0c\u5176\u5b9e\u4f1a\u6539\u53d8 Spring \u7684\u6574\u4e2a\u5bf9\u8c61\u751f\u547d\u5468\u671f\u3002<\/li>\n<li>\u6211\u4eec\u628a PR \u5f53\u4f5c\u5b64\u7acb\u6539\u52a8\u6765\u8bc4\u5ba1\u3002Dave \u7684\u91cd\u6784\u770b\u8d77\u6765\u6ca1\u95ee\u9898\uff1bScope \u7684\u6539\u52a8\u4e5f\u770b\u8d77\u6765\u6ca1\u95ee\u9898\uff1b\u653e\u5230\u4e00\u8d77\u5c31\u662f\u707e\u96be\u3002<\/li>\n<li>\u6211\u4eec\u7684\u6d4b\u8bd5\u6ca1\u8986\u76d6\u5230\u6240\u6709\u8def\u5f84\u3002\u96c6\u6210\u6d4b\u8bd5\u53ea\u8dd1\u4e86\u63a7\u5236\u5668\u8def\u5f84\uff08\u5b83\u662f\u6b63\u5e38\u7684\uff09\uff0c\u6ca1\u4eba\u6d4b\u5b9a\u65f6\u4efb\u52a1\u8def\u5f84\u3002<\/li>\n<li>\u6211\u4eec\u4ee5\u4e3a\u5b57\u6bb5\u6ce8\u5165\u548c\u6784\u9020\u5668\u6ce8\u5165\u662f\u7b49\u4ef7\u7684\u3002\u4e0d\u662f\u3002\u6784\u9020\u5668\u6ce8\u5165 + prototype \u8303\u56f4 + \u5b57\u6bb5\u8bbf\u95ee = \u7a7a\u5f15\u7528\u3002<\/li>\n<\/ol>\n<h2>\u6211\u4eec\u4e4b\u540e\u7684\u6539\u8fdb<\/h2>\n<p>\u65b0\u7684\u56e2\u961f\u89c4\u5219\uff1a<\/p>\n<ul>\n<li>\u2705 \u672a\u7ecf\u660e\u786e\u5ba1\u6279\uff0c\u4e0d\u5f97\u4f7f\u7528 prototype \u8303\u56f4\u3002\u9700\u8981\u65f6\u5fc5\u987b\u5728 wiki \u91cc\u5199\u660e\u7406\u7531\u548c\u4f7f\u7528\u65b9\u5f0f\u3002<\/li>\n<li>\u2705 \u53ea\u7528\u6784\u9020\u5668\u6ce8\u5165\uff08\u4e00\u4e2a\u4f8b\u5916\uff09\u3002\u5b57\u6bb5\u6ce8\u5165\u88ab\u7981\u7528\u3002\u552f\u4e00\u7684\u4f8b\u5916\u662f\u73af\u72b6\u4f9d\u8d56\uff08\u7136\u540e\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u6784\u6389\u5b83\uff09\u3002<\/li>\n<li>\u2705 \u96c6\u6210\u6d4b\u8bd5\u5fc5\u987b\u8986\u76d6\u6240\u6709\u4ee3\u7801\u8def\u5f84\u3002\u4e0d\u53ea\u201c\u5feb\u4e50\u8def\u5f84\u201d\u3002\u8981\u6d4b\u5b9a\u65f6\u4efb\u52a1\u3001\u8fb9\u754c\u573a\u666f\u3002<\/li>\n<li>\u2705 \u4ee3\u7801\u8bc4\u5ba1\u8981\u68c0\u67e5\u4ea4\u4e92\u6548\u5e94\u3002\u5408\u5e76\u524d\u8981\u770b\u8fd1\u671f\u6709\u54ea\u4e9b PR \u52a8\u8fc7\u540c\u4e00\u670d\u52a1\u3002<\/li>\n<li>\u2705 \u5185\u5b58\u201c\u6cc4\u6f0f\u201d\u5fc5\u987b\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\u8bc1\u660e\u3002\u6ca1\u6709 YourKit \u6216 VisualVM \u7684\u8bc1\u636e\uff0c\u5c31\u4e0d\u80fd\u53eb\u201c\u6cc4\u6f0f\u201d\u3002<\/li>\n<\/ul>\n<h2>\u4e0d\u90a3\u4e48\u8212\u9002\u7684\u771f\u76f8<\/h2>\n<p>Spring \u8ba9\u4f9d\u8d56\u6ce8\u5165\u770b\u8d77\u6765\u5f88\u6709\u201c\u9b54\u6cd5\u201d\u300290% \u7684\u65f6\u5019\uff0c\u5b83\u786e\u5b9e\u5f88\u597d\u7528\u3002<\/p>\n<p>\u4f46\u201c\u9b54\u6cd5\u201d\u4e5f\u6709\u8fb9\u89d2\u3002\u539f\u578b Bean \u4e0e\u4ee3\u7406\u3001\u6784\u9020\u5668\u6ce8\u5165\u4e2d\u7684\u73af\u72b6\u4f9d\u8d56\u3001<code>@Transactional<\/code> \u7684\u5185\u90e8\u65b9\u6cd5\u8c03\u7528\u7b49\u7b49\u3002<\/p>\n<p>\u6587\u6863\u90fd\u5199\u4e86\u3002\u8fd9\u4e9b\u5751\u4e5f\u90fd\u88ab\u8bb0\u5f55\u4e86\u3002\u4f46\u6ca1\u4eba\u4f1a\u5728\u5199\u4e00\u4e2a <code>@Service<\/code> \u7c7b\u4e4b\u524d\uff0c\u628a 500 \u9875\u7684 Spring \u53c2\u8003\u624b\u518c\u901a\u8bfb\u4e00\u904d\u3002<\/p>\n<p>\u6211\u4eec\u90fd\u662f\u5728\u7ffb\u8f66\u4e2d\u5b66\u4e60\u3002\u8fd9\u6b21\u6211\u4eec\u5728\u51cc\u6668\u4e24\u70b9\u70b8\u4e86\u751f\u4ea7\uff0c\u82b1\u4e86\u4e09\u5929\u65f6\u95f4\u624d\u641e\u6e05\u695a\u4e00\u4e2a\u6ce8\u89e3\u80cc\u540e\u7684\u4ea4\u4e92\u6548\u5e94\u3002<\/p>\n<p>Dave \u7684\u91cd\u6784\u5e76\u6ca1\u6709\u9519\u3002prototype \u8303\u56f4\u4e5f\u4e0d\u662f\u7edd\u5bf9\u9519\u8bef\u3002\u9519\u7684\u662f\u5b83\u4eec\u7684\u7ec4\u5408\u2014\u2014\u53ea\u5728\u751f\u4ea7\u3001\u53ea\u5728\u67d0\u4e9b\u4ee3\u7801\u8def\u5f84\u3001\u53ea\u5728\u4e24\u4e2a PR \u90fd\u5408\u5e76\u4e4b\u540e\u624d\u663e\u73b0\u7684\u95ee\u9898\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u201c\u9b54\u6cd5\u201d\u6846\u67b6\u7684\u771f\u5b9e\u4ee3\u4ef7\uff1a\u987a\u7684\u65f6\u5019\u5f88\u5999\uff1b\u4e0d\u987a\u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u4e00\u4e2a Spring \u5185\u90e8\u673a\u5236\u535a\u58eb\u5b66\u4f4d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047341104\">https:\/\/segmentfault.com\/a\/1190000047341104<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51cc\u6668\u4e24\u70b9\uff0c\u652f\u4ed8\u670d\u52a1\u7684\u544a\u8b66\u50cf\u96ea\u5d29\u4e00\u6837\u7838\u6765\uff0c\u4f60\u5728\u63a7\u5236\u53f0\u548c\u6808\u8ddf\u8e2a\u95f4\u75af\u72c2\u7a7f\u68ad\uff0c\u5374\u59cb\u7ec8\u60f3\u4e0d\u660e\u767d\uff1aSpring \u7684\u4f9d\u8d56\u6ce8\u5165 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021046","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021046"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021046\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}