參考:使用阿里云進行OSS對象存儲(超詳細)
一文掌握SpringBoot注解之@Component 知識文集(1)
@ConfigurationProperties注解原理與實戰
1.配置屬性類?AliOssProperties
package com.sky.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "sky.alioss") // 綁定配置文件前綴
@Data // Lombok自動生成getter/setter
public class AliOssProperties {private String endpoint; // OSS訪問域名private String accessKeyId; // 訪問密鑰IDprivate String accessKeySecret; // 訪問密鑰private String bucketName; // 存儲桶名稱
}
作用:從配置文件(如application.yml
)讀取阿里云OSS配置參數。
alioss:endpoint: oss-cn-shenzhen.aliyuncs.comaccess-key-id: LTAI5tNeccJwteJtxpa3Xtajaccess-key-secret: rLRhmNUzAT8JPaCczOwDGvQmmdw2rkbucket-name: bucket-pl
2.自動配置類?OssConfiguration
package com.sky.config;import com.sky.properties.AliOssProperties;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@Slf4j
public class OssConfiguration {@Bean@ConditionalOnMissingBean // 容器不存在AliOssUtil時生效public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {log.info("創建阿里云OSS工具對象,配置參數:{}", aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}
作用:
-
自動將
AliOssProperties
注入工具類構造器 -
創建
AliOssUtil
的Spring Bean -
@ConditionalOnMissingBean
確保當用戶未自定義該Bean時才創建默認實例
3. 工作原理
-
屬性注入:
Spring Boot自動將application.yml
中以sky.alioss
開頭的配置映射到AliOssProperties
對象。 -
Bean創建:
當Spring容器啟動時:-
檢測到
@Configuration
注解,執行配置類邏輯 -
通過構造器參數自動注入
AliOssProperties
-
使用配置參數實例化
AliOssUtil
-
-
條件裝配:
如果項目其他地方已定義AliOssUtil
(如自定義配置),則跳過默認創建。
4. 使用示例
在Service中直接注入使用:
@Service
public class UploadService {@Autowiredprivate AliOssUtil ossUtil; // 自動注入工具類public void uploadFile(MultipartFile file) {String url = ossUtil.upload(file); // 調用OSS上傳方法// ...}
}
5. 優勢
-
解耦配置:敏感信息(密鑰等)與代碼分離
-
集中管理:所有OSS配置統一在yml文件中維護
-
靈活擴展:支持自定義Bean覆蓋默認實現
-
啟動可視化:日志打印配置參數便于調試
提示:需要確保
AliOssUtil
類存在且包含匹配的構造方法,其實現應基于阿里云OSS SDK(需額外添加依賴com.aliyun.oss:aliyun-sdk-oss
)。
核心注解說明
@Component
?作用:它的作用是將一個 Java 類標識為 Spring 的組件(Bean)。
被?
@Component
?注解標注的類會被 Spring 自動掃描并注冊到應用上下文中,可以通過應用上下文獲取并使用這些組件。具體來說,@Component 注解可以用于標記任何一個普通的 Java 類,并將其納入 Spring 管理。這樣,它們就可以享受到 Spring 提供的各種依賴注入、自動裝配、AOP 等特性。
需要注意的是,@Component 注解通常作為其他更具體的注解(如 @Service、@Repository、@Controller 等)的基礎,用于創建具有特定用途的組件。
但是,@Component 注解本身并沒有明確定義 Bean 的角色或用途。
總結來說,@Component 注解的作用是將一個 Java 類標識為 Spring 組件(Bean),使其納入 Spring 的管理與控制范圍,可以享受到 Spring 提供的各種特性和便利。
以下是一些常用的注解及其用途:
1.@Service:用于標記服務層(Service),相當于早期的 Service 實現類上方也有一個 @Service 注解,用于告訴 Spring 容器,這個類是一個服務層組件,需要被掃描并加入到 Spring 容器中。
2.@Repository:用于標記持久層(DAO),表示這是一個數據庫相關的 Bean。與 @Service 注解類似,使用 @Repository 注解標記的類也會被 Spring 掃描并自動注入到其他組件中。
3.@Controller:用于標記控制層(Controller),表示這是 MVC 架構中的控制器組件。Spring 會自動掃描使用了 @Controller 注解的類,并將其注冊為控制器 Bean,在處理請求時自動將請求映射到相應的 @RequestMapping 注解標注的方法中進行處理。
4.@Configuration:用于標記配置類,主要用于定義應用程序的 Bean 以及 Bean 之間的依賴關系。與 @Component 注解不同,@Configuration 注解不需要在類名前面添加前綴,其作用相當于一個工廠,用于生產 Bean,并使用 @Bean 注解將其返回。
5.@ControllerAdvice:用于定義全局異常處理器,可以捕獲處理所有 Controller 中拋出的異常。@ControllerAdvice 注解需要結合 @ExceptionHandler 注解一起使用,用于定義異常處理方法。
6.@ModelAttribute:用于標注一個方法的返回值或方法參數,表示它們應該被添加到綁定到請求中的 Model 屬性中。在使用 @ModelAttribute 標記的方法中,可以向模型中添加屬性或組合多個模型屬性。
總結來說,以上這些注解都是基于 @Component 注解派生出來的,用于表示不同類型的 Spring Bean 組件,使用這些注解可以更加精細地管理和控制應用程序中的組件。
@ConfigurationProperties 基本使用
在 SpringBoot 中,當想需要獲取到配置文件數據時,除了可以用 Spring 自帶的 @Value 注解外,SpringBoot 還提供了一種更加方便的方式:@ConfigurationProperties。只要在 Bean 上添加上了這個注解,指定好配置文件的前綴,那么對應的配置文件數據就會自動填充到 Bean 中。
比如在application.properties文件中有如下配置文件
?? 那么按照如下注解配置,config.username=jay.zhou config.password=3333
?? 那么按照如下注解配置,SpringBoot項目中使用@ConfigurationProperties的Bean,它的username與password就會被自動注入值了。就像下面展示的那樣?
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix = "config") public class TestBean{private String username;private String password; }
SpringBoot主要幫助我們做了兩件事情。
? ? ? ? ?第一件事情就是獲取到使用@ConfigurationProperties的類。
? ? ? ? ?第二件事就是解析配置文件,并把對應的值設置到我們的Bean中。
@Data
Lombok自動生成getter/setter