網(wǎng)站首頁 編程語言 正文
一.通過注解注入的一般形式
Bean類
public class TestBean{
}
Configuration類
@Configuration注解去標(biāo)記了該類,這樣標(biāo)明該類是一個Spring的一個配置類,在加載配置的時候會去加載他。
@Bean的注解,標(biāo)明這是一個注入Bean的方法,會將下面的返回的Bean注入IOC。
//創(chuàng)建一個class配置文件
@Configuration
public class TestConfiguration{
//將一個Bean交由Spring進(jìn)行管理
@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);
二.通過構(gòu)造方法注入Bean
我們在生成一個Bean實(shí)例的時候,可以使用Bean的構(gòu)造方法將Bean實(shí)現(xiàn)注入
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,默認(rèn)為類名小駝峰")
public class AnotherBean {
}
Configuration類
@Configuration
@ComponentScan("com.company.annotationbean")
public class TestConfiguration{
}
注解解釋
@AutoWired
自動裝配??
若是在這里注入的時候指定一個Bean的id就要使用@Qualifier注解。
注釋可以在 setter 方法中被用于自動連接 bean,就像 @Autowired 注釋,容器,一個屬性或者任意命名的可能帶有多個參數(shù)的方法。
還有不解的地方可以直接百度注解有更詳細(xì)的解釋跟實(shí)例
@Component(默認(rèn)單例模式)
@Component 被稱為元注釋,它是@Repository、@Service、@Controller、@Configuration的父類,理論上可以使用@Component來注釋任何需要Spring自動裝配的類。但每個注釋都有他們自己的作用,用來區(qū)分類的作用,Spring文檔上也說明后續(xù)版本中可能為@Repository、@Service、@Controller、@Configuration這些注釋添加其他功能,所以建議大家還是少使用@Component。
@Repository注解是任何滿足存儲庫角色或構(gòu)造型(也稱為數(shù)據(jù)訪問對象或DAO)的類的標(biāo)記。該標(biāo)記的用途之一是異常的自動翻譯,如異常翻譯中所述。
@Service注解一般使用在Service層。
@Controller注解一般使用在Controller層的類,@RestController繼承了@Controller。
@Configuration注解一般用來配置類,用來項(xiàng)目啟動時加載的類。
@ComponentScan("")
@ComponentScan注解一般用在Spring項(xiàng)目的核心配置類,或者在Spring Boot項(xiàng)目的啟動類里使用。作用就是用來掃描@Component及其子類注釋的類,用于Spring容器自動裝配。@ComponentScan默認(rèn)是掃描的路徑是同級路徑及同級路徑的子目錄,所以把Spring Boot的啟動類放在根目錄下,@ComponentScan是可以省略不寫的。
主要屬性:
- value屬性、basePackages屬性
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
這兩個值的作用是一樣的,是相互別名的關(guān)系。內(nèi)容是填寫要掃描的路徑,如果是有一個路徑,可以不用寫value,有幾種寫法如下:
@ComponentScan("com.zh.service")
@ComponentScan(value = "com.zh.service")
@ComponentScan(value = {"com.zh.dao", "com.zh.service"})
- includeFilters屬性、excludeFilters屬性
這兩個的作用都是過濾器,excludeFilters的作用是剔除values屬性內(nèi)的個別不需要加載的類,而includeFilters一般是和excludeFilters配合使用,就是被excludeFilters剔除的類里面,還需要其中的幾個類,就用includeFilters再加上。
舉個例子,假設(shè)送了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實(shí)現(xiàn)注入
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進(jìn)行了注入,對List中的元素會逐一注入。
下面我們換一種注入List方式
TestConfiguration類
@Bean
//通過該注解設(shè)定Bean注入的優(yōu)先級,不一定連續(xù)數(shù)字
@Order(34)
public String string1(){
return "String-1";
}
@Bean
@Order(14)
public String string2(){
return "String-2";
}
注入與List中泛型一樣的類型,會自動去匹配類型,及時這里沒有任何List的感覺,只是String的類型,但他會去通過List的Bean的方式去注入。
注意:
第二種方式的優(yōu)先級高于第一種,當(dāng)兩個都存在的時候,若要強(qiáng)制去使用第一種方式,則要去指定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一樣,第二種優(yōu)先級大于第一種
原文鏈接:https://blog.csdn.net/A_awen/article/details/123224794
相關(guān)推薦
- 2022-04-26 使用jquery庫實(shí)現(xiàn)電梯導(dǎo)航效果_jquery
- 2023-06-19 圖文詳解Go中的channel_Golang
- 2022-09-09 詳解C語言中動態(tài)內(nèi)存管理及柔性數(shù)組的使用_C 語言
- 2023-02-14 教你使用SQL語句進(jìn)行數(shù)據(jù)庫復(fù)雜查詢_MsSql
- 2022-03-26 android獲取及監(jiān)聽手機(jī)網(wǎng)絡(luò)狀態(tài)_Android
- 2022-10-17 C++模擬實(shí)現(xiàn)vector的示例代碼_C 語言
- 2023-07-05 React hooks之useEffect、useMemo優(yōu)化技巧
- 2023-07-02 Python+streamlit實(shí)現(xiàn)輕松創(chuàng)建人事系統(tǒng)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支