springboot中的@configuration、@mapperscan注解
1、@configuration注解
- @configuration注解用于标记一个类为配置类,表示该类包含一个或多个@bean方法,这些方法返回的实例会被spring容器管理。
- 配置类替代了传统的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类:创建并注册配置类实例到容器。 - 解析
@configuration类中的@bean方法:创建并注册@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的代理对象,从而可以在业务逻辑中直接使用这些mapper接口。
@mapperscan 注解的底层原理:
- spring 容器启动:spring 容器启动时,会扫描带有
@mapperscan注解的类。 - 解析
@mapperscan注解:扫描指定包下的 mapper 接口。 - 创建并注册 mapper 接口的实现类到容器:创建并注册 mapper 接口的实现类到容器。
- 初始化 mapper 实现类:初始化 mapper 实现类。
- 配置完成:配置完成。
总结:
@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注解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论