spring的各种注解的作用-持续更新中
所謂的注入到底啥意思?
其實就是spring內部的import
和python里面的import numpy差不多的效果.
?
依賴注入:
扯白了就是你自己寫的對象,需要被spring兼容.
?
先說說所謂的自動裝配是啥意思?
Spring是一輛大卡車.
貨物是自定義的一堆類,
類在被裝上卡車前,需要配置(例如幾個箱子整齊疊在一起),這個也就是配置類發揮的作用,
所謂的配置類就是class的定義中帶有@Configuration的就是配置類
配置完了之后需要在spring導入(也就是所謂的依賴注入),也就用到了@Autowired.
由于Spring可以隨手一指(用@Autowired來注入一個瞎編的名字的bean),
并不一定非要是@Configuration所在類中,用@bean注冊過的函數,
所以出來了@Primary,哪個函數被@Primary標記過?
那么當@SpringBootApplication所在類的@Autowired想要亂注入的時候,
哪個函數頭上有@Primary,就注入這個函數.
所以,根據這個圖示,我們其實可以把一個spring實驗分成三大類:
①配置類
②啟動類
③貨物類
#--------------------------------------------------------------------------------------------------------
被@Bean注解的方法會被Spring重寫(重寫就是裝配的意思)
#--------------------------------------------------------------------------------------------------------
@Service和@Component有啥區別呢?
看了下@Service的源碼
package org.springframework.stereotype;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor;@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service {@AliasFor(annotation = Component.class)String value() default ""; }目前暫且認為這兩個可以互相替換
| 注解 | 可替換注解 |
| @Inject | @Autowired |
| @Named | @Component |
| @Component | @Service |
?
#--------------------------------------------------------------------------------------------------------
| 注解 | 大致用法 | 作用 |
| @GetMapping | 完整用法示例 | 路由函數 |
| @PathVariable | 完整用法示例 | 接收http請求中變量的值 |
| @SpringBootApplication | 完整用法示例 | 這個類中包含main |
| @RestController | 完整用法示例 | 等效於 @ResponseBody + @Controller |
| @ComponentScan? | 第4章-SpringBoot的注解/4.1/spring-ants/src/main/java/scan/ScanApp.java | 會自動掃描 @Controller @Service @Repository @Component @Configuration ? 一般是被包含在 @SpringBootApplication中如果web應用采用了@SpringBootConfiguration 那么此時就需要采用@ComponentScan? ? |
| @Primary | 第4章-SpringBoot的注解/4.1/spring-ants/src/main/java/primary/PrimaryConfig.java | 在配置類中有@Primary以后. main類中的注入可以隨便亂寫名字, 實際注入的以@Primary附屬的函數為準. |
| @Scope | @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)每次返回不同的對象(非單態) @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)每次返回同一個人對象(單態) | ①非單態的bean每一次返回的是一個新的實例 則這個單態bean所維護的非單態bean實例,將不會被刷新。 |
| @Configuration | ? | ? |
| @Component | ? | ? |
| @ConfigurationProperties | ? | ? |
| @Autowired | ? | 想要在一個bean中使用另一個 bean , 則可以直接在使用者一端,將被調用 者的 實例注入 |
| @WebListener | ? | ? |
| @WebServlet | ? | ? |
| @Qualifier | 4.2/ants-adv/src/main/java/qlf/ | 通過bean的類名進行注入 |
?
還加入了 ServletComponentScan
注解,這個注解主要用于掃描源碼包中的 Serviet 組件,包括使用@Web Servlet 、@WebFilter
和@WebListener 進行修飾的類。
?
?
?
?
?
測試類使用:
| 注解 | 作用 |
| @RunWith | ? |
| @SpringBootTest | ? |
| @AutoConfigureMockMvc | mock相關的自動配置 |
?
?
Feign注解
| 注解 | 作用 |
| @RequestLine | 發送http請求 |
| @Param | 接收參數 |
?
AOP注解
| 注解 | 作用 |
| @Aspect | 修飾代理類 |
| @Before | 設置通知 業務方法調用前執行 before 方法 |
| @After | 設置通知 業務方法調用后執行After方法 |
\
Reference:
[1]@Controller和@RestController的區別?
[2]何為Spring中的配置類?
[13]Spring中可以相互替換的注解
--------------------------下面是沒看的-------------------------------------------
[3]使用spring配置類代替xml配置文件注冊bean類
[4]簡單講講@SpringBootApplication
[5]@SpringBootApplication的使用
[6]使用spring配置類代替xml配置文件注冊bean類
[7]Spring常用注解匯總
[8]SpringBoot常用注解思維導圖
[9]Spring中的常見注解
[10]Spring常用注解
[11]Spring框架常用注解
[12]Spring系列之Spring常用注解總結
[13]SpringBoot常用注解思維導圖
總結
以上是生活随笔為你收集整理的spring的各种注解的作用-持续更新中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 地图服务 纬度、经度对应坐标轴x,y
- 下一篇: 调整xfce中的鼠标大小到48以上-目前