<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 `@ComponentScan`教程 > 原文: [http://zetcode.com/spring/componentscan/](http://zetcode.com/spring/componentscan/) Spring `@ComponentScan`教程顯示了如何在 Spring 應用中啟用組件掃描。 通過組件掃描,可以通過 Spring 容器自動檢測咖啡豆。 Spring 是用于創建企業應用的流行 Java 應用框架。 ## Spring `@ComponentScan` `@ComponentScan`注解啟用 Spring 中的組件掃描。 Spring 會自動檢測以`@Component`,`@Configuration`和`@Service`等構造型修飾的 Java 類。 使用`@ComponentScan`的`basePackages`屬性指定應掃描哪些包裝中的裝飾豆。 `@ComponentScan`注解是`<context:component-scan>` XML 標簽的替代方法。 ## Spring `@ComponentScan`示例 該應用允許使用`@ComponentScan`進行組件掃描。 我們有一個返回當前時間的服務 bean。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───service │ │ TimeService.java │ └───resources │ logback.xml └───test └───java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>componentscan</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring-version>5.1.3.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.zetcode.Application</mainClass> </configuration> </plugin> </plugins> </build> </project> ``` 在`pom.xml`文件中,我們具有基本的 Spring 依賴項`spring-core`,`spring-context`和日志記錄`logback-classic`依賴項。 `exec-maven-plugin`用于在命令行上從 Maven 執行 Spring 應用。 `resources/logback.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <configuration> <logger name="org.springframework" level="ERROR"/> <logger name="com.zetcode" level="INFO"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration> ``` `logback.xml`是 Logback 日志庫的配置文件。 `com/zetcode/service/TimeService.java` ```java package com.zetcode.service; import org.springframework.stereotype.Service; import java.time.LocalTime; @Service public class TimeService { public LocalTime getTime() { var now = LocalTime.now(); return now; } } ``` `TimeService`類帶有`@Service`注解。 Spring 在組件掃描的幫助下將其注冊為托管 Bean。 `com/zetcode/Application.java` ```java package com.zetcode; import com.zetcode.service.TimeService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @ComponentScan(basePackages = "com.zetcode") @Configuration public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { var ctx = new AnnotationConfigApplicationContext(Application.class); var timeService = (TimeService) ctx.getBean("timeService"); logger.info("The time is {}", timeService.getTime()); ctx.close(); } } ``` 該應用帶有`@ComponentScan`注解。 `basePackages`選項告訴 Spring 在`com/zetcode`包及其子包中查找組件。 ```java var ctx = new AnnotationConfigApplicationContext(Application.class); ``` `AnnotationConfigApplicationContext`是 Spring 獨立應用上下文。 它接受帶注解的`Application`作為輸入; 因此啟用了掃描。 ```java var timeService = (TimeService) ctx.getBean("timeService"); logger.info("The time is {}", timeService.getTime()); ``` 我們獲取注冊的服務 bean 并調用其方法。 ```java $ mvn -q exec:java 10:57:01.912 INFO com.zetcode.Application - The time is 10:57:01.912235800 ``` 我們運行該應用。 在本教程中,我們使用`@ComponentScan`啟用了組件掃描。 您可能也對這些相關教程感興趣: [Spring `BeanDefinitionBuilder`教程](/spring/beandefinitionbuilder/), [Spring `AnnotationConfigApplicationContext`](/spring/annotationconfigapplicationcontext/) , [Spring 單例范圍 bean](/spring/singletonscope/) , [Spring `@Bean`注解教程](/spring/bean/), [Spring `@Configuration`注解教程](/spring/configuration/), [Spring 注入列表 XML 教程](/spring/injectlistxml/), [Spring `BeanDefinitionBuilder`教程](/spring/beandefinitionbuilder/), [Spring HikariCP 教程](/articles/springhikaricp/)和 [Java 教程](/lang/java/)。
                  <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>

                              哎呀哎呀视频在线观看