{"id":11554,"date":"2023-11-28T22:24:17","date_gmt":"2023-11-28T14:24:17","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11554"},"modified":"2023-12-11T11:16:07","modified_gmt":"2023-12-11T03:16:07","slug":"spring-kuang-jia-ke-yi-zhu-runull-huo-kong-zi-fu-c","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-kuang-jia-ke-yi-zhu-runull-huo-kong-zi-fu-c\/","title":{"rendered":"Spring\u6846\u67b6\u53ef\u4ee5\u6ce8\u5165null\u6216\u7a7a\u5b57\u7b26\u4e32\u5417\uff1f"},"content":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u53ef\u4ee5\u6ce8\u5165<code>null<\/code>\u6216\u7a7a\u5b57\u7b26\u4e32\uff08\u7a7a\u4e32\uff09\u4f5c\u4e3aBean\u7684\u5c5e\u6027\u503c\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5982\u4f55\u8fdb\u884c\u914d\u7f6e\u548c\u6ce8\u5165\u3002<\/p>\n<ol>\n<li>\n<strong>\u6ce8\u5165<code>null<\/code>\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679cBean\u7684\u5c5e\u6027\u662f\u4e00\u4e2a\u5bf9\u8c61\u7c7b\u578b\uff08\u4f8b\u5982<code>String<\/code>\u3001\u81ea\u5b9a\u4e49\u7c7b\u7b49\uff09\uff0c\u800c\u4e14\u6ca1\u6709\u663e\u5f0f\u5730\u6307\u5b9a\u503c\uff0cSpring\u5bb9\u5668\u4f1a\u5c06\u8be5\u5c5e\u6027\u6ce8\u5165\u4e3a<code>null<\/code>\u3002\u8fd9\u662f\u9ed8\u8ba4\u884c\u4e3a\uff0c\u65e0\u9700\u989d\u5916\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\npublic class MyComponent {\n    private String stringValue;  \/\/ \u9ed8\u8ba4\u4e3anull\n    \/\/ getter and setter\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6ce8\u5165\u7a7a\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u5728XML\u914d\u7f6e\u6216\u6ce8\u89e3\u4e2d\u663e\u5f0f\u5730\u6307\u5b9a\u4e86\u7a7a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5c5e\u6027\u7684\u503c\uff0cSpring\u5bb9\u5668\u4f1a\u5c06\u8be5\u7a7a\u5b57\u7b26\u4e32\u6ce8\u5165\u5230\u5bf9\u5e94\u7684\u5c5e\u6027\u4e2d\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528XML\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot;&gt;\n    &lt;property name=&quot;stringValue&quot; value=&quot;&quot; \/&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<p>\u4f7f\u7528\u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\npublic class MyComponent {\n    @Value(&quot;&quot;)\n    private String stringValue;\n    \/\/ getter and setter\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982<code>int<\/code>\u3001<code>boolean<\/code>\u7b49\uff09\uff0cSpring\u6846\u67b6\u4e0d\u652f\u6301\u5c06\u7a7a\u5b57\u7b26\u4e32\u6ce8\u5165\u4e3a\u9ed8\u8ba4\u503c\uff0c\u56e0\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0d\u5141\u8bb8\u4e3a<code>null<\/code>\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u8981\u6307\u5b9a\u7a7a\u5b57\u7b26\u4e32\u7684\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5c5e\u6027\u5e76\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u8f6c\u6362\u6216\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\npublic class MyComponent {\n    private int intValue;\n\n    @Autowired\n    public void setStringValue(@Value(&quot;${my.property:}&quot;) String stringValue) {\n        this.intValue = stringValue.isEmpty() ? 0 : Integer.parseInt(stringValue);\n    }\n    \/\/ getter and setter for intValue\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>intValue<\/code>\u5c5e\u6027\u4f1a\u5728\u6ce8\u5165\u65f6\u6839\u636e<code>stringValue<\/code>\u7684\u503c\u8fdb\u884c\u5904\u7406\uff0c\u5982\u679c<code>stringValue<\/code>\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219<code>intValue<\/code>\u8bbe\u4e3a0\u3002\u8fd9\u6837\u53ef\u4ee5\u8fbe\u5230\u5c06\u7a7a\u5b57\u7b26\u4e32\u6ce8\u5165\u4e3a\u9ed8\u8ba4\u503c\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u53ef\u4ee5\u6ce8\u5165null\u6216\u7a7a\u5b57\u7b26\u4e32\uff08\u7a7a\u4e32\uff09\u4f5c\u4e3aBean\u7684\u5c5e\u6027\u503c\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5982\u4f55\u8fdb\u884c\u914d\u7f6e\u548c\u6ce8\u5165\u3002  [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11554","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11554"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11554\/revisions"}],"predecessor-version":[{"id":17072,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11554\/revisions\/17072"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11554"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}