@ComponentScan和@MapperScan的区别
时间: 2023-08-10 14:53:44 浏览: 63
@ComponentScan和@MapperScan都是Spring框架中的注解,但是它们的作用不同。
@ComponentScan注解用于自动扫描并注册指定包及其子包下的所有组件,包括@Service、@Controller、@Repository和@Component等注解修饰的类。
@MapperScan注解用于自动扫描并注册指定包及其子包下的所有MyBatis的Mapper接口的实现类,即DAO层的实现类。这个注解是为了简化MyBatis的配置,将Mapper接口的实现类交由Spring容器管理。
因此,@ComponentScan和@MapperScan的作用不同,一个是用于扫描并注册组件,另一个是用于扫描并注册DAO层的实现类。
相关问题
@componentscan和@mapperscan区别
@componentscan和@mapperscan是Spring Boot中的注解。
@componentscan扫描并注册了被@Component、@Repository、@Service、@Controller、@Configuration等注解标识的类,将其放入Spring上下文中,方便其他组件进行调用。
@mapperscan是MyBatis框架中的注解,用于扫描Mapper接口所在的包,并将其注册到MyBatis中,让Mapper接口能够被注入到其他组件中进行调用。
区别在于,@componentscan主要是扫描和注册Spring的Bean,而@mapperscan主要是扫描和注册Mapper接口,两者作用不同,但都是为了提高组件的可用性。
@ComponentScan 和@MapperScan的区别
@ComponentScan 和 @MapperScan 都是 Spring Boot 中的注解,用于扫描指定的包并加载对应的 Bean。
@ComponentScan 注解表示要扫描指定的包及其子包,将其中标记为 @Component、@Service、@Repository、@Controller、@Configuration 等注解的类注册为 Spring 容器中的 Bean。
@MapperScan 注解则是 MyBatis-Spring 提供的注解,主要用于扫描 MyBatis 的 Mapper 接口,并将其注册为 Spring 容器中的 Bean。这个注解的作用相当于在 XML 配置文件中配置 Mapper 接口的扫描路径。
因此,@ComponentScan 和 @MapperScan 的区别在于扫描的目标不同。@ComponentScan 扫描的是 Spring 的组件,@MapperScan 扫描的是 MyBatis 的 Mapper 接口。