springboot依賴注入的三種方式
1.使用 XML 配置依賴注入
在 Spring Boot 中,使用 XML 配置依賴注入(DI)時,需要使用<bean>
元素來定義 bean,并使用<property>
元素來為 bean 的屬性注入值或依賴對象。
以下是一個簡單的示例:
-
在
src/main/resources
目錄下創(chuàng)建applicationContext.xml
文件。 -
在該文件中定義一個
testBean
bean,并注入一個 String 類型的屬性name
和一個 UserService 類型的依賴對象。<?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 類中聲明一個 name 屬性和一個 UserService 的依賴:
public class TestBean { private String name; private UserService userService; //setter和getter方法省略 }
-
在啟動類中調用 ApplicationContext 的構造函數并傳入
applicationContext.xml
文件的路徑,然后通過 getBean 方法獲取 TestBean 實例,并訪問它的屬性和方法: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進行其他操作 } }
這樣就完成了 Spring Boot 中使用 XML 配置依賴注入的過程。需要注意的是,在 Spring Boot 中,官方推薦使用 JavaConfig(基于 Java 類的配置方式)或注解(Annotation)來進行依賴注入,因為它們更加方便和易于維護。
2.使用 Java 配置類實現(xiàn)依賴注入
使用 Java Config 實現(xiàn)依賴注入可以通過@Configuration和@Bean注解來實現(xiàn)。
以下是一個簡單的示例:
-
創(chuàng)建一個配置類,并使用@Configuration注解標記,定義兩個 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 類,并在該類中聲明一個 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 接口和它的實現(xiàn)類 UserServiceImpl:
public interface UserService { void addUser(); } public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("Add user success"); } }
-
在啟動類中使用 AnnotationConfigApplicationContext 獲取配置類對象,然后使用
getBean()
方法獲取 TestBean 和 UserService 實例: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 實現(xiàn)依賴注入的過程。需要注意的是,JavaConfig 等價于 XML 配置文件,但是 JavaConfig 更加的面向對象,更加靈活,更加易于維護。
3.使用注解來進行依賴注入
可以使用注解來進行依賴注入,常用的注解有@Autowired和@Qualifier。
以下是一個簡單的示例:
-
定義一個 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; } }
-
在啟動類中使用 AnnotationConfigApplicationContext 獲取配置類對象,然后使用
getBean()
方法獲取 Service 實例: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()); } }
-
運行啟動類,可以看到控制臺輸出:文章來源:http://www.zghlxwxcb.cn/news/detail-422527.html
小明
這樣就完成了使用注解進行依賴注入的過程。需要注意的是,使用注解可以使代碼更加簡潔、易于閱讀和維護,但是需要注意注解的使用和作用范圍。文章來源地址http://www.zghlxwxcb.cn/news/detail-422527.html
到了這里,關于springboot依賴注入的三種方式的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!