SpringBoot依賴注入的三種方式
SpringBoot依賴注入的三種方式
1.使用 XML 配置依賴注入
在 Spring Boot 中,使用 XML 配置依賴注入(DI)時(shí),需要使用<bean>元素來(lái)定義 bean,并使用<property>元素來(lái)為 bean 的屬性注入值或依賴對(duì)象。
以下是一個(gè)簡(jiǎn)單的示例:
- 在src/main/resources目錄下創(chuàng)建applicationContext.xml文件。
- 在該文件中定義一個(gè) testBean bean,并注入一個(gè) String 類型的屬性name和一個(gè) UserService 類型的依賴對(duì)象。
<?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="testBean" class="com.example.demo.TestBean">
<property name="name" value="小明"/>
<property name="userService" ref="userService"/>
</bean>
<bean id="userService" class="com.example.demo.UserService"/>
</beans>
- 在 TestBean 類中聲明一個(gè) name 屬性和一個(gè) UserService 的依賴:
public class TestBean {
private String name;
private UserService userService;
//setter和getter方法省略
}
- 在啟動(dòng)類中調(diào)用 ApplicationContext 的構(gòu)造函數(shù)并傳入
applicationContext.xml文件的路徑,然后通過(guò) getBean 方法獲取 TestBean 實(shí)例,并訪問(wèn)它的屬性和方法:
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestBean testBean = (TestBean)context.getBean("testBean");
String name = testBean.getName();
UserService userService = testBean.getUserService();
//使用testBean和userService進(jìn)行其他操作
}
}
這樣就完成了 Spring Boot 中使用 XML 配置依賴注入的過(guò)程。需要注意的是,在 Spring Boot 中,官方推薦使用 JavaConfig(基于 Java 類的配置方式)或注解(Annotation)來(lái)進(jìn)行依賴注入,因?yàn)樗鼈兏臃奖愫鸵子诰S護(hù)。
2.使用 Java 配置類實(shí)現(xiàn)依賴注入
使用 Java Config 實(shí)現(xiàn)依賴注入可以通過(guò)@Configuration和@Bean注解來(lái)實(shí)現(xiàn)。
以下是一個(gè)簡(jiǎn)單的示例:
- 創(chuàng)建一個(gè)配置類,并使用@Configuration注解標(biāo)記,定義兩個(gè) Bean:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public TestBean testBean() {
return new TestBean("小明");
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
}
- 定義 TestBean 類,并在該類中聲明一個(gè) name 屬性:
public class TestBean {
private String name;
public TestBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 定義 UserService 接口和它的實(shí)現(xiàn)類 UserServiceImpl:
public interface UserService {
void addUser();
}
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("Add user success");
}
}
- 在啟動(dòng)類中使用 AnnotationConfigApplicationContext 獲取配置類對(duì)象,然后使用
getBean()方法獲取 TestBean 和 UserService 實(shí)例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
TestBean testBean = context.getBean(TestBean.class);
String name = testBean.getName();
UserService userService = context.getBean(UserService.class);
userService.addUser();
}
}
這樣就完成了使用 JavaConfig 實(shí)現(xiàn)依賴注入的過(guò)程。需要注意的是,JavaConfig 等價(jià)于 XML 配置文件,但是 JavaConfig 更加的面向?qū)ο?,更加靈活,更加易于維護(hù)。
3.使用注解來(lái)進(jìn)行依賴注入
可以使用注解來(lái)進(jìn)行依賴注入,常用的注解有@Autowired和@Qualifier。
以下是一個(gè)簡(jiǎn)單的示例:
- 定義一個(gè) Service 類,使用@Autowired注解注入TestBean:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Service {
@Autowired
private TestBean testBean;
public String getName() {
return testBean.getName();
}
}
- 定義 TestBean 類,測(cè)試注入是否成功:
import org.springframework.stereotype.Component;
@Component
public class TestBean {
private String name = "小明";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 在啟動(dòng)類中使用 AnnotationConfigApplicationContext 獲取配置類對(duì)象,然后使用
getBean()方法獲取 Service 實(shí)例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Service service = context.getBean(Service.class);
System.out.println(service.getName());
}
}
運(yùn)行啟動(dòng)類,可以看到控制臺(tái)輸出:
小明
這樣就完成了使用注解進(jìn)行依賴注入的過(guò)程。需要注意的是,使用注解可以使代碼更加簡(jiǎn)潔、易于閱讀和維護(hù),但是需要注意注解的使用和作用范圍。
以上就是SpringBoot依賴注入的三種方式的詳細(xì)內(nèi)容,更多關(guān)于Java SpringBoot依賴注入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot?security驗(yàn)證碼的登錄實(shí)例
這篇文章主要介紹了springboot?security驗(yàn)證碼的登錄實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Java BufferedImage轉(zhuǎn)換為MultipartFile方式
這篇文章主要介紹了Java BufferedImage轉(zhuǎn)換為MultipartFile方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
elasticsearch節(jié)點(diǎn)的transport請(qǐng)求發(fā)送處理分析
這篇文章主要為大家介紹了elasticsearch節(jié)點(diǎn)的transport請(qǐng)求發(fā)送處理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
JAVA SpringBoot統(tǒng)一日志處理原理詳解
這篇文章主要介紹了SpringBoot的統(tǒng)一日志處理原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09

