SpringBoot 創(chuàng)建對象常見的幾種方式小結
在 Spring Boot 中,將 Bean 對象添加到 IOC 容器中,通用的有下面幾種方式:
- 使用
@Component
、@Service
、@Repository
或@Controller
注解 - 使用
@Configuration
和@Bean
注解 - 使用
@Import
注解導入其他配置類 - 通過
ApplicationContext
編程方式手動注冊 Bean
1. 使用 @Component(或 @Service、@Repository、@Controller)注解
最常見的方式,Spring 會自動掃描并將帶有這些注解的類注冊為 Bean。
import org.springframework.stereotype.Component; @Component public class User { public void say() { System.out.println("Hello User!"); } }
配置掃描路徑(通常在 @SpringBootApplication
上已經(jīng)啟用了掃描,如果需要指定包路徑,可以使用 @ComponentScan
注解):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
在 Spring Boot 啟動時,@Component
注解的類會自動注冊為 Bean,并添加到 IOC 容器中。
2. 使用 @Configuration 和 @Bean 注解
通過在 @Configuration
注解的配置類中使用 @Bean
注解,可以手動將對象添加到 Spring 容器中。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class BeanConfig { @Bean public User user() { return new User(); } }
User 類:
public class user { public void say() { System.out.println("Hello User!"); } }
BeanConfig
類使用 @Bean
注解注冊了一個 user
實例,Spring 會將其自動加入到 IOC 容器中。
3. 使用 @Import 注解導入配置類
@Import
注解可以導入其他配置類,將其配置的 Bean 添加到當前應用的 IOC 容器中。
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(BeanConfig.class) public class MainConfig { // 通過 @Import 導入 BeanConfig 中的 Bean }
MainConfig
類通過 @Import(BeanConfig.class)
導入了 BeanConfig
中定義的所有 Bean,這樣 MyBean
也會被注冊到 IOC 容器中。
4. 使用 ApplicationContext 編程方式手動注冊 Bean
在某些特殊的場景中,可能需要手動編程注冊 Bean,這時可以使用 AnnotationConfigApplicationContext
或 GenericWebApplicationContext
類。
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ManualBeanRegistration { public static void main(String[] args) { // 創(chuàng)建 Spring 容器 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // 注冊配置類 context.register(BeanConfig.class); // 啟動容器 context.refresh(); // 獲取并使用 Bean MyBean myBean = context.getBean(MyBean.class); myBean.printMessage(); // 關閉容器 context.close(); } }
通過 AnnotationConfigApplicationContext
顯式地手動注冊了 BeanConfig
配置類,并啟動了 Spring 容器。
總結
@Component
注解(及其衍生注解@Service
、@Repository
、@Controller
)是最常用的方式,通過自動掃描自動將 Bean 注冊到 IOC 容器中。@Configuration
和@Bean
注解 可以在配置類中手動注冊 Bean。@Import
注解 可以將其他配置類中的 Bean 導入到當前配置類中。- 手動注冊 通過
ApplicationContext
等類可以編程方式注冊 Bean。
到此這篇關于SpringBoot 創(chuàng)建對象常見的幾種方式小結的文章就介紹到這了,更多相關SpringBoot 創(chuàng)建對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中Redis存儲String類型會有亂碼的問題及解決方案
在java中使用Redis存儲String類型的數(shù)據(jù)時,會出現(xiàn)亂碼,我寫了一條存儲key為name,值為虎哥的字符串,然后獲取一下這個key為name的值,打印得到的值,下面通過實例代碼介紹Java中Redis存儲String類型會有亂碼的問題及解決方案,一起看看吧2024-04-04詳解SpringMVC的url-pattern配置及原理剖析
這篇文章主要介紹了SpringMVC的url-pattern配置及原理剖析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06springboot中如何通過main方法調(diào)用service或dao
這篇文章主要介紹了springboot中如何通過main方法調(diào)用service或dao,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot項目部署到阿里云服務器的實現(xiàn)步驟
本文主要介紹了SpringBoot項目部署到阿里云服務器的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06