Spring中的@Repository注解源碼詳解
@Repository注解
不多廢話,直接看源碼
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Repository { @AliasFor( annotation = Component.class ) String value() default ""; }
分析如下
- @Component代表@Repository可以把一個類組件加入到IOC容器中
- @Target,target注解決定MyAnnotation注解可以加在哪些成分上,如加```xml
ElementType.TYPE // 作用在類身上 ElementType.Filed) //作用到屬性身上 ElementType.METHOD //作用到方法身上
- @Retention注解決定MyAnnotation注解的生命周期
生命周期長度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用
- source:注解只保留在源文件,當Java文件編譯成class文件的時候,注解被遺棄;被編譯器忽略,如果只是做一些檢查性的操作,比如 @Override 和 @SuppressWarnings,則可選用 SOURCE 注解
- class:注解被保留到class文件,但jvm加載class文件時候被遺棄,這是默認的生命周期,如果要在編譯時進行一些預(yù)處理操作,比如生成一些輔助代碼(如 ButterKnife),就用 CLASS注解
- runtime:注解不僅被保存到class文件中,jvm加載class文件之后,仍然存在,如果需要在運行時去動態(tài)獲取注解信息,那只能用 RUNTIME 注解
這3個生命周期分別對應(yīng)于:Java源文件(.java文件) ---> .class文件 ---> 內(nèi)存中的字節(jié)碼。
此時我們差不多把該注解剖析完了,該注解還有一個字段value,value其實是在java程序動態(tài)運行時去告訴Spring創(chuàng)建一個名字為xxx的組件實例,比如
@Repository(value="userServiceNew") public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; public User Sel(int id){ return userMapper.Sel(id); } }
該注解是告訴Spring,讓Spring創(chuàng)建一個名字叫“userServiceNew的UserServiceImpl實例。當Service需要使用Spring創(chuàng)建的名字叫“userServiceNew”的UserServiceImpl實例時,就可以使用@Resource(name = “UserServiceNew”)注解告訴Spring,Spring把創(chuàng)建好的UserServiceImpl注入給Service即可。
@Repository(value="userServiceNew") public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; public User Sel(int id){ return userMapper.Sel(id); } }
案例如下
@RestController @RequestMapping("/testBoot") public class UserController { @Resource(name = "userServiceNew") private UserService userService; @RequestMapping("getUser/{id}") public Object GetUser(@PathVariable int id){ return userService.Sel(id); } }
@Autowired注解和@Resource區(qū)別
作用范圍不相同(field,setter,constructor,method’s param)
- @Autowired作用域:@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
- @Resource作用域:@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
策略不同,前者默認按類型操作,如果找到多個再按組件名字查找,或者通過@Qualifier判斷,有@Qualifier修飾那么直接按后者默認按名字查找,即使沒有指定名字也會安裝注解作用的對象名來匹配,按默認組件名沒有查找到再按類型查找
如果我另一個包出現(xiàn)了同名的類
@Service public class UserServiceNew { }
啟動springboot時就會報錯:
Failed to parse configuration class [com.sobot.demo7.Demo7Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name ‘userServiceNew’ for bean class [com.sobot.demo7.service.UserServiceNew] conflicts with existing, non-compatible bean definition of same name and class [com.sobot.demo7.service.UserServiceImpl]
SpringMVC的Controller 應(yīng)該是采用類似鍵值對(key/value)的映射方式處理的。而當中的鍵,默認是用cotroller的類名(非全類名)作為鍵。這樣,如果不同包下面的兩個Contoller 重名的話,就會導致SpringMVC的容器管理中的controller map中的key重復(fù)了。所以我們可以通過重命名來解決這個問題,比如
@Service(value = "userSerivce") public class UserServiceNew { }
到此這篇關(guān)于Spring中的@Repository注解源碼詳解的文章就介紹到這了,更多相關(guān)@Repository注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven項目如何在pom文件中引入lib下的第三方j(luò)ar包并打包進去
在使用Maven進行項目開發(fā)時,引入第三方私有的Jar包可能會遇到問題,一種常見的解決方案是將Jar包添加到項目的lib目錄,并通過IDE進行配置,但這需要每個開發(fā)者單獨操作,效率低下,更好的方法是通過Maven的pom.xml文件管理這些Jar包2024-09-09Mybatis中TypeAliasRegistry的作用及使用方法
Mybatis中的TypeAliasRegistry是一個類型別名注冊表,它的作用是為Java類型建立別名,使得在Mybatis配置文件中可以使用別名來代替完整的Java類型名。使用TypeAliasRegistry可以簡化Mybatis配置文件的編寫,提高配置文件的可讀性和可維護性2023-05-05詳解SpringMVC實現(xiàn)圖片上傳以及該注意的小細節(jié)
本篇文章主要介紹了詳解SpringMVC實現(xiàn)圖片上傳以及該注意的小細節(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02運行Jar包出現(xiàn)提示xxx中沒有主清單屬性報錯問題解決方法
這篇文章主要介紹了運行Jar包出現(xiàn):xxx中沒有主清單屬性報錯,當出現(xiàn)報錯:xxx中沒有主清單屬性,解決方法也很簡單,在pom.xml配置中,加上相應(yīng)配置即可,需要的朋友可以參考下2023-08-08