<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring 的`@Autowired`注解 > 原文: [https://javatutorial.net/autowired-annotation-in-spring](https://javatutorial.net/autowired-annotation-in-spring) `@Autowired`注解是實現依賴注入的一種相對較新的樣式。 它允許您將其他豆注入另一個所需的豆中。 與`@Required`的注解類似,`@Autowired`注解可用于在 setter 方法以及構造函數和屬性上“自動裝配” bean。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## setter 方法上的`@Autowired` 請注意,在 setter 方法上使用`@Autowired`注解時,它會自動擺脫 XML 配置文件中的`<property>`元素。 相反,當 Spring 發現使用`@Autowired`注釋的 setter 方法時,它將對該特定方法執行按類型“自動裝配”。 讓我們看看`@Autowired`在實踐中。 更具體地說,在設置方法上使用`@Autowired`。 ### `ExampleService.java` ```java public class ExampleService { private Employee employee; @Autowired public void setEmployee(Employee emp) { // setting the employee employee = emp; } } ``` ### 分解 上面的示例中沒有什么花哨的。 我們只有一個名為`ExampleService`的服務類,該類具有一個`Employee`類型的實例變量,并且有一個名為`setEmployee(Employee emp)`的設置方法,該方法僅用于設置雇員為參數給出的任何東西。 感謝`@Autowired`注解,在實例化`ExampleService`時,將`Employee`的實例作為該方法的參數注入。 ## 構造函數上的`@Autowired`注釋 ```java public class ExampleService { private Employee employee; @Autowired public ExampleService(Employee employee) { this.employee = employee; } @Autowired public void setEmployee(Employee emp) { // setting the employee employee = emp; } } ``` 再次感謝`@Autowired`注釋,當實例化`ExampleService`時,將`Employee`的實例作為構造函數的參數注入。 ## 屬性上的`@Autowired`注釋 自動裝配屬性節省了我們的時間和代碼行。 怎么樣? 好吧,當我們在屬性上使用該注解時,這些屬性不再需要 getter 和 setter。 酷吧? ```java public class ExampleService { @Autowired private Employee employee; @Autowired public ExampleService(Employee employee) { this.employee = employee; } @Autowired public void setEmployee(Employee emp) { // setting the employee employee = emp; } } ``` 在上面的代碼片段中,我們自動連接了名為`employee`的屬性。 因此,它不再需要 setter 和 getter 方法。`employee`在創建`ExampleService`時被 Spring 注入。 ## 可選依賴項 `@Autowired`也可以是可選的。 像這樣: ```java public class ExampleService { @Autowired(required = false) private Employee employee; @Autowired public ExampleService(Employee employee) { this.employee = employee; } @Autowired public void setEmployee(Employee emp) { // setting the employee employee = emp; } } ``` `required = false`使其不是必需的依賴項。 我們需要可選依賴項的原因是因為 Spring 希望在構造依賴項 bean 時`@Autowired`的依賴項可用。 否則,將引發錯誤。 由于`required = false`,我們可以解決此問題。 ## 總結 通過使用`@Autowired`注解,我們節省了幾行代碼,還節省了一些時間,因為我們不需要指定屬性和構造函數參數。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看