網站首頁 編程語言 正文
一.通過注解注入的一般形式
Bean類
public class TestBean{
}
Configuration類
@Configuration注解去標記了該類,這樣標明該類是一個Spring的一個配置類,在加載配置的時候會去加載他。
@Bean的注解,標明這是一個注入Bean的方法,會將下面的返回的Bean注入IOC。
//創建一個class配置文件
@Configuration
public class TestConfiguration{
//將一個Bean交由Spring進行管理
@Bean
public TestBean myBean(){
return new TestBean();
}
}
測試類
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean = cotext.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);
二.通過構造方法注入Bean
我們在生成一個Bean實例的時候,可以使用Bean的構造方法將Bean實現注入
Bean類
@Component
public class TestBeanConstructor {
private AnotherBean anotherBeanConstructor;
@Autowired
public TeanBeanConstructor(AnotherBean anotherBeanConstructor){
this.anotherBeanConstructor = anotherBeanConstructor;
}
@Override
public String toString() {
return "TeanBean{" +
"anotherBeanConstructor=" + anotherBeanConstructor +
'}';
}
}
AnotherBean類
@Component(value="Bean的id,默認為類名小駝峰")
public class AnotherBean {
}
Configuration類
@Configuration
@ComponentScan("com.company.annotationbean")
public class TestConfiguration{
}
注解解釋
@AutoWired
自動裝配?
若是在這里注入的時候指定一個Bean的id就要使用@Qualifier注解。
注釋可以在 setter 方法中被用于自動連接 bean,就像 @Autowired 注釋,容器,一個屬性或者任意命名的可能帶有多個參數的方法。
還有不解的地方可以直接百度注解有更詳細的解釋跟實例
@Component(默認單例模式)
@Component 被稱為元注釋,它是@Repository、@Service、@Controller、@Configuration的父類,理論上可以使用@Component來注釋任何需要Spring自動裝配的類。但每個注釋都有他們自己的作用,用來區分類的作用,Spring文檔上也說明后續版本中可能為@Repository、@Service、@Controller、@Configuration這些注釋添加其他功能,所以建議大家還是少使用@Component。
@Repository注解是任何滿足存儲庫角色或構造型(也稱為數據訪問對象或DAO)的類的標記。該標記的用途之一是異常的自動翻譯,如異常翻譯中所述。
@Service注解一般使用在Service層。
@Controller注解一般使用在Controller層的類,@RestController繼承了@Controller。
@Configuration注解一般用來配置類,用來項目啟動時加載的類。
@ComponentScan("")
@ComponentScan注解一般用在Spring項目的核心配置類,或者在Spring Boot項目的啟動類里使用。作用就是用來掃描@Component及其子類注釋的類,用于Spring容器自動裝配。@ComponentScan默認是掃描的路徑是同級路徑及同級路徑的子目錄,所以把Spring Boot的啟動類放在根目錄下,@ComponentScan是可以省略不寫的。
主要屬性:
- value屬性、basePackages屬性
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
這兩個值的作用是一樣的,是相互別名的關系。內容是填寫要掃描的路徑,如果是有一個路徑,可以不用寫value,有幾種寫法如下:
@ComponentScan("com.zh.service")
@ComponentScan(value = "com.zh.service")
@ComponentScan(value = {"com.zh.dao", "com.zh.service"})
- includeFilters屬性、excludeFilters屬性
這兩個的作用都是過濾器,excludeFilters的作用是剔除values屬性內的個別不需要加載的類,而includeFilters一般是和excludeFilters配合使用,就是被excludeFilters剔除的類里面,還需要其中的幾個類,就用includeFilters再加上。
舉個例子,假設送了excludeFilters剔除了所有注解是Repository的類,但其中一個Stub開頭的類,還要用到,就可以按下面的例子這樣寫。
ComponentScan.Filter[] includeFilters() default {};
ComponentScan.Filter[] excludeFilters() default {};
Filter作為過濾器的基本對象
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Filter {
FilterType type() default FilterType.ANNOTATION;
@AliasFor("classes")
Class<?>[] value() default {};
@AliasFor("value")
Class<?>[] classes() default {};
String[] pattern() default {};
}
FilterType是過濾器的類型,定義方式有幾種
public enum FilterType {
ANNOTATION,
ASSIGNABLE_TYPE,
ASPECTJ,
REGEX,
CUSTOM;
private FilterType() {
}
}
三.通過set方法注入Bean
我們可以在一個屬性的set方法中去將Bean實現注入
Bean類
@Component
public class TestBeanSet {
private AnotherBean anotherBeanSet;
@Autowired
public void setAnotherBeanSet(AnotherBean anotherBeanSet) {
this.anotherBeanSet = anotherBeanSet;
}
@Override
public String toString() {
return "TestBeanSet{" +
"anotherBeanSet=" + anotherBeanSet +
'}';
}
}
Configuration類
@Configuration
@ComponentScan("com.company.annotationbean")
public class TestConfiguration{
}
Test類
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean = cotext.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);
四.通過屬性去注入Bean
@Component
public class TestBeanProperty {
@Autowired
private AnotherBean anotherBeanProperty;
@Override
public String toString() {
return "TestBeanProperty{" +
"anotherBeanProperty=" + anotherBeanProperty +
'}';
}
}
這里可以通過@AutoWired去自動裝配它。
五.通過List注入Bean
TestBeanList類
@Component
public class TestBeanList {
private List<String> stringList;
@Autowired
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
public List<String> getStringList() {
return stringList;
}
}
TestConfiguration類()配置類
@Configuration
@ComponentScan("annoBean.annotationbean")
public class TestConfiguration {
@Bean
public List<String> stringList(){
List<String> stringList = new ArrayList<String>();
stringList.add("List-1");
stringList.add("List-2");
return stringList;
}
}
這里我們將TestBeanList進行了注入,對List中的元素會逐一注入。
下面我們換一種注入List方式
TestConfiguration類
@Bean
//通過該注解設定Bean注入的優先級,不一定連續數字
@Order(34)
public String string1(){
return "String-1";
}
@Bean
@Order(14)
public String string2(){
return "String-2";
}
注入與List中泛型一樣的類型,會自動去匹配類型,及時這里沒有任何List的感覺,只是String的類型,但他會去通過List的Bean的方式去注入。
注意:
第二種方式的優先級高于第一種,當兩個都存在的時候,若要強制去使用第一種方式,則要去指定Bean的id即可。
六.通過Map去注入Bean
@Component
public class TestBeanMap {
private Map<String,Integer> integerMap;
public Map<String, Integer> getIntegerMap() {
return integerMap;
}
@Autowired
public void setIntegerMap(Map<String, Integer> integerMap) {
this.integerMap = integerMap;
}
}
/**
*第一種注入map方式
*/
@Bean
public Map<String,Integer> integerMap(){
Map<String,Integer> integerMap = new HashMap<String, Integer>();
integerMap.put("map-1",1);
integerMap.put("map-2",2);
return integerMap;
}
/**
*第二種注入map方式
*/
@Bean
public Integer integer1(){
return 1;
}
@Bean
public Integer integer2(){
return 2;
}
這里跟List一樣,第二種優先級大于第一種
原文鏈接:https://blog.csdn.net/A_awen/article/details/123224794
相關推薦
- 2022-09-09 Go語言提升開發效率的語法糖技巧分享_Golang
- 2022-08-21 使用?DataAnt?監控?Apache?APISIX的原理解析_Linux
- 2023-01-03 利用Rust實現一個簡單的Ping應用_Rust語言
- 2022-03-17 .NET?Core使用C#掃描并讀取圖片中的文字_C#教程
- 2022-10-30 go語言reflect.Type?和?reflect.Value?應用示例詳解_Golang
- 2022-09-17 C++中cin的返回值問題_C 語言
- 2022-04-29 C++圖解單向鏈表類模板和iterator迭代器類模版詳解_C 語言
- 2022-09-04 C/C++?引用作為函數的返回值方式_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支