SpringBoot中的@Configuration、@MapperScan注解
SpringBoot中的@Configuration、@MapperScan注解
1、@Configuration注解
- @Configuration注解用于標記一個類為配置類,表示該類包含一個或多個@Bean方法,這些方法返回的實例會被Spring容器管理。
- 配置類替代了傳統(tǒng)的XML配置文件,使得配置更加簡潔和類型安全
使用方式:
- 在需要定義Bean的類上添加@Configuration注解
- 在類中使用@Bean注解的方法來定義和初始化Bean
示例:
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }
使用場景:
- 當需要定義和管理多個Bean時,使用@Configuration注解的類來集中管理這些Bean。
- 替代XML配置文件,使配置更加簡潔和易于維護。
底層原理:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { @AliasFor( annotation = Component.class ) String value() default ""; boolean proxyBeanMethods() default true; }
- @Configuration注解的類會被Spring容器識別,并且類中的@Bean方法會調用,返回的實例會被注冊到Spring容器中。
- @Configuration類會被編譯成一個代理類,確保@Bean方法的調用是線程安全的,并且可以支持方法間的依賴注入。
@Configuration
注解的底層原理:
Spring 容器啟動:Spring 容器啟動時,會掃描帶有 @Configuration
注解的類。
- 解析
@Configuration
類:創(chuàng)建并注冊配置類實例到容器。 - 解析
@Configuration
類中的@Bean
方法:創(chuàng)建并注冊@Bean
方法定義的 Bean 到容器。 - 初始化 Configuration 類中的 Bean:初始化配置類中的 Bean。
- 配置完成:配置完成。
2、@MapperScan注解
- @MapperScan注解用于掃描指定包下的所有Mapper接口,并將它們注冊為Spring管理的Bean
- 常用與Mybatis框架中,自動掃描并注冊Mapper接口,避免手動在每個Mapper接口中添加@Mapper注解
使用方式:
在配置類上添加@MapperScan注解,并指定需要掃描的包路徑。
示例:
@Configuration @MapperScan("com.briup.*.mapper") public class MyBatisConfig { // 其他配置 }
使用場景:
- 當項目中有多個Mapper接口時,使用@MapperScan注解可以一次性掃描并注冊所有的Mapper接口,避免在每個接口上手動添加@Mapper注解。
- 適用于使用Mybatis框架的項目,簡化Mapper接口的管理。
底層原理:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({MapperScannerRegistrar.class}) @Repeatable(MapperScans.class) public @interface MapperScan { String[] value() default {}; String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; Class<? extends Annotation> annotationClass() default Annotation.class; Class<?> markerInterface() default Class.class; String sqlSessionTemplateRef() default ""; String sqlSessionFactoryRef() default ""; Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class; String lazyInitialization() default ""; String defaultScope() default ""; }
- @MapperScan注解通過ClassPathMapperScanner類來掃描指定包下的所有接口。
- @ClassPathMapperScanner會查找所有帶有@Mapper注解的接口,并將它們注冊為Spring管理的Bean
注冊的Bean會配置為Mybatis的SqlSessionTemplate或SqlSessionDaoSupport的代理對象,從而可以在業(yè)務邏輯中直接使用這些Mapper接口。
@MapperScan
注解的底層原理:
- Spring 容器啟動:Spring 容器啟動時,會掃描帶有
@MapperScan
注解的類。 - 解析
@MapperScan
注解:掃描指定包下的 Mapper 接口。 - 創(chuàng)建并注冊 Mapper 接口的實現(xiàn)類到容器:創(chuàng)建并注冊 Mapper 接口的實現(xiàn)類到容器。
- 初始化 Mapper 實現(xiàn)類:初始化 Mapper 實現(xiàn)類。
- 配置完成:配置完成。
總結:
@Configuration
注解:
- 功能:標記一個類為配置類,包含
@Bean
方法。 - 使用方式:在類上添加
@Configuration
注解,類中使用@Bean
注解的方法定義 Bean。 - 使用場景:集中管理多個 Bean,替代 XML 配置文件。
- 底層原理:類被編譯成代理類,
@Bean
方法被調用,返回的實例注冊到 Spring 容器中。
@MapperScan
注解:
- 功能:掃描指定包下的所有 Mapper 接口,并注冊為 Spring 管理的 Bean。
- 使用方式:在配置類上添加
@MapperScan
注解,指定需要掃描的包路徑。 - 使用場景:簡化 MyBatis 框架中 Mapper 接口的管理,避免手動添加
@Mapper
注解。 - 底層原理:通過
ClassPathMapperScanner
類掃描接口并注冊為 Spring 管理的 Bean。
到此這篇關于SpringBoot中的@Configuration、@MapperScan注解的文章就介紹到這了,更多相關SpringBoot中的@Configuration、@MapperScan注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于ApplicationContext的三個常用實現(xiàn)類
這篇文章主要介紹了關于ApplicationContext的三個常用實現(xiàn)類,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Spring如何消除代碼中的if-else/switch-case
這篇文章主要給大家介紹了關于Spring如何消除代碼中if-else/switch-case的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04詳解maven的setting配置文件中mirror和repository的區(qū)別
這篇文章主要介紹了詳解maven的setting配置文件中mirror和repository的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12