Spring使用注解存儲Bean對象的方法詳解
通過配置文件注冊對象從而存儲到 Spring 中,這種方式其實還是挺繁瑣的。實際上,在使用學習使用Spring過程中,當我們要實現一個功能的時候,先應該考慮的是有沒有相應的注解是實現對應功能的,Spring 中很多功能的配置都是可以依靠注解實現的,而本篇中介紹的是使用注解來存儲 Bean 對象。
一. 配置掃描路徑
首先還是要創(chuàng)建 Spring 項目,這里有問題還是去看我上一篇博客。當創(chuàng)建好項目后,我們的第一步就是配置掃描路徑,這一步驟非常關鍵的,這里錯了,之后的的操作就都不會生效了。
我們在resources
目錄下創(chuàng)建一個spring-config.xml
配置文件,用來設置掃描的路徑,在配置文件中添加如下內容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:content="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <content:component-scan base-package=""></content:component-scan> </beans>
其中<content:component-scan base-package=""></content:component-scan>
里面 base-package
的值設置為你需要掃描對象的根路徑,這個路徑從java
目錄開始,比如我在如圖中的com.tr.demo
目錄下創(chuàng)建類:
那么這個配置文件中根路徑就為com.tr.demo
,所以我們將base-package
的值設置為com.tr.demo
。
<content:component-scan base-package="com.tr.demo"></content:component-scan>
二. 使用注解儲存Bean對象
想要使用注解,那得先知道能使用哪些注解,在 Spring 中有五大類注解和方法注解,分別為:
- 五大類注解:@Controller(控制器)、@Service(服務)、@Repository(倉庫)、@Component(組件)、@Configuration(配置)。
- 方法注解:@Bean。
1. 使用五大類注解儲存Bean
首先,我們來了解如何使用五大類注解來儲存對象,先以@Controller
注解為例,我們有如下的代碼:
package com.tr.demo; import org.springframework.stereotype.Controller; @Controller public class UserController { public void sayHi() { System.out.println("Hi, UserController~"); } }
像這樣在掃描路徑下創(chuàng)建類,并在類上加上@Controller
注解就將Bean
存儲到容器當中了。
接下來就要從 Spring 中讀取出我們的對象,這里還是先使用依賴查找的方式來獲取 Bean,使用五大類注解,默認情況下,Bean 的名字就是原類名首字母小寫(小駝峰)。
import com.tr.demo.UserController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); //獲取對象時使用類名的小駝峰形式作為 name 參數 UserController userController = context.getBean("userController", UserController.class); userController.sayHi(); } }
運行結果:
要注意,是使用了五大類注解創(chuàng)建的類且類必須要在前面我們配置的掃描路徑下(包括子包)才能將 Bean 存儲到 Spring 當中,否則是無效的,所以這個掃描路徑也叫做根路徑。
設置根路徑其實也是為了提高程序的性能,因為如果不設置根路徑,Spring 就會掃描項目文件中所有的目錄,但并不是所有類都需要儲存到 Spring當中,這樣性能就會比較低,設置了根路徑,Spring 就只掃描該根路徑下所有的目錄就可以了,提高了程序的性能。
上面只使用了 @Controller,那么我們再來驗證一下其他四個注解可不可以達到同樣的目的,同時為了驗證上面的結論,我們在com.tr.demo
目錄下再創(chuàng)建一個inner
目錄,在根路徑外在創(chuàng)建一個類Student
使用類注解。
package com.tr.demo.inner; import org.springframework.stereotype.Component; @Component public class UserComponent { public void sayHi() { System.out.println("Hi, UserComponent~"); } } package com.tr.demo.inner; import org.springframework.context.annotation.Configuration; @Configuration public class UserConfiguration { public void sayHi() { System.out.println("Hi, UserConfiguration~"); } } package com.tr.demo.inner; import org.springframework.stereotype.Repository; @Repository public class UserRepository { public void sayHi() { System.out.println("Hi, UserRepository~"); } } package com.tr.demo.inner; import org.springframework.stereotype.Service; @Service public class UserService { public void sayHi() { System.out.println("Hi, UserService~"); } } import com.tr.demo.UserController; import com.tr.demo.inner.UserComponent; import com.tr.demo.inner.UserConfiguration; import com.tr.demo.inner.UserRepository; import com.tr.demo.inner.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); //獲取對象時使用類名的小駝峰形式作為 name 參數 UserController userController = context.getBean("userController", UserController.class); userController.sayHi(); UserService service = context.getBean("userService", UserService.class); service.sayHi(); UserConfiguration configuration = context.getBean("userConfiguration", UserConfiguration.class); configuration.sayHi(); UserComponent component = context.getBean("userComponent", UserComponent.class); component.sayHi(); UserRepository repository = context.getBean("userRepository", UserRepository.class); repository.sayHi(); } }
運行結果:
五大類注解效果都是一樣的,而不在根路徑下的Student
是無效的。
還需要知道的是使用注解存儲的 Bean 和使用XML
存儲的的 Bean 是可以一同使用的,比如我們將將剛剛有問題的Student
重新通過XML
的方式進行存儲。
運行結果:
2. 為什么要有五大類注解?
既然都五大類完成的是同樣的工作,那為什么要有五大類注解呢?
其實五大類注解主要是為了規(guī)范 Java 項目的代碼,Java 項目的標準分層如下:
- 控制層(Controller)
- 服務層(Service)
- 數據持久層(Dao)
而五大類注解便是對應著不同的層級別使用的,讓程序猿看到某一個注解就可以明確這個了類是做什么的。
- @Controller:控制器,校驗用戶請求數據的正確性(安保系統);直接和前端打交道,校驗前端發(fā)來請求是參數和合法性。
- @Service:服務,編排和調度具體執(zhí)行方法的(客服中心);不會直接操作數據庫,根據請求判斷具體調用哪個方法。
- @Repository:數據持久層,直接和數據庫交互(實際業(yè)務的執(zhí)行),也叫DAO層(data access object)。
- @Component:組件(工具類層),為整個項目存放一些需要使用的組件,但又和其他層沒有什么實際交互。
- @Configuration 配置項(項目中的一些配置)。
包括企業(yè)中也是按照這樣的結構來將項目分層的,典型的比如阿里,它只是在標準分層在服務層(Service)做了一個擴展,劃分的更加細致詳細了。
五大類注解主要起到的是“見名知意”的作用,代碼層面上來看,作用是類似的,我們去查看五大類類注解的源碼看一看。
可以看到五大類的源碼中除了 @Component 以外,其他四大類注解中都包含了 @Component 注解的功能,這四大類注解都是基于 @Component 實現的,是 @Component 拓展。
3.有關獲取Bean參數的命名規(guī)則
上文中在使用依賴查找的方式獲取Bean
時,getBean
方法的BeanName
是使用類名的小駝峰形式(即類名的首字母小寫),這是因為使用注解儲存對象時,默認會將類名的小駝峰形式設置為 Bean 的名字,但并不是完全依照這個規(guī)則的,是有特殊情況的。
比如,我們創(chuàng)建一個類,將它的前兩個字母大寫,如UConfig
,此時來看使用類名的小駝峰形式還能不能獲取到 Bean。
package com.tr.demo; import org.springframework.stereotype.Repository; @Repository public class UConfig { public void sayHi(){ System.out.println("Hi, UConfig~"); } }
啟動類
import com.tr.demo.UConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP2 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UConfig uConfig= context.getBean("uConfig", UConfig.class); uConfig.sayHi(); } }
運行結果:
可以看到程序報錯了,說沒有找到beanName
為uConfig
的對象,那此時的beanName
是什么呢?
此時再來試一下原本的類名(大駝峰),看能不能獲?。?/p>
UConfig uConfig= context.getBean("UConfig", UConfig.class);
運行結果:
此時就獲取到了,好像多少有點玄學在里面,我們來翻一翻源碼,看一看這是什么原因。
雙擊Shift
進行全局搜索,上面是根于對象名稱來找到對象的,所以我們輸入beanName
,試著搜索一下:
我們會發(fā)現有AnnotationBeanNameGenerator
類與BeanNameGenerator
接口,那我們就試著點到AnnotationBeanNameGenerator
類源碼看一看。
正常點開后看到的應該是 IDEA 將.class
文件反編譯出來的代碼,缺少注釋和明確的變量命名。
我們點擊Download Sources
將 Spring 源碼下載下來即可,此時我們在在源碼中就能看到下面的方法,看名字也知道,用來建立默認的BeanName
的。
返回值是Introspector.decapitalize
方法的返回值,再點進去看看這個方法。
此時我們就能分析得出結論,如果類名長度大于1
并且滿足第一個與第二個字母為大寫,則構造的BeanName
就為原類名,其他正常情況為類名的小駝峰形式,這就解釋了UConfig
類的BeanName
為什么是原類名了。
而且我們會發(fā)現這個方法所在類是來自于jdk
的。
所以,BeanName
的規(guī)范命名規(guī)則并不是 Spring 獨創(chuàng)的,而依照 Java 標準庫的規(guī)則進行的。
- 如果類名不存在或類名為空字符串,
BeanName
為原類名。 - 如果類名字長度大于1,且第一個與第二個字符為大寫,
BeanName
為原類名。 - 其他情況,
BeanName
為原類名的小駝峰形式。
三. 使用方法注解儲存Bean對象
1. 方法注解儲存對象的用法
五大類注解是添加到某個類上的,而方法注解是放到方法上的,當一個方法返回的是一個具體的實例對象時,我們就可以使用方法注解@Bean
來將對象儲存到 Spring,但是單單使用一個@Bean
是不能夠成功儲存對象的,還需要在方法所在類上使用五大類注解才行,比如搭配一 個@Component 注解,方法注解是不能夠單獨使用的,@Bean
注解必須要搭配五大類注解一起使用(Spring為了提升性能所做的規(guī)定,畢竟造方法的成本太低了,不能去掃描整個項目的方法吧)。
還是要注意使用必須是在根路徑下。
比如我們有一個普通文章的實體類ArticleInfo
package com.tr.demo.model; import java.time.LocalDateTime; /** * 普通的文章實體類 */ public class ArticleInfo { private int aid; private LocalDateTime createtime; private String title; private String author; private String content; public void setAid(int aid) { this.aid = aid; } public void setCreatetime(LocalDateTime createtime) { this.createtime = createtime; } public void setTitle(String title) { this.title = title; } public void setAuthor(String author) { this.author = author; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "ArticleInfo{" + "aid=" + aid + ", createtime=" + createtime + "\n" + ", title='" + title + '\'' + ", author='" + author + '\'' + "\n" + ", content='" + content + '\'' + '}'; } }
下面演示使用@Bean
方法注解儲存對象
package com.tr.demo; import com.tr.demo.model.ArticleInfo; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Controller; import java.time.LocalDateTime; @Controller public class Articles { @Bean// 將當前方法返回的對象存儲到 IoC 容器 public ArticleInfo getArt(){ // 偽代碼(實際上這里的 Bean 不是 new 出來的) ArticleInfo articleInfo = new ArticleInfo(); articleInfo.setAid(1); articleInfo.setCreatetime(LocalDateTime.now()); articleInfo.setTitle("夏日絕句"); articleInfo.setAuthor("李清照"); articleInfo.setContent("生當做人杰,死亦為鬼雄。至今思項羽,不肯過江東。"); return articleInfo; } public void sayHi(){ System.out.println("Hi, Articles~"); } }
獲取方法注解儲存的對象時,傳入的BeanName
參數值默認值就是方法名,我上面的代碼中方法名為getArt
,所以獲取時,就使用getArt
作為參數來進行獲取。
import com.tr.demo.model.ArticleInfo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP3 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); ArticleInfo article = context.getBean("getArt", ArticleInfo.class); System.out.println(article); } }
運行結果:
2. @Bean的重命名
獲取方法注解儲存的對象時,傳入的BeanName
參數值默值為方法名,但像上面那樣返回對象的方法名稱往往是getXXX
這樣式取名的,雖然在語法與實現上是沒有問題的,但實際開發(fā)寫出這樣的代碼,看起來還是比較別扭的。
實際上注解 @Bean 是可以加參數的,給儲存的對象起別名,像下面這個樣子。
@Controller public class Articles { @Bean("article")// 將當前方法返回的對象存儲到 IoC 容器 public ArticleInfo getArt(){ // 偽代碼(實際上這里的 Bean 不是 new 出來的) ArticleInfo articleInfo = new ArticleInfo(); articleInfo.setAid(1); articleInfo.setCreatetime(LocalDateTime.now()); articleInfo.setTitle("夏日絕句"); articleInfo.setAuthor("李清照"); articleInfo.setContent("生當做人杰,死亦為鬼雄。至今思項羽,不肯過江東。"); return articleInfo; } public void sayHi(){ System.out.println("Hi, Articles~"); } }
也可以給 Bean 設置多個別名,總結起來有如下幾種方式:
//方式一(省略參數名的情況下默認是name) @Bean("article1") //方式二 @Bean(name = "article2") //方式三 @Bean(value = "article3") //起多個別名 @Bean(name = {"article4", "article5"}) @Bean(value = {"article6", "article7"}) @Bean({"article8", "article9", "article10"})
我們按照第 9 行的方式設置,此時獲取方法注解儲存的對象就能夠使用別名來進行獲取。
import com.tr.demo.model.ArticleInfo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP4 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); ArticleInfo article1 = context.getBean("article6", ArticleInfo.class); System.out.println(article1); System.out.println("-----------------------------------------------------"); ArticleInfo article2 = context.getBean("article7", ArticleInfo.class); System.out.println(article2); System.out.println("-----------------------------------------------------"); } }
運行結果:
再想一下,當一個 Bean 有別名了,那使用之前那個方法名還能夠獲取到對象嗎?嘗試一下:
import com.tr.demo.model.ArticleInfo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class APP5 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); ArticleInfo article = context.getBean("getArt", ArticleInfo.class); System.out.println(article); } }
運行結果:
此時就能發(fā)現是獲取不到的
所以使用 @Bean 存儲對象的beanName
命名規(guī)則是,當沒有設置name/value
屬性時,此時 Bean 的默認名字就是方法名,一旦添加了別名name/value
屬性后,就只能通過重命名的別名來獲取 Bean 了,默認的使用方法名獲取 Bean 對象就不能使用了。
還要簡單注意一下,@Bean 使用時,同一類如果多個 Bean 使用相同的名稱,此時程序執(zhí)行是不會報錯的,他會根據類加載順序和類中代碼從上至下的的順序,將第一個 Bean 存放到 Spring 中,但第一個之后的對象就不會被存放到容器中了,也就是只有在第一次創(chuàng)建 Bean 的時候會將對象和 Bean 名稱關聯起來,后續(xù)再有相同名稱的Bean存儲時候,容器會自動忽略。
還可以通過類注解 @Order
注解控制類加載順序(值越小,優(yōu)先級越高),進而影響 Bean 的存放的先后順序,這些也比較簡單,就不做演示了。
以上就是Spring使用注解存儲Bean對象的方法詳解的詳細內容,更多關于Spring注解存儲Bean的資料請關注腳本之家其它相關文章!
相關文章
火遍全網的Hutool使用Builder模式創(chuàng)建線程池的方法
這篇文章主要介紹了火遍全網的Hutool使用Builder模式創(chuàng)建線程池的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03