知識庫 / Spring RSS 訂閱

上下文:annotation-config 與 context:component-scan 的區別

Spring
HongKong
9
01:11 PM · Dec 06 ,2025

1. 概述

本教程將介紹 Spring 兩個主要 XML 配置元素之間的差異:<context:annotation-config><context:component-scan>

2. Bean 定義

正如我們所知,Spring 提供了兩種定義 Bean 和依賴的方式:XML 配置和 Java 註解。Spring 的註解還可以分為兩類:依賴注入註解和 Bean 註解。

在註解之前,我們必須手動在 XML 配置文件中定義所有 Bean 和依賴。現在,得益於 Spring 的註解,它能夠自動發現並連接所有 Bean 和依賴,從而省去了我們手動配置 XML 的麻煩。

然而,我們應該記住,註解在沒有激活的情況下是毫無用處的。為了激活它們,我們可以將 <context:annotation-config><context:component-scan> 添加到我們的 XML 文件中。

在本節中,我們將探討 <context:annotation-config><context:component-scan> 在激活註解方面的差異。

3. 通過激活 <em context:annotation-config</em> 進行註解激活

<em context:annotation-config</em> 註解主要用於激活依賴注入註解。<em @Autowired</em>, <em @Qualifier</em>, <em @PostConstruct</em>, <em @PreDestroy</em>, 和 <em @Resource</em> 都是 <em context:annotation-config</em> 可以解析的。

讓我們通過一個簡單的例子來觀察 <em context:annotation-config</em> 如何簡化我們的 XML 配置。

首先,讓我們創建一個具有依賴字段的類:

public class UserService {
    @Autowired
    private AccountService accountService;
}
public class AccountService {}

現在,讓我們定義我們的 Bean。

<bean id="accountService" class="AccountService"></bean>

<bean id="userService" class="UserService"></bean>

在繼續之前,我們首先需要聲明 XML 中的 Bean。這是因為<context:annotation-config> 僅會激活已在應用程序上下文中註冊的 Bean。

如您所見,我們使用<em>@Autowired</em> 註解了

如果我們沒有使用<em>@Autowired</em>,那麼我們就需要手動設置

<bean id="userService" class="UserService">
    <property name="accountService" ref="accountService"></property>
</bean>

現在,我們可以通過單元測試引用我們的 Bean 和依賴項:

@Test
public void givenContextAnnotationConfig_whenDependenciesAnnotated_thenNoXMLNeeded() {
    ApplicationContext context
      = new ClassPathXmlApplicationContext("classpath:annotationconfigvscomponentscan-beans.xml");

    UserService userService = context.getBean(UserService.class);
    AccountService accountService = context.getBean(AccountService.class);

    Assert.assertNotNull(userService);
    Assert.assertNotNull(accountService);
    Assert.assertNotNull(userService.getAccountService());
}

嗯,這裏似乎出問題了。看起來 Spring 並沒有為 accountService 進行依賴注入,儘管我們已經通過 @Autowired 進行了註解。看起來 @Autowired 沒有生效。為了解決這個問題,我們只需在 XML 文件上方添加以下行:

<context:annotation-config/>

4. 通過掃描組件激活標註

<em lang="en">context:annotation-config</em 類似,<em lang="en">context:component-scan</em 也能識別和處理依賴注入標註。 此外,<em lang="en">context:component-scan</em 能夠檢測到 <em lang="en">context:annotation-config</em 不會檢測到的 Bean 標註。

基本上,<em lang="en">context:component-scan</em 通過包掃描來檢測標註。換句話説,它告訴 Spring 需要掃描哪些包來查找帶有標註的 Bean 或組件。

<em lang="en">@Component</em<em lang="en">@Repository</em<em lang="en">@Service</em<em lang="en">@Controller</em<em lang="en">@RestController</em<em lang="en">@Configuration</em 都是 <em lang="en">context:component-scan</em 可以檢測到的標註。

@Component
public class UserService {
    @Autowired
    private AccountService accountService;
}

@Component
public class AccountService {}

在這裏, 標註表明我們的類是 Bean。現在,我們可以清理 XML 文件中的所有 Bean 定義。當然,我們還需要在頂部保留

<context:component-scan
  base-package="com.baeldung.annotationconfigvscomponentscan.components" />

最後,請注意,Spring 會在由 base-package 屬性指示的包下查找帶有註解的 Bean 和依賴項。

5. 結論

在本教程中,我們探討了 <em>context:annotation-config</em> 和 <em>context:component-scan</em> 之間的差異。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.