溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring容器中怎么添加組件

發(fā)布時間:2021-08-06 16:35:50 來源:億速云 閱讀:173 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)Spring容器中怎么添加組件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

建個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;@SpringBootTestclass 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注解的方式實(shí)現(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注解實(shí)踐上也是一個Componentpublic 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;@SpringBootTestclass SpringbootPropertiesConfigApplicationTests { @Autowired ApplicationContext ioc; @Test void contextLoads() { //傳方法名testService1 boolean flag = ioc.containsBean("testService1"); System.out.println(flag); }}

Junit測試,返回的還是TRUE,如果改下name為testService就是返回FALSE的,因?yàn)榻M件名稱就是@Bean注解對應(yīng)的方法名

其實(shí)以前寫Spring項目的時候,很顯然也可以用@Service或者@Controller注解將組件添加到容器里,如果你去點(diǎn)一下源碼,其實(shí)這些注解都有一個共同點(diǎn)就是都引入了@Component注解,而本博客介紹的@Configuration注解,本質(zhì)上也是引入了@Component注解,而@Bean是沒有引入的,所以,如果你只加@Bean,而不加@Configuration注解的情況,是不可以將組件添加到Spring容器的

看完上述內(nèi)容,你們對Spring容器中怎么添加組件有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI