原文网址:SpringBoot注解--@SpringBootApplication/@MapperScan/自定义启动类注解_IT利刃出鞘的博客-CSDN博客

简介

本文介绍SpringBoot启动类常用的几个注解,以及自定义启动类注解的方法。

@SpringBootApplication

scanBasePackages

示例:

@SpringBootApplication(scanBasePackages="com.test.chapter4")

    此basePackages其实是@ComponentScan中的,通过查看scanBasePackages即可得知。

    @SpringBootApplication只会扫描@SpringBootApplication注解标记类包下及其子包的类,将这些类纳入到spring容器,只要类有@Component注解即可。

    有的注解的定义中已加入@Component,所以这些注解也会被扫描到:@Controller,@Service,@Configuration

@MapperScan

作用:定义扫描MyBatis接口。

SpringBoot有个bug:在application.properties中指定Mapper是无效的:mybatis.mapper-locations=classpath:com.springboot.chapter15/mapper/*.xml

示例:(对于Mybatis)

package com.example.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan(annotationClass = Mapper.class, basePackages = "com.springboot.chapter15.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

示例:(对于Mybatis-Plus)

对于Mybatis-Plus,不需要写annotationClass

​package com.example.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan(basePackages = "com.springboot.chapter15.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

多个包下的mapper

例如:com.springboot.chapter15.mapper, com.springboot.chapter16.section1.mapper,两种方法:

法1:@MapperScan(basePackages = "com.springboot.**.mapper")
法2:@MapperScan(basePackages = {"com.springboot.chapter15.mapper", "com.springboot.chapter16.section1.mapper"});

@MapperScan与@Mapper

@MapperScan作用

Spring启动时,扫描到所有的Mapper文件,并生成代理类交给spring容器管理;

@Mapper作用

当项目启动时,扫描到被此注解标识接口类,就会创建代理类并交给spring容器管理。如果不加@MapperScan注解,那么Mapper接口类上就要添加@Mapper注解。

@Repository注解

@Repository是spring的注解,主动标识当前类要交给spring容器管理(相当于@Component注解)。

一般添加了@Mapper注解或者@MapperScan注解,那么就不用添加@Repository。因为@Mapper注解(@MapperScan注解)已将改接口的代理类给了spring容器管理。

但是,有时候IDEA会识别不了@Mapper注解,在项目自动编译的时候,项目会爆红色波浪线,加@Respository注解,可以抑制这种报错。

自定义启动类注解

在微服务项目中,多个微服务有时会在启动类上都加同一个注解来启用某个功能。比如:我用的是SpringCloud,那么每个项目的启动类都要加上:@EnableDiscoveryClient,很麻烦,解决方法为:将都要使用的注解统一拿出来:

package com.example.common.common.annotation;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.annotation.AliasFor;import java.lang.annotation.*;@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@MapperScan("com.example.**.mapper")
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.example.common.feign")
public @interface CommonApplication {@AliasFor(annotation = SpringBootApplication.class)String[] scanBasePackages() default "com.example.**";
}

这样,所有微服务只需加一个@CommonApplication即可,例如:

package com.example.order;import com.example.common.common.annotation.CommonApplication;
import org.springframework.boot.SpringApplication;@CommonApplication
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}}

如果微服务想引入其他注解,也可以添加,例如:

package com.example.order;import com.example.common.common.annotation.CommonApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling
@CommonApplication
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}}

其他网址

@SpringBootApplication

springboot快速入门及@SpringBootApplication注解分析

《深入浅出Spring Boot 2.x》=> 3.2.1 通过扫描装配你的Bean

SpringBoot注解--@SpringBootApplication/@MapperScan/自定义启动类注解相关推荐

  1. springboot 启动类注解 @SpringBootApplication 和 @ComponentScan 的问题

    问题: 在启动类Application上使用@ComponentScan 这个注解来扫描第三方的包,然后就会出现报错,异常就是无法加载本地的某些类,例如某些Dao无法注入. 分析: 启动类注解 @Sp ...

  2. springboot系列(三) 启动类中关键注解作用解析

    一.Springboot:请求入口@SpringBootApplication @EnableAspectJAutoProxy @EnableScheduling @EnableTransaction ...

  3. 【开发随机】JAVA+POI+自定义注解+反射构建自定义工具类实现快捷简便的Excel模板化导出(附demo代码)

    220907更新 项目UAT期间,用户反映了一个问题,数据量稍大的情况下,会出现"从某一行开始,往下所有行设置的字体和字体大小不生效"的BUG. 经过排查,发现原因是:POI的XS ...

  4. 基于SpringBoot的后台管理系统(启动类解析,开源的世界真好)(一)

    Guns基于SpringBoot,致力于做更简洁的后台管理系统,完美整合springmvc + shiro + 分页插件PageHelper + 通用Mapper + beetl!Guns项目代码简洁 ...

  5. IDEA设置类注解模板

    IDEA设置类注解解模板 一.类注解模板配置 一.类注解模板配置 步骤一:打开Idea,在菜单栏中找到 File ->setting -> File and Code Templates ...

  6. 简述SpringBoot Starter原理及自定义实现

    简述SpringBoot Starter原理及自定义实现 一.简述 二.结合SpringBoot启动原理看容器如何实现自动装配 三.解析mybatis-spring-boot-starter包看myb ...

  7. java 联合主键 注解_Hibernate的联合主键注解方式

    Hibernate联合主键注解方式,即是Hibernate中某一张表出现多个字段联合为唯一主键的情况.这时候就可以使用Hibernate的联合主键来操作.以用户表为例子,userId+userName ...

  8. SpringBoot 启动类 @SpringBootApplication 注解 以及执行流程

    @SpringBootApplication  : 是Sprnig Boot项目的核心注解,目的是开启自动配置 目录 @SpringBootApplication  : 是Sprnig Boot项目的 ...

  9. SpringBoot启动类的扫描注解的用法及冲突原则

    背景 SpringBoot 启动类上,配置扫描包路径有三种方式,最近看到一个应用上三种注解都用上了,代码如下: @SpringBootApplication(scanBasePackages ={&q ...

最新文章

  1. 中国科协发布20个重大科学问题和工程技术难题
  2. java生成Json文件
  3. Stanford机器学习笔记-1.线性回归
  4. ProgressBar 类
  5. 美军开发远程人脸识别系统,实现1公里内目标识别
  6. iTOP-iMX6开发板-设备树内核-注册驱动例程
  7. Python爬虫实战02:分析Ajax请求并抓取今日头条街拍
  8. php删除提示信息,php删除一条记录(删除确认提示)
  9. kafka启动后闪退
  10. 水面反光如何拍摄_如何抓拍水的不同瞬间
  11. python读取qq客户端消息_使用 Python 读取 QQ 消息
  12. CATIA_CAA_RADER26+VS2012安装教程
  13. Qt 之 QToolBox
  14. 天正安装autocad启动失败_安装天正后cad无法启动 - 卡饭网
  15. c语言中num =10,num/100%10 这是什么意思求详细的计算逻辑
  16. ER图、功能图画图必备Vi.sio
  17. android模拟器模拟拨号电话号码,如何在Android的Genymotion模拟器中拨打电话?
  18. mindmanager 2021许可密钥全新可视工作管理和思维导图软件
  19. 小故事——巴比奇和他的计算机
  20. NPL系列之应用介绍和技术需求(一)

热门文章

  1. vue flex 布局实现div均分自动换行
  2. python调用Matlab函数
  3. 在django中插入EX表格
  4. R语言程序设计中的for循环实战
  5. vue项目中使用 webpack优化之HappyPack 实战
  6. unity_EasyTouch
  7. 关于标量积和矢量积的理解
  8. 华工计算机实验班保研率,华南理工大学2021年保送1197名优秀应届本科毕业生免试读研...
  9. 《孤岛之鬼》——江户川乱步
  10. python闭包修改全局变量_Python 闭包 自由变量