Spring容器添加組件方式實(shí)現(xiàn)
本博客介紹SpringBoot項(xiàng)目中將組件添加到Spring容器中的方法,SpringBoot項(xiàng)目有一個(gè)很明顯的優(yōu)點(diǎn),就是不需要再編寫(xiě)xml配置文件,只需要用SpringBoot的注解就可以實(shí)現(xiàn)類(lèi)似功能,不過(guò)其實(shí)SpringBoot項(xiàng)目還是支持引入xml配置文件的,所以本博客介紹一下兩種使用方式
ok,介紹一下SpringBoot項(xiàng)目的@ImportResource注解,這個(gè)注解的作用就是引入一些xml資源,加載到Spring容器里
建個(gè)TestBean類(lèi)
public class TestService {
}
新建一個(gè)beans.xml,寫(xiě)一個(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類(lèi)里直接引用,也可以加載Configuration配置類(lèi)上面
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è)試類(lèi):
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)過(guò)測(cè)試,返回的是true,ok,換Springboot注解的方式實(shí)現(xiàn)
新建一個(gè)PropertiesConfig配置類(lèi),注意:組件的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 {
//通過(guò)@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件名稱(chēng)就是@Bean注解對(duì)應(yīng)的方法名
其實(shí)以前寫(xiě)Spring項(xiàng)目的時(shí)候,很顯然也可以用@Service或者@Controller注解將組件添加到容器里,如果你去點(diǎn)一下源碼,其實(shí)這些注解都有一個(gè)共同點(diǎn)就是都引入了@Component注解,而本博客介紹的@Configuration注解,本質(zhì)上也是引入了@Component注解,而@Bean是沒(méi)有引入的,所以,如果你只加@Bean,而不加@Configuration注解的情況,是不可以將組件添加到Spring容器的
example source:github例子代碼下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring實(shí)戰(zhàn)之容器中的工程Bean用法示例
- Spring實(shí)戰(zhàn)之讓Bean獲取Spring容器操作示例
- springboot關(guān)于容器啟動(dòng)事件總結(jié)
- Spring創(chuàng)建IOC容器的方式解析
- Spring IoC容器知識(shí)點(diǎn)詳解
- 基于SpringMVC的全局異常處理器介紹
- 解析Java的Spring框架的BeanPostProcessor發(fā)布處理器
- Spring中的后置處理器BeanPostProcessor詳解
- Spring Validator接口校驗(yàn)與全局異常處理器
- Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
- Spring實(shí)戰(zhàn)之容器后處理器操作示例
相關(guān)文章
SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)
本文主要介紹了SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java實(shí)體映射工具M(jìn)apStruct使用方法詳解
MapStruct是用于代碼中JavaBean對(duì)象之間的轉(zhuǎn)換,例如DO轉(zhuǎn)換為DTO,DTO轉(zhuǎn)換為VO,或Entity轉(zhuǎn)換為VO等場(chǎng)景,這篇文章主要給大家介紹了關(guān)于Java實(shí)體映射工具M(jìn)apStruct使用的相關(guān)資料,需要的朋友可以參考下2021-11-11
解讀動(dòng)態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問(wèn)題
這篇文章主要介紹了解讀動(dòng)態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
java使用RSA加密方式實(shí)現(xiàn)數(shù)據(jù)加密解密的代碼
這篇文章給大家分享java使用RSA加密方式實(shí)現(xiàn)數(shù)據(jù)加密解密,通過(guò)實(shí)例代碼文字相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下2019-11-11
Java?Process中waitFor()的問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于Java?Process中waitFor()問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-12-12
java 用泛型參數(shù)類(lèi)型構(gòu)造數(shù)組詳解及實(shí)例
這篇文章主要介紹了java 用泛型參數(shù)類(lèi)型構(gòu)造數(shù)組詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02

