<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring 注解 > 原文: [https://howtodoinjava.com/spring-core/spring-annotations/](https://howtodoinjava.com/spring-core/spring-annotations/) 了解最廣泛使用的 Spring 注解。 在本教程中,我們將簡要介紹 spring 核心提供的重要注解,這些注解用于定義 bean 和創建復雜的應用程序上下文配置。 ## 1\. Bean 注解 給定的注解列表用于在 Spring 中定義 bean 并在應用程序上下文中控制其注冊首選項。 ### `@Bean` 方法級別的注解,用于聲明 Spring bean。 當配置執行帶注解的方法時,它將返回值注冊為[`BeanFactory`](https://howtodoinjava.com/spring-core/different-spring-ioc-containers/)中的 Bean。默認情況下,bean 名稱將與方法名稱相同。 要自定義 Bean 名稱,請使用其`name`或`value`屬性。 ```java @Bean EmployeeService employeeService() { return new EmployeeServiceImpl(); } ``` ### `@Component` 指示帶注解的類是“組件”,并且在使用[基于注解的配置](https://howtodoinjava.com/spring5/core/spring-bean-java-config/)和類路徑掃描時將被自動檢測。要使用此注解,請將其應用于類,如下所示: ```java @Component public class EmployeeDAOImpl implements EmployeeDAO { ... } ``` ### [`@Repository`](https://howtodoinjava.com/spring-boot/repository-annotation/) `@Component`注解的特化。 除了將帶注解的 DAO 類導入 DI 容器之外,它還使未經檢查的異常(從 DAO 方法拋出)有資格轉換為 Spring `DataAccessException`。 ```java @Repository public class EmployeeDAOImpl implements EmployeeDAO { //... } ``` ### `@Service` `@Component`注解的特化。 它表明一個類是“業務服務門面”或類似的名稱。 ```java @Service ("employeeManager") public class EmployeeManagerImpl implements EmployeeManager { @Autowired EmployeeDAO dao; ... } ``` ### `@Controller` 專門用于注解控制器(例如 Web 控制器)的`@Component`。 它與基于`RequestMapping`注解的處理器方法結合使用。 ```java @Controller ("employeeController") public class EmployeeController { ... } ``` ### `@Qualifier` 在自動裝配過程中,如果容器中有多個同類型的 bean,那么容器將拋出運行時異常。 為了解決這個問題,我們必須使用此注解,專門告訴 spring 必須注入哪個 bean。在給定的示例中,如果有兩個類型為存儲庫的 bean,則在運行時,將注入名稱為`fsRepository`的 bean。 ```java public class EmployeeService { @Autowired @Qualifier("fsRepository") private Repository repository; } ``` ### `@Autowired` 將構造函數,字段,setter 方法或配置方法標記為通過依賴項注入自動裝配。 我們可以使用`required`屬性標記注解的依賴項是否是必需的(強制填充)。 默認情況下,其值為`true`。 ```java public class EmployeeService { @Autowired private EmployeeDao dao; } ``` ### [`@Required`](https://howtodoinjava.com/spring-core/spring-required-annotation-and-requiredannotationbeanpostprocessor-example/) 缺省的 bean 自動裝配僅檢查是否已設置依賴項。 它不檢查分配的值是否為`null`。 使用`@Required`,我們可以檢查設置的值是否為非空值。 現在已不推薦使用。 ### `@Value` 適用于字段或方法/構造函數參數級別,并指示受影響參數的默認值表達式。 ```java public class SomeService { @Value("${ENV}") private String environment; } ``` ### `@Lazy` 指示是否要延遲初始化 bean。 默認情況下,在 Spring DI 中,將進行立即初始化。當在任何 bean 上應用該 bean 時,只有在另一個 bean 引用或從封裝的`BeanFactory`中顯式檢索到該 bean 時,才會初始化該 bean。 ```java public class SomeService { @Autowired @Lazy private RemoteService remoting; } ``` ### `@DependsOn` 在組件掃描期間,它用于指定當前 bean 所依賴的 bean。 保證指定的 bean 由容器在該 bean 之前創建。 ```java public class SomeService { @Autowired @DependsOn ("pingService") private RemoteService remoting; } ``` ### `@Lookup` 表示一種方法為“查找”方法。 最好用于將原型作用域的 bean 注入到單例 bean 中。 ```java @Component @Scope("prototype") public class AppNotification { //prototype-scoped bean } @Component public class NotificationService { @Lookup public AppNotification getNotification() { //return new AppNotification(); } } ``` ### `@Primary` 指示當多個候選者有資格自動裝配單值依賴項時,應優先考慮 Bean。當不使用`@Primary`時,我們可能需要提供`@Qualifier`注解以正確注入 bean。在給定的示例中,當自動裝配`FooRepository`時,將注入`HibernateFooRepository`的實例 – 直到使用`@Qualifier`注解。 ```java @Component public class JdbcFooRepository extends FooRepository { } @Primary @Component public class HibernateFooRepository extends FooRepository { } ``` ### `@Scope` 指示用于帶注解類型的實例的范圍的名稱。 在 Spring 5 中,bean 可以屬于六個范圍之一,即單例,原型,請求,會話,應用程序和 websocket。 | ## 2\. 上下文配置注解 這些注解有助于將不同的 Bean 綁定在一起以形成運行時應用程序上下文。 ### `@ComponentScan` `@ComponentScan`與`@Configuration`一起用于啟用和配置組件掃描。 默認情況下,如果不指定路徑,它將掃描當前程序包及其所有子程序包中的組件。使用組件掃描,spring 可以自動掃描所有帶有[原型注解](https://howtodoinjava.com/spring-core/stereotype-annotations/)`@Component`,`@Controller`,`@Service`和`@Repository`并使用`BeanFactory`對其進行配置。 ```java @Configuration @ComponentScan(basePackages = {com.howtodoinjava.data.jpa}) public class JpaConfig { } ``` ### [`@Configuration`](https://howtodoinjava.com/spring-core/spring-configuration-annotation/) 指示一個類聲明一個或多個`@Bean`方法,并且與`@ComponentScan`一起使用時,容器可以對其進行處理以生成 bean 定義。 ```java @Configuration public class AppConfig { @Bean public AppUtils appUtils() { return new AppUnits(); } } ``` ### `@Profile` 指示當一個或多個指定的配置文件處于活動狀態時,組件可以進行 Bean 注冊。 配置文件是 Bean 的命名邏輯分組,例如開發,生產等。 ```java @Bean @Profile("dev") public AppUtils appUtils() { return new DevAppUnits(); } @Bean @Profile("prod") public AppUtils appUtils() { return new ProdAppUnits(); } ``` ### `@Import` 指示要導入的一個或多個組件類,通常是`@Configuration`類。導入的`@Configuration`類中聲明的`@Bean`定義應使用`@Autowired`注入進行訪問。 ```java @Configuration @Import({ JpaConfig.class, SchedulerConfig.class }) public class AppConfig { } ``` ### `@ImportResource` 指示一個或多個包含要導入的 bean 定義的資源。 它用于 XML bean 定義,就像`@Import`用于使用`@Bean`的 Java 配置一樣。 ```java @Configuration @ImportResource( { "spring-context.xml" } ) public class ConfigClass { } ``` 請把關于上述 Spring 注解列表或解釋的問題與我聯系。 學習愉快! 參考: [上下文注解包](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/package-summary.html) [構造型注解包](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/package-summary.html)
                  <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>

                              哎呀哎呀视频在线观看