{"id":10275,"date":"2023-03-27T21:11:56","date_gmt":"2023-03-27T13:11:56","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10275"},"modified":"2023-03-28T09:17:03","modified_gmt":"2023-03-28T01:17:03","slug":"spring-%e6%b5%8b%e8%af%95%e7%b1%bb-dirtiescontext","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10275","title":{"rendered":"Spring \u6d4b\u8bd5\u7c7b @DirtiesContext"},"content":{"rendered":"\n<p><strong>\u80cc\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4ece\u6628\u5929\u5230\u4eca\u5929\uff0c\u8017\u4e86\u6574\u6574\u4e00\u5929\u7684Build Faild\u6700\u7ec8\u88ab\u6ce8\u89e3@DirtiesContext\u89e3\u51b3\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-337-1024x119.png\" alt=\"\" class=\"wp-image-10278\" width=\"484\" height=\"55\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337-1024x119.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337-300x35.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337-768x89.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337-230x27.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337-350x41.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337-480x56.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-337.png 1238w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/figure><\/div>\n\n\n\n<p>\u76f8\u540c\u7684\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5728\u672c\u5730\u8fd0\u884c\u826f\u597d\uff0c\u4f46\u5728Jenkins\u91cc\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u5206\u522b\u7f16\u8bd1\u5931\u8d25\uff0c\u800c\u4e14\u6bcf\u6b21\u5931\u8d25\u7684\u9519\u8bef\u8fd8\u4e0d\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u7ec8\u6781\u539f\u56e0\u662f\u8fd0\u884c\u6d4b\u8bd5\u7684application context\u7684\u7f13\u5b58\u4e2d\u6709\u810f\u6570\u636e\u3002<\/p>\n\n\n\n<p><code>@DirtiesContext<\/code>\u6ce8\u89e3\u7528\u5728\u6d4b\u8bd5\u7c7b\u4e0a\uff0c\u5f85\u6574\u4e2a\u6d4b\u8bd5\u7c7b\u7684\u6240\u6709\u6d4b\u8bd5\u6267\u884c\u7ed3\u675f\u540e\uff0c\u8be5\u6d4b\u8bd5\u7684<code>application context<\/code>\u4f1a\u88ab\u5173\u95ed\uff0c\u540c\u65f6\u7f13\u5b58\u4f1a\u6e05\u9664\u3002<code>@DirtiesContext<\/code>\u5206\u4e3a<code>method-level<\/code>\u548c<code>class-level<\/code><\/p>\n\n\n\n<p><strong>ChatGpt\u7684\u89e3\u91ca<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-338-1024x809.png\" alt=\"\" class=\"wp-image-10281\" width=\"420\" height=\"331\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-1024x809.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-300x237.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-768x607.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-830x656.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-230x182.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-350x277.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338-480x379.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-338.png 1534w\" sizes=\"(max-width: 420px) 100vw, 420px\" \/><\/figure><\/div>\n\n\n\n<p><strong>@DirtiesContext<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>@DirtiesContext\u662f\u4e00\u4e2aSpring\u6d4b\u8bd5\u6ce8\u91ca\u3002\u5b83\u6307\u793a\u5173\u8054\u7684\u6d4b\u8bd5\u6216\u7c7b\u4fee\u6539\u4e86ApplicationContext\u3002\u5b83\u544a\u8bc9\u6d4b\u8bd5\u6846\u67b6\u5173\u95ed\u5e76\u91cd\u65b0\u521b\u5efa\u4e0a\u4e0b\u6587\u4ee5\u7528\u4e8e\u4ee5\u540e\u7684\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u6ce8\u91ca\u6d4b\u8bd5\u65b9\u6cd5\u6216\u6574\u4e2a\u7c7b\u3002\u901a\u8fc7\u8bbe\u7f6eMethodMode\u6216ClassMode\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236Spring\u4f55\u65f6\u6807\u8bb0\u8981\u5173\u95ed\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u5c06@DirtiesContext\u653e\u5728\u7c7b\u4e0a\uff0c\u5219\u6ce8\u91ca\u5c06\u5e94\u7528\u4e8e\u5177\u6709\u7ed9\u5b9aClassMode\u7684\u7c7b\u4e2d\u7684\u6bcf\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-336-1024x251.png\" alt=\"\" class=\"wp-image-10276\" width=\"479\" height=\"116\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-336-1024x251.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-336-300x74.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-336-768x188.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-336-230x56.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-336-350x86.png 350w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5728\u4e0d\u6e05\u9664Spring\u4e0a\u4e0b\u6587\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class User {\n    String firstName;\n    String lastName;\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u8fd8\u6709\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684UserCache\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\npublic class UserCache {\n\n    @Getter\n    private Set&lt;String> userList = new HashSet&lt;>();\n\n    public boolean addUser(String user) {\n        return userList.add(user);\n    }\n\n    public void printUserList(String message) {\n        System.out.println(message +\":\" + userList);\n    }\n\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u96c6\u6210\u6d4b\u8bd5\u6765\u52a0\u8f7d\u548c\u6d4b\u8bd5\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@TestMethodOrder(OrderAnnotation.class)\n@ExtendWith(SpringExtension.class)\n@SpringBootTest(classes = SpringDataRestApplication.class)\nclass DirtiesContextIntegrationTest {\n\n    @Autowired\n    protected UserCache userCache;\n   \n    ...\n}<\/pre>\n\n\n\n<p>\u7b2c\u4e00\u79cd\u65b9\u6cd5addJaneDoeAndPrintCache\u5c06\u4e00\u4e2a\u6761\u76ee\u6dfb\u52a0\u5230\u7f13\u5b58\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\n@Order(1)\nvoid addJaneDoeAndPrintCache() {\n    userCache.addUser(\"Jane Doe\");\n    userCache.printUserList(\"addJaneDoeAndPrintCache\");\n}<\/pre>\n\n\n\n<p>\u5c06\u7528\u6237\u6dfb\u52a0\u5230\u7f13\u5b58\u540e\uff0c\u5b83\u5c06\u6253\u5370\u7f13\u5b58\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addJaneDoeAndPrintCache: [Jane Doe]<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0cprintCache\u518d\u6b21\u6253\u5370\u7528\u6237\u7f13\u5b58\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\n@Order(2)\nvoid printCache() {\n    userCache.printUserList(\"printCache\");\n}<\/pre>\n\n\n\n<p>\u5b83\u5305\u542b\u5728\u5148\u524d\u6d4b\u8bd5\u4e2d\u6dfb\u52a0\u7684\u540d\u79f0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">printCache: [Jane Doe]<\/pre>\n\n\n\n<p>\u5047\u8bbe\u4ee5\u540e\u7684\u6d4b\u8bd5\u4f9d\u8d56\u4e8e\u7a7a\u7f13\u5b58\u8fdb\u884c\u67d0\u4e9b\u58f0\u660e\u3002\u4ee5\u524d\u63d2\u5165\u7684\u540d\u79f0\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u826f\u884c\u4e3a\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528@DirtiesContext<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u9ed8\u8ba4\u65b9\u6cd5\u6a21\u5f0fAFTER_METHOD\u663e\u793a@DirtiesContext\u3002\u8fd9\u610f\u5473\u7740Spring\u5c06\u5728\u76f8\u5e94\u7684\u6d4b\u8bd5\u65b9\u6cd5\u5b8c\u6210\u540e\u5c06\u4e0a\u4e0b\u6587\u6807\u8bb0\u4e3a\u5173\u95ed\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u9694\u79bb\u6d4b\u8bd5\u7684\u66f4\u6539\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86@DirtiesContext\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n\n\n\n<p>addJohnDoeAndPrintCache\u6d4b\u8bd5\u65b9\u6cd5\u5c06\u7528\u6237\u6dfb\u52a0\u5230\u7f13\u5b58\u4e2d\u3002\u6211\u4eec\u8fd8\u6dfb\u52a0\u4e86@DirtiesContext\u6279\u6ce8\uff0c\u8be5\u6279\u6ce8\u8868\u793a\u4e0a\u4e0b\u6587\u5e94\u5728\u6d4b\u8bd5\u65b9\u6cd5\u7ed3\u675f\u65f6\u5173\u95ed\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)\n@Test\n@Order(3)\nvoid addJohnDoeAndPrintCache() {\n    userCache.addUser(\"John Doe\");\n    userCache.printUserList(\"addJohnDoeAndPrintCache\");\n}<\/pre>\n\n\n\n<p>\u73b0\u5728\u7684\u8f93\u51fa\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addJohnDoeAndPrintCache: [John Doe, Jane Doe]<\/pre>\n\n\n\n<p>\u6700\u540e\uff0cprintCacheA\u518d\u6b21\u6253\u5370\u7f13\u5b58\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\n@Order(4)\nvoid printCacheAgain() {\n    userCache.printUserList(\"printCacheAgain\");\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c\u5b8c\u6574\u7684\u6d4b\u8bd5\u7c7b\uff0c\u6211\u4eec\u770b\u5230\u5728addJohnDoeAndPrintCache\u548cprintCacheAgain\u4e4b\u95f4\u91cd\u65b0\u52a0\u8f7d\u4e86Spring\u4e0a\u4e0b\u6587\u3002\u56e0\u6b64\uff0c\u7f13\u5b58\u5c06\u91cd\u65b0\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u8f93\u51fa\u4e3a\u7a7a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">printCacheAgain: []<\/pre>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Class\u7ea7\u522b\uff1a\u6d4b\u8bd5\u7c7b\u7684ClassMode\u9009\u9879\u5b9a\u4e49\u4e86\u4f55\u65f6\u91cd\u7f6e\u4e0a\u4e0b\u6587<\/p>\n\n\n\n<ul><li>BEFORE_CLASS\uff1a\u5728\u5f53\u524d\u6d4b\u8bd5\u8bfe\u7a0b\u4e4b\u524d<\/li><li>BEFORE_EACH_TEST_METHOD\uff1a\u5728\u5f53\u524d\u6d4b\u8bd5\u7c7b\u4e2d\u7684\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u524d<\/li><li>AFTER_EACH_TEST_METHOD\uff1a\u5f53\u524d\u6d4b\u8bd5\u7c7b\u4e2d\u7684\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u540e<\/li><li>AFTER_CLASS\uff1a\u5728\u5f53\u524d\u6d4b\u8bd5\u8bfe\u7a0b\u4e4b\u540e<\/li><\/ul>\n\n\n\n<p>\u65b9\u6cd5\u7ea7\u522b\uff1a\u5355\u4e2a\u65b9\u6cd5\u7684MethodMode\u9009\u9879\u5b9a\u4e49\u4f55\u65f6\u91cd\u7f6e\u4e0a\u4e0b\u6587<\/p>\n\n\n\n<ul><li>BEFORE_METHOD\uff1a\u5f53\u524d\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u524d<\/li><li>AFTER_METHOD\uff1a\u5f53\u524d\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u540e<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u4ece\u6628\u5929\u5230\u4eca\u5929\uff0c\u8017\u4e86\u6574\u6574\u4e00\u5929\u7684Build Faild\u6700\u7ec8\u88ab\u6ce8\u89e3@DirtiesContext\u89e3\u51b3\u4e86\u3002 \u76f8\u540c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10275"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10275"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10275\/revisions"}],"predecessor-version":[{"id":10283,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10275\/revisions\/10283"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}