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測試繼續(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)文章
SpringMVC源碼之HandlerMapping處理器映射器解析
這篇文章主要介紹了SpringMVC源碼之HandlerMapping處理器映射器解析,在Spring?MVC中,HandlerMapping處理器映射器用于確定請求處理器對象,請求處理器可以是任何對象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下2023-08-08
Java的內(nèi)存區(qū)域與內(nèi)存溢出異常你了解嗎
這篇文章主要為大家詳細介紹了Java的內(nèi)存區(qū)域與內(nèi)存溢出異常,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Jersey Restful接口如何獲取參數(shù)的問題
這篇文章主要介紹了Jersey Restful接口如何獲取參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
如何實現(xiàn)自己的spring boot starter
這篇文章主要介紹了如何實現(xiàn)自己的spring boot starter,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
Spring Cloud Stream整合RocketMQ的搭建方法
本文介紹了如何使用SpringCloudStream整合RocketMQ進行消息傳遞,SpringCloudStream是一個用于構(gòu)建與共享消息系統(tǒng)連接的框架,支持持久pub/sub語義和消費者組,感興趣的朋友跟隨小編一起看看吧2024-11-11

