Spring容器添加組件方式實現(xiàn)
本博客介紹SpringBoot項目中將組件添加到Spring容器中的方法,SpringBoot項目有一個很明顯的優(yōu)點,就是不需要再編寫xml配置文件,只需要用SpringBoot的注解就可以實現(xiàn)類似功能,不過其實SpringBoot項目還是支持引入xml配置文件的,所以本博客介紹一下兩種使用方式
ok,介紹一下SpringBoot項目的@ImportResource注解,這個注解的作用就是引入一些xml資源,加載到Spring容器里
建個TestBean類
public class TestService { }
新建一個beans.xml,寫一個service的bean配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="testService" class="com.example.springboot.properties.service.TestService"></bean> </beans>
然后可以Application類里直接引用,也可以加載Configuration配置類上面
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(locations = {"classpath:beans.xml"}) public class SpringbootPropertiesConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringbootPropertiesConfigApplication.class, args); } }
Junit測試類:
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; @SpringBootTest class SpringbootPropertiesConfigApplicationTests { //裝載ioc容器 @Autowired ApplicationContext ioc; @Test void contextLoads() { //測試這個bean是否已經(jīng)加載到Spring容器 boolean flag = ioc.containsBean("testService"); System.out.println(flag); } }
經(jīng)過測試,返回的是true,ok,換Springboot注解的方式實現(xiàn)
新建一個PropertiesConfig配置類,注意:組件的id就是方法名
import com.example.springboot.properties.service.TestService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration //@Configuration注解實踐上也是一個Component public class PerpertiesConfig { //通過@Bean注解將組件添加到Spring容器,組件的id就是方法名 @Bean public TestService testService1(){ return new TestService(); } }
Junit測試?yán)^續(xù):
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; @SpringBootTest class SpringbootPropertiesConfigApplicationTests { @Autowired ApplicationContext ioc; @Test void contextLoads() { //傳方法名testService1 boolean flag = ioc.containsBean("testService1"); System.out.println(flag); } }
Junit測試,返回的還是TRUE,如果改下name為testService就是返回FALSE的,因為組件名稱就是@Bean注解對應(yīng)的方法名
其實以前寫Spring項目的時候,很顯然也可以用@Service或者@Controller注解將組件添加到容器里,如果你去點一下源碼,其實這些注解都有一個共同點就是都引入了@Component注解,而本博客介紹的@Configuration注解,本質(zhì)上也是引入了@Component注解,而@Bean是沒有引入的,所以,如果你只加@Bean,而不加@Configuration注解的情況,是不可以將組件添加到Spring容器的
example source:github例子代碼下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring實戰(zhàn)之容器中的工程Bean用法示例
- Spring實戰(zhàn)之讓Bean獲取Spring容器操作示例
- springboot關(guān)于容器啟動事件總結(jié)
- Spring創(chuàng)建IOC容器的方式解析
- Spring IoC容器知識點詳解
- 基于SpringMVC的全局異常處理器介紹
- 解析Java的Spring框架的BeanPostProcessor發(fā)布處理器
- Spring中的后置處理器BeanPostProcessor詳解
- Spring Validator接口校驗與全局異常處理器
- Spring實戰(zhàn)之Bean的后處理器操作示例
- Spring實戰(zhàn)之容器后處理器操作示例
相關(guān)文章
SpringSecurity跨域請求偽造(CSRF)的防護實現(xiàn)
本文主要介紹了SpringSecurity跨域請求偽造(CSRF)的防護實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題
這篇文章主要介紹了解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密的代碼
這篇文章給大家分享java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密,通過實例代碼文字相結(jié)合給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下2019-11-11java 用泛型參數(shù)類型構(gòu)造數(shù)組詳解及實例
這篇文章主要介紹了java 用泛型參數(shù)類型構(gòu)造數(shù)組詳解及實例的相關(guān)資料,需要的朋友可以參考下2017-02-02