摘要:使用依賴注入的方式很簡單,只需要給添加相應(yīng)的注解即可用于標(biāo)注業(yè)務(wù)層組件用于標(biāo)注控制層組件用于標(biāo)注數(shù)據(jù)訪問組件,即組件泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。
spring boot使用依賴注入的方式很簡單,只需要給添加相應(yīng)的注解即可
@Service用于標(biāo)注業(yè)務(wù)層組件
@Controller用于標(biāo)注控制層組件
@Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件
@Component泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。
然后在使用的地方使用@Autowired即可
創(chuàng)建MyComponent,使用@Componentimport org.springframework.stereotype.Component; @Component//泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。 public class MyComponent { public void hi(String name) { System.out.println("hi " + name + ",I am MyComponent"); } }創(chuàng)建MyController,使用@Controller
import org.springframework.stereotype.Controller; @Controller//用于標(biāo)注控制層組件 public class MyController { public void hi(String name) { System.out.println("hi " + name + ",I am MyController"); } }創(chuàng)建MyRepository,使用@Repository
@Repository//用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件 public class MyRepository { public void hi(String name) { System.out.println("hi " + name + ",I am MyRepository"); } }創(chuàng)建MyService,MyServiceImpl,使用@Service
public interface MyService { void doSomeThing(); }
import org.springframework.stereotype.Service; @Service//用于標(biāo)注業(yè)務(wù)層組件 public class MyServiceImpl implements MyService { @Override public void doSomeThing() { System.out.println("i am MyServiceImpl"); } }單元測試
在src/test/java/你的包名/你的項目名ApplicationTests編寫對應(yīng)的單元測試來驗證是否可以成功注入
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class DiApplicationTests { @Autowired//自動注入 private MyController myController; @Autowired//自動注入 private MyRepository myRepository; @Autowired//自動注入 private MyComponent myComponent; @Autowired//自動注入實現(xiàn)了該接口的bean private MyService myService; @Test public void contextLoads() { myController.hi("lierabbit"); myRepository.hi("lierabbit"); myComponent.hi("lierabbit"); myService.doSomeThing(); } }
運行測試用例
顯示紅圈的4句話證明成功注入
源碼地址:https://github.com/LieRabbit/...
原文地址:https://lierabbit.cn/2018/01/...
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/68266.html
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:簡介簡化應(yīng)用開發(fā)的一個框架整個技術(shù)棧的一個大整合開發(fā)的一站式解決方案微服務(wù),微服務(wù)架構(gòu)風(fēng)格服務(wù)微化一個應(yīng)用應(yīng)該是一組小型服務(wù)可以通過的方式進(jìn)行互通單體應(yīng)用微服務(wù)每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元環(huán)境準(zhǔn)備推薦及以上以上版 1、Spring Boot 簡介簡化Spring應(yīng)用開發(fā)的一個框架; 整個Spring技術(shù)棧的一個大整合; J2EE開發(fā)的一站式解決方案; 2、微...
摘要:快速入門這個快速入門使用的服務(wù)器和客戶端。屬性在端點中顯示為高優(yōu)先級屬性源,如以下示例所示。名為的屬性源包含值為且具有最高優(yōu)先級的屬性。屬性源名稱中的是存儲庫,而不是配置服務(wù)器。 Spring Cloud Config快速入門 這個快速入門使用Spring Cloud Config Server的服務(wù)器和客戶端。 首先,啟動服務(wù)器,如下所示: $ cd spring-cloud-con...
閱讀 3520·2021-11-08 13:20
閱讀 3443·2021-09-30 09:48
閱讀 2677·2021-09-29 09:41
閱讀 644·2021-09-22 15:04
閱讀 2584·2021-08-23 09:44
閱讀 3836·2020-12-03 17:26
閱讀 1079·2019-08-30 14:10
閱讀 1650·2019-08-29 18:34