您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“SpringBoot依賴注入的三種方式是什么”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“SpringBoot依賴注入的三種方式是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
在 Spring Boot 中,使用 XML 配置依賴注入(DI)時(shí),需要使用<bean>元素來定義 bean,并使用<property>元素來為 bean 的屬性注入值或依賴對(duì)象。
以下是一個(gè)簡單的示例:
在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
文件的路徑,然后通過 getBean 方法獲取 TestBean 實(shí)例,并訪問它的屬性和方法:
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 配置依賴注入的過程。需要注意的是,在 Spring Boot 中,官方推薦使用 JavaConfig(基于 Java 類的配置方式)或注解(Annotation)來進(jìn)行依賴注入,因?yàn)樗鼈兏臃奖愫鸵子诰S護(hù)。
使用 Java Config 實(shí)現(xiàn)依賴注入可以通過@Configuration和@Bean注解來實(shí)現(xiàn)。
以下是一個(gè)簡單的示例:
創(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)依賴注入的過程。需要注意的是,JavaConfig 等價(jià)于 XML 配置文件,但是 JavaConfig 更加的面向?qū)ο螅屿`活,更加易于維護(hù)。
可以使用注解來進(jìn)行依賴注入,常用的注解有@Autowired和@Qualifier。
以下是一個(gè)簡單的示例:
定義一個(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 類,測試注入是否成功:
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)行依賴注入的過程。需要注意的是,使用注解可以使代碼更加簡潔、易于閱讀和維護(hù),但是需要注意注解的使用和作用范圍。
讀到這里,這篇“SpringBoot依賴注入的三種方式是什么”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。