本篇文章給大家分享的是有關(guān)怎么為Spring容器添加組件,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

為泰州等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及泰州網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、泰州網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
建個(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測試類:
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() {
//測試這個(gè)bean是否已經(jīng)加載到Spring容器
boolean flag = ioc.containsBean("testService");
System.out.println(flag);
}
}經(jīng)過測試,返回的是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測試?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);
}
}以上就是怎么為Spring容器添加組件,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:怎么為Spring容器添加組件
本文網(wǎng)址:http://chinadenli.net/article20/gdeejo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、外貿(mào)建站、網(wǎng)站導(dǎo)航、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)