3個Spring Boot核心註解,你知道幾個?

2021-09-26 07:45:44 字數 984 閱讀 7715

spring boot 核心註解講解

spring boot 最大的特點是無需 xml 配置檔案,能自動掃瞄包路徑裝載並注入物件,並能做到根據 classpath 下的 jar 包自動配置。

所以 spring boot 最核心的 3 個註解就是:

1、@configuration

org.springframework.context.annotation.configuration

@target(elementtype.type)

@retention(retentionpolicy.runtime)

@documented

@configuration

public @inte***ce springbootconfiguration

2、@componentscan

org.springframework.context.annotation.componentscan

這是 spring 3.1 新增的乙個註解,用來代替配置檔案中的 component-scan 配置,開啟元件掃瞄,即自動掃瞄包路徑下的 @component 註解進行註冊 bean 例項到 context 中。

另外,@componentscans 是可重複註解,即可以配置多個,用來配置註冊不同的子包。

3、@enableautoconfiguration

org.springframework.boot.autoconfigure.enableautoconfiguration

看全路徑就知道,這是自 spring boot 誕生時新增的註解,用來提供自動配置,上面的兩個都是 spring-context 包下的,不屬於 spring boot,所以 spring 3.0 之後的去 xml 配置方式已經為 spring boot 埋下了伏筆!

SpringBoot最核心的3個註解

org springframework context annotation configuration bean 用來代替 xml 配置檔案裡面的 配置 importresource 如果有些通過類的註冊方式配置不了的,可以通過這個註解引入額外的 xml 配置檔案,有些老的配置檔案無法通過 con...

Spring Boot 最核心的 3 個註解詳解

spring boot 最大的特點是無需 xml 配置檔案,能自動掃瞄包路徑裝載並注入物件,並能做到根據 classpath 下的 jar 包自動配置 1.1 bean 代替 xml 配置檔案裡面的 1.2 importresource 有些通過類的註冊方式配置不了的,可以通過這個註解引入額外的 x...

springboot 測試時bean無法注入的問題

今天在使用springboot 測試的時候時遇到了乙個很坑的問題,自己明明注入了bean,但就是拿不到 情況如下 於是自己在controller測試看看到底注入了沒,開啟瀏覽器,發現確實注入了啊 那咋測試時獲取的偏偏是null.我懷疑自己是不是沒有使用上spring的測試模組,或者說 springb...