spring 常用註解
用於創建對象
Component
把資源讓 spring 來管理。相當於在 xml 中配置一個 bean。
Controller:用於表現層
Service:用於業務層
Repository:用於持久層
用於注入數據 註解不能注入集合類型
Autowried 自動按照類型注入,只能注入bean類型 (不常用)
Qualifier 在自動注入的基礎上,按照id注入,只能與 Autowried 搭配使用
Resource 直接按照 id 注入(常用)可能需要導包
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
Value 注入基本類型和 String
用於改變作用範圍
Scope
屬性:
singleton 單例
prototype 多例
單例多例參見 https://blog.csdn.net/qq_38009970/article/details/104528189
與 xml 配置的比較
完全使用註解方式,不需要xml :創建配置類
使用註解
加在配置類上
Configuration:指定當前類是一個 spring 的配置類
ComponentScan:指定 spring 在初始化容器時需要掃描的包
PropertySource:用於加載.properties 文件中的配置
Import:用於導入其他配置類
加在方法上
Bean:表明使用此方法創建一個對象,並且放入 spring 容器。
整合junit