{"id":135,"date":"2026-06-16T14:37:00","date_gmt":"2026-06-16T06:37:00","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=135"},"modified":"2026-06-16T14:37:00","modified_gmt":"2026-06-16T06:37:00","slug":"fastapi-%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/16\/fastapi-%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5-2\/","title":{"rendered":"FastAPI \u4f9d\u8d56\u6ce8\u5165"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">FastAPI \u7684<strong>\u4f9d\u8d56\u6ce8\u5165\uff08Dependency Injection, DI\uff09<\/strong> \u662f\u5176\u6700\u5f3a\u5927\u3001\u6700\u6838\u5fc3\u7684\u7279\u6027\u4e4b\u4e00\u3002\u5b83\u5141\u8bb8\u4f60\u58f0\u660e\u4ee3\u7801\u8fd0\u884c\u6240\u9700\u7684\u201c\u4f9d\u8d56\u201d\uff08\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u5f53\u524d\u7528\u6237\u3001\u914d\u7f6e\u4fe1\u606f\u7b49\uff09\uff0cFastAPI \u4f1a\u81ea\u52a8\u4e3a\u4f60\u89e3\u6790\u3001\u6267\u884c\u5e76\u6ce8\u5165\u8fd9\u4e9b\u4f9d\u8d56\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u8bbe\u8ba1\u4e0d\u4ec5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u53ef\u590d\u7528\uff0c\u8fd8\u6781\u5927\u5730\u7b80\u5316\u4e86\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f FastAPI \u4f9d\u8d56\u6ce8\u5165\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5e38\u7528\u7528\u6cd5\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u57fa\u7840\u7528\u6cd5\uff1a\u4f7f\u7528 <code>Depends<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f9d\u8d56\u901a\u5e38\u662f\u4e00\u4e2a\u666e\u901a\u7684 Python \u51fd\u6570\uff08\u6216\u7c7b\uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>Depends<\/code> \u5c06\u5176\u6ce8\u5165\u5230\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\u4e2d\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Depends\n\napp = FastAPI()\n\n# 1. \u5b9a\u4e49\u4e00\u4e2a\u4f9d\u8d56\u51fd\u6570\ndef common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):\n    return {&quot;q&quot;: q, &quot;skip&quot;: skip, &quot;limit&quot;: limit}\n\n# 2. \u5728\u8def\u5f84\u64cd\u4f5c\u4e2d\u4f7f\u7528 Depends \u6ce8\u5165\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(commons: dict = Depends(common_parameters)):\n    return commons<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>FastAPI \u4f1a\u81ea\u52a8\u8c03\u7528 <code>common_parameters<\/code>\uff0c\u89e3\u6790\u5176\u4e2d\u7684\u67e5\u8be2\u53c2\u6570\uff0c\u5e76\u5c06\u8fd4\u56de\u503c\u4f20\u9012\u7ed9 <code>commons<\/code>\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u73b0\u4ee3\u63a8\u8350\u5199\u6cd5\uff1a\u4f7f\u7528 <code>Annotated<\/code> (FastAPI 0.95.0+)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u76ee\u524d\u5f3a\u70c8\u63a8\u8350\u4f7f\u7528 <code>typing.Annotated<\/code>\uff0c\u5b83\u53ef\u4ee5\u8ba9\u7c7b\u578b\u63d0\u793a\u548c\u4f9d\u8d56\u58f0\u660e\u5206\u79bb\uff0c\u4ee3\u7801\u66f4\u6e05\u6670\uff0c\u4e14\u517c\u5bb9\u66f4\u591a\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from typing import Annotated\nfrom fastapi import FastAPI, Depends\n\napp = FastAPI()\n\ndef get_current_user(token: str):\n    # \u6a21\u62df\u9a8c\u8bc1 token \u5e76\u8fd4\u56de\u7528\u6237\n    return {&quot;username&quot;: &quot;admin&quot;, &quot;token&quot;: token}\n\n# \u4f7f\u7528 Annotated \u58f0\u660e\u4f9d\u8d56\n@app.get(&quot;\/users\/me&quot;)\nasync def read_users_me(\n    current_user: Annotated[dict, Depends(get_current_user)]\n):\n    return current_user<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7c7b\u4f5c\u4e3a\u4f9d\u8d56<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f9d\u8d56\u4e0d\u4ec5\u53ef\u4ee5\u662f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u7c7b\u3002FastAPI \u4f1a\u5b9e\u4f8b\u5316\u8be5\u7c7b\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4f9d\u8d56\u9879\u6ce8\u5165\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from typing import Annotated\nfrom fastapi import FastAPI, Depends\n\napp = FastAPI()\n\nclass CommonQueryParams:\n    def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):\n        self.q = q\n        self.skip = skip\n        self.limit = limit\n\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(commons: Annotated[CommonQueryParams, Depends()]):\n    # \u6ce8\u610f\uff1a\u8fd9\u91cc Depends() \u91cc\u9762\u53ef\u4ee5\u4e3a\u7a7a\uff0cFastAPI \u4f1a\u6839\u636e\u7c7b\u578b\u63d0\u793a\u81ea\u52a8\u63a8\u65ad\n    return {&quot;q&quot;: commons.q, &quot;skip&quot;: commons.skip, &quot;limit&quot;: commons.limit}<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5e26\u6709 <code>yield<\/code> \u7684\u4f9d\u8d56\uff08\u5904\u7406\u6e05\u7406\u5de5\u4f5c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f FastAPI \u4f9d\u8d56\u6ce8\u5165\u6700\u5b9e\u7528\u7684\u573a\u666f\u4e4b\u4e00\u3002\u5982\u679c\u4f60\u7684\u4f9d\u8d56\u9700\u8981<strong>\u5728\u8bf7\u6c42\u7ed3\u675f\u540e\u6267\u884c\u6e05\u7406\u64cd\u4f5c<\/strong>\uff08\u5982\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3001\u91ca\u653e\u6587\u4ef6\u53e5\u67c4\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>yield<\/code> \u4ee3\u66ff <code>return<\/code>\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from typing import Annotated\nfrom fastapi import FastAPI, Depends\n\napp = FastAPI()\n\n# \u6a21\u62df\u6570\u636e\u5e93\u4f1a\u8bdd\nclass FakeDBSession:\n    def __init__(self):\n        print(&quot;\ud83d\udd17 \u6570\u636e\u5e93\u8fde\u63a5\u5df2\u5efa\u7acb&quot;)\n\n    def close(self):\n        print(&quot;\ud83d\udd0c \u6570\u636e\u5e93\u8fde\u63a5\u5df2\u5173\u95ed&quot;)\n\ndef get_db():\n    db = FakeDBSession()\n    try:\n        yield db  # \u5c06 db \u6ce8\u5165\u7ed9\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\n    finally:\n        db.close()  # \u8bf7\u6c42\u7ed3\u675f\u540e\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\u8fd9\u91cc\n\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(db: Annotated[FakeDBSession, Depends(get_db)]):\n    return {&quot;status&quot;: &quot;\u4f7f\u7528\u6570\u636e\u5e93\u67e5\u8be2\u6570\u636e&quot;}<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u8fd0\u884c\u7ed3\u679c\uff1a\u5148\u6253\u5370\u201c\u5efa\u7acb\u8fde\u63a5\u201d\uff0c\u5904\u7406\u8bf7\u6c42\uff0c\u6700\u540e\u6253\u5370\u201c\u5173\u95ed\u8fde\u63a5\u201d\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f9d\u8d56\u7f13\u5b58 (Dependency Caching)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u540c\u4e00\u4e2a\u8bf7\u6c42\u4e2d\uff0c\u5982\u679c\u4f60\u591a\u6b21\u58f0\u660e\u4e86\u540c\u4e00\u4e2a\u4f9d\u8d56\uff0cFastAPI <strong>\u9ed8\u8ba4\u53ea\u4f1a\u6267\u884c\u4e00\u6b21<\/strong>\uff0c\u5e76\u7f13\u5b58\u5176\u7ed3\u679c\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u8282\u7701\u8d44\u6e90\uff08\u4f8b\u5982\u907f\u514d\u591a\u6b21\u67e5\u8be2\u6570\u636e\u5e93\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">def get_query(q: str | None = None):\n    print(&quot;\u6267\u884c\u4e86 get_query&quot;) # \u8fd9\u4e2a\u51fd\u6570\u5728\u4e00\u6b21\u8bf7\u6c42\u4e2d\u53ea\u4f1a\u6253\u5370\u4e00\u6b21\n    return {&quot;q&quot;: q}\n\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(\n    q1: Annotated[dict, Depends(get_query)],\n    q2: Annotated[dict, Depends(get_query)] # \u4e0d\u4f1a\u518d\u6b21\u6267\u884c get_query\n):\n    return {&quot;q1&quot;: q1, &quot;q2&quot;: q2}<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u5982\u679c\u4f60\u5e0c\u671b\u6bcf\u6b21\u90fd\u91cd\u65b0\u6267\u884c\uff0c\u53ef\u4ee5\u8bbe\u7f6e <code>Depends(get_query, use_cache=False)<\/code>\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u5b50\u4f9d\u8d56 (Sub-dependencies)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f9d\u8d56\u53ef\u4ee5\u62e5\u6709\u81ea\u5df1\u7684\u4f9d\u8d56\u3002FastAPI \u4f1a\u81ea\u52a8\u6784\u5efa\u4e00\u4e2a\u4f9d\u8d56\u56fe\uff08Dependency Graph\uff09\uff0c\u5e76\u6309\u6b63\u786e\u7684\u987a\u5e8f\u89e3\u6790\u5b83\u4eec\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">def verify_token(x_token: str):\n    if x_token != &quot;fake-super-secret-token&quot;:\n        raise HTTPException(status_code=400, detail=&quot;X-Token header invalid&quot;)\n    return x_token\n\ndef verify_key(x_key: str):\n    if x_key != &quot;fake-super-secret-key&quot;:\n        raise HTTPException(status_code=400, detail=&quot;X-Key header invalid&quot;)\n    return x_key\n\n# get_header \u4f9d\u8d56\u4e8e verify_token \u548c verify_key\ndef get_header(\n    x_token: Annotated[str, Depends(verify_token)],\n    x_key: Annotated[str, Depends(verify_key)]\n):\n    return {&quot;x_token&quot;: x_token, &quot;x_key&quot;: x_key}\n\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(header: Annotated[dict, Depends(get_header)]):\n    return header<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u8986\u76d6\u4f9d\u8d56 (Dependency Overrides) &#8211; \u6d4b\u8bd5\u795e\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u4f60\u901a\u5e38\u4e0d\u60f3\u8fde\u63a5\u771f\u5b9e\u7684\u6570\u636e\u5e93\u6216\u8c03\u7528\u771f\u5b9e\u7684\u5916\u90e8 API\u3002FastAPI \u63d0\u4f9b\u4e86 <code>app.dependency_overrides<\/code>\uff0c\u5141\u8bb8\u4f60\u7528 Mock \u51fd\u6570\u66ff\u6362\u771f\u5b9e\u7684\u4f9d\u8d56\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><span>25<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi.testclient import TestClient\n\n# \u771f\u5b9e\u7684\u4f9d\u8d56\ndef get_db():\n    return &quot;Real Database Connection&quot;\n\n@app.get(&quot;\/data&quot;)\nasync def get_data(db: Annotated[str, Depends(get_db)]):\n    return {&quot;db&quot;: db}\n\n# --- \u6d4b\u8bd5\u4ee3\u7801 ---\nclient = TestClient(app)\n\ndef override_get_db():\n    return &quot;Mock Database Connection&quot;\n\n# \u8986\u76d6\u4f9d\u8d56\napp.dependency_overrides[get_db] = override_get_db\n\ndef test_read_data():\n    response = client.get(&quot;\/data&quot;)\n    assert response.json() == {&quot;db&quot;: &quot;Mock Database Connection&quot;}\n\n# \u6d4b\u8bd5\u7ed3\u675f\u540e\uff0c\u5efa\u8bae\u6e05\u9664\u8986\u76d6\napp.dependency_overrides.clear()<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u89c1\u5e94\u7528\u573a\u666f\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5e93\u4f1a\u8bdd\u7ba1\u7406<\/strong>\uff1a\u4f7f\u7528 <code>yield<\/code> \u83b7\u53d6\u548c\u5173\u95ed SQLAlchemy\/SQLModel \u4f1a\u8bdd\u3002<\/li>\n\n\n\n<li><strong>\u8eab\u4efd\u9a8c\u8bc1\u4e0e\u6388\u6743<\/strong>\uff1a\u89e3\u6790 JWT Token\uff0c\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237 (<code>get_current_user<\/code>)\u3002<\/li>\n\n\n\n<li><strong>\u901f\u7387\u9650\u5236 (Rate Limiting)<\/strong>\uff1a\u68c0\u67e5 IP \u6216\u7528\u6237\u7684\u8bf7\u6c42\u9891\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5171\u4eab\u4e1a\u52a1\u903b\u8f91<\/strong>\uff1a\u5c06\u590d\u6742\u7684\u53c2\u6570\u6821\u9a8c\u6216\u6570\u636e\u9884\u5904\u7406\u63d0\u53d6\u4e3a\u72ec\u7acb\u7684\u4f9d\u8d56\u51fd\u6570\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u5177\u4f53\u7684\u573a\u666f\uff08\u6bd4\u5982\uff1a\u5982\u4f55\u7ed3\u5408 SQLAlchemy \u5199\u4f9d\u8d56\uff0c\u6216\u8005\u5982\u4f55\u505a JWT \u8ba4\u8bc1\uff09\uff0c\u53ef\u4ee5\u544a\u8bc9\u6211\uff0c\u6211\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9488\u5bf9\u6027\u7684\u4ee3\u7801\u793a\u4f8b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FastAPI \u7684\u4f9d\u8d56\u6ce8\u5165\uff08Dependency Injec&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-fastapi-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}