Spring容器添加組件方式實(shí)現(xiàn)
本博客介紹SpringBoot項(xiàng)目中將組件添加到Spring容器中的方法,SpringBoot項(xiàng)目有一個(gè)很明顯的優(yōu)點(diǎn),就是不需要再編寫xml配置文件,只需要用SpringBoot的注解就可以實(shí)現(xiàn)類似功能,不過其實(shí)SpringBoot項(xiàng)目還是支持引入xml配置文件的,所以本博客介紹一下兩種使用方式
ok,介紹一下SpringBoot項(xiàng)目的@ImportResource注解,這個(gè)注解的作用就是引入一些xml資源,加載到Spring容器里
建個(gè)TestBean類
public class TestService {
}
新建一個(gè)beans.xml,寫一個(gè)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測(cè)試類:
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() {
//測(cè)試這個(gè)bean是否已經(jīng)加載到Spring容器
boolean flag = ioc.containsBean("testService");
System.out.println(flag);
}
}
經(jīng)過測(cè)試,返回的是true,ok,換Springboot注解的方式實(shí)現(xiàn)
新建一個(gè)PropertiesConfig配置類,注意:組件的id就是方法名
import com.example.springboot.properties.service.TestService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //@Configuration注解實(shí)踐上也是一個(gè)Component
public class PerpertiesConfig {
//通過@Bean注解將組件添加到Spring容器,組件的id就是方法名
@Bean
public TestService testService1(){
return new TestService();
}
}
Junit測(cè)試?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測(cè)試,返回的還是TRUE,如果改下name為testService就是返回FALSE的,因?yàn)榻M件名稱就是@Bean注解對(duì)應(yīng)的方法名
其實(shí)以前寫Spring項(xiàng)目的時(shí)候,很顯然也可以用@Service或者@Controller注解將組件添加到容器里,如果你去點(diǎn)一下源碼,其實(shí)這些注解都有一個(gè)共同點(diǎn)就是都引入了@Component注解,而本博客介紹的@Configuration注解,本質(zhì)上也是引入了@Component注解,而@Bean是沒有引入的,所以,如果你只加@Bean,而不加@Configuration注解的情況,是不可以將組件添加到Spring容器的
example source:github例子代碼下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC源碼之HandlerMapping處理器映射器解析
這篇文章主要介紹了SpringMVC源碼之HandlerMapping處理器映射器解析,在Spring?MVC中,HandlerMapping處理器映射器用于確定請(qǐng)求處理器對(duì)象,請(qǐng)求處理器可以是任何對(duì)象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下2023-08-08
Java輸入字母來(lái)判斷星期幾的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java輸入字母來(lái)判斷星期幾的實(shí)現(xiàn)代碼,用情況語(yǔ)句比較好,如果第一個(gè)字母一樣,則判斷用情況語(yǔ)句或if語(yǔ)句判斷第二個(gè)字母需要的朋友可以參考下2017-02-02
Java的內(nèi)存區(qū)域與內(nèi)存溢出異常你了解嗎
這篇文章主要為大家詳細(xì)介紹了Java的內(nèi)存區(qū)域與內(nèi)存溢出異常,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
Jersey Restful接口如何獲取參數(shù)的問題
這篇文章主要介紹了Jersey Restful接口如何獲取參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
如何實(shí)現(xiàn)自己的spring boot starter
這篇文章主要介紹了如何實(shí)現(xiàn)自己的spring boot starter,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Flowable?ReceiveTask使用場(chǎng)景分析
這篇文章主要為大家介紹了Flowable?ReceiveTask使用場(chǎng)景分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Spring Cloud Stream整合RocketMQ的搭建方法
本文介紹了如何使用SpringCloudStream整合RocketMQ進(jìn)行消息傳遞,SpringCloudStream是一個(gè)用于構(gòu)建與共享消息系統(tǒng)連接的框架,支持持久pub/sub語(yǔ)義和消費(fèi)者組,感興趣的朋友跟隨小編一起看看吧2024-11-11

