常用注解概览

这里整理了一张SpringBoot常用注解的思维导图,本文主要讲解这些注解的用法。

组件相关注解

@ComponentScan

默认扫描当前包及其子包下面被@component,@Controller,@Service,@Repository标记标注的类然后纳入spring管理容器中去

#@Controller

用于修饰MVC中controller层的组件,SpringBoot中的组件扫描功能会识别到该注解,并为修饰的类实例化对象,通常与@RequestMapping联用,当SpringMVC获取到请求时会转发到指定路径的方法进行处理。

/**
* @auther macrozheng
* @description 后台用户管理Controller
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Controller
@RequestMapping("/admin")
public class UmsAdminController { }

@Service

用于修饰service层的组件,service层组件专注于系统业务逻辑的处理,同样会被组件扫描并生成实例化对象。

/**
* @auther macrozheng
* @description 后台用户管理Service实现类
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Service
public class UmsAdminServiceImpl implements UmsAdminService { }

@Repository和@Mapper区别

用于修饰dao层的组件,dao层组件专注于系统数据的处理,例如数据库中的数据,同样会被组件扫描并生成实例化对象。

/**
* @auther macrozheng
* @description 后台用户与角色关系管理自定义Dao
* @date 2018/10/8
* @github https://github.com/macrozheng
*/
@Repository
public interface UmsAdminRoleRelationDao { }

springboot整合mybatis时,mapper接口上用的注解是以前学spring时用的@Repository注解,可一运行,就出现了错误。

找不到mapper这个bean

1、@Repository

@Repository 是 Spring 的注解,用于声明一个 Bean。@Repository单独使用没用。可以这样理解,注解放在接口上本来就没有意义,spring中在mapper接口上写一个@Repository注解,只是为了标识,要想真正是这个接口被扫描,必须使用@MapperScannerConfigurer

<!-- 配置 Mapper 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.shenlei.mapper"/>
</bean>

这段配置会扫描com.shenlei.mapper包下所有的接口,然后创建各自的动态代理类。
与spring集成可分三个步骤:
1、把java类对应的Mapper接口类纳入spring总的IOC容器。
2、把Java类对应的XML命名空间添加到Mybatis中的Configuration类中的mapperRegistry(用于管理Mybatis的Mapper)
3、使用spring中的IOC容器拓展FactoryBean获取到Mapper的实例。(第一步纳入spring只是接口)

2、 @Mapper

@Mapper是mybatis自身带的注解。在spring程序中,mybatis需要找到对应的mapper,在编译时生成动态代理类,与数据库进行交互,这时需要用到@Mapper注解

但是有时候当我们有很多mapper接口时,就需要写很多@Mappe注解,这样很麻烦,有一种简便的配置化方法便是在启动类上使
用@MapperScan注解。

这样可以自动扫描包路径下所有的mapper接口,从而不用再在接口上添加任何注解。

3、区别

相同点:

@Mapper和@Repository都是作用在dao层接口,使得其生成代理对象bean,交给spring 容器管理
对于mybatis来说,都可以不用写mapper.xml文件

不同点:

1、@Mapper不需要配置扫描地址,可以单独使用,如果有多个mapper文件的话,可以在项目启动类中加入@MapperScan(“mapper文件所在包”)
2、@Repository不可以单独使用,否则会报错误,要想用,必须配置扫描地址(@MapperScannerConfigurer)

4、解决使用@mapper接口时,注入mapper爆红问题

在idea中单独使用@Mapper注解,在@Autowired时,idea会提示找不到bean,但是不影响运行,如果想消除爆红,可以将@Mapper注解跟@Repository注解一起用,这样便可消除爆红

这样便可消除爆红

@Component

用于修饰SpringBoot中的组件,会被组件扫描并生成实例化对象。@Controller@Service@Repository都是特殊的组件注解。

/**
* @auther macrozheng
* @description 取消订单消息的生产者组件
* @date 2018/9/14
* @github https://github.com/macrozheng
*/
@Component
public class CancelOrderSender { }

依赖注入注解

#@Autowired

会根据对象的类型自动注入依赖对象,默认要求注入对象实例必须存在,可以配置required=false来注入不一定存在的对象。

/**
* @auther macrozheng
* @description 后台用户管理Controller
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Controller
@RequestMapping("/admin")
public class UmsAdminController {
@Autowired
private UmsAdminService adminService;
}

@Resource

默认会根据对象的名称自动注入依赖对象,如果想要根据类型进行注入,可以设置属性为type = UmsAdminService.class

/**
* @auther macrozheng
* @description 后台用户管理Controller
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Controller
@RequestMapping("/admin")
public class UmsAdminController {
@Autowired
@Resource(name = "umsAdminServiceImpl")
private UmsAdminService adminService;
}

@Qualifier

当同一个对象有多个实例可以注入时,使用@Autowired注解无法进行注入,这时可以使用@Qualifier注解指定实例的名称进行精确注入。

/**
* @auther macrozheng
* @description 后台用户管理Controller
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Controller
@RequestMapping("/admin")
public class UmsAdminController {
@Autowired
@Qualifier("umsAdminServiceImpl")
private UmsAdminService adminService;
}

实例与生命周期相关注解

#@Bean

用于修饰方法,标识该方法会创建一个Bean实例,并交给Spring容器来管理。

/**
* @auther macrozheng
* @description RestTemplate相关配置
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

@Scope

用于声明一个SpringBean实例的作用域,作用域的范围有以下几种:

    • singleton:单例模式,在Spring容器中该实例唯一,Spring默认的实例模式。
    • prototype:原型模式,每次使用实例都将重新创建。
    • request:在同一请求中使用相同的实例,不同请求重新创建。
    • session:在同一会话中使用相同的实例,不同会话重新创建。
/**
* @auther macrozheng
* @description RestTemplate相关配置
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Configuration
public class RestTemplateConfig {
@Bean
@Scope("singleton")
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

@Primary

当同一个对象有多个实例时,优先选择该实例。

/**
* @auther macrozheng
* @description Jackson相关配置,配置json不返回null的字段
* @date 2018/8/2
* @github https://github.com/macrozheng
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}
}

@PostConstruct

用于修饰方法,当对象实例被创建并且依赖注入完成后执行,可用于对象实例的初始化操作。

#@PreDestroy

用于修饰方法,当对象实例将被Spring容器移除时执行,可用于对象实例持有资源的释放。

#@PostConstruct、@PreDestroy示例

/**
* @auther macrozheng
* @description 动态权限数据源,用于获取动态权限规则
* @date 2020/2/7
* @github https://github.com/macrozheng
*/
public class DynamicSecurityMetadataSource implements FilterInvocationSecurityMetadataSource { private static Map<String, ConfigAttribute> configAttributeMap = null;
@Autowired
private DynamicSecurityService dynamicSecurityService; @PostConstruct
public void loadDataSource() {
configAttributeMap = dynamicSecurityService.loadDataSource();
} @PostConstruct
public void loadDataSource() {
configAttributeMap = dynamicSecurityService.loadDataSource();
} @PreDestroy
public void clearDataSource() {
configAttributeMap.clear();
configAttributeMap = null;
}
}

SpringMVC相关注解

#@RequestMapping

可用于将Web请求路径映射到处理类的方法上,当作用于类上时,可以统一类中所有方法的路由路径,当作用于方法上时,可单独指定方法的路由路径。

method属性可以指定请求的方式,如GET、POST、PUT、DELETE等。

#@RequestBody

表示方法的请求参数为JSON格式,从Body中传入,将自动绑定到方法参数对象中。

#@ResponseBody

表示方法将返回JSON格式的数据,会自动将返回的对象转化为JSON数据。

#@RequestParam

用于接收请求参数,可以是如下三种形式:

  • query param:GET请求拼接在地址里的参数。
  • form data:POST表单提交的参数。
  • multipart:文件上传请求的部分参数。

#@PathVariable

用于接收请求路径中的参数,常用于REST风格的API。

#@RequestPart

用于接收文件上传中的文件参数,通常是multipart/form-data形式传入的参数。

/**
* @auther macrozheng
* @description MinIO对象存储管理Controller
* @date 2019/12/25
* @github https://github.com/macrozheng
*/
@Controller
@RequestMapping("/minio")
public class MinioController { @RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public CommonResult upload(@RequestPart("file") MultipartFile file) {
//省略文件上传操作...
return CommonResult.success(minioUploadDto);
}
}

SpringMVC注解示例

/**
* @auther macrozheng
* @description 后台用户管理Controller
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Controller
@RequestMapping("/admin")
public class UmsAdminController { @RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public CommonResult<UmsAdmin> register(@RequestBody UmsAdminParam umsAdminParam) {
UmsAdmin umsAdmin = adminService.register(umsAdminParam);
if (umsAdmin == null) {
return CommonResult.failed();
}
return CommonResult.success(umsAdmin);
} @RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public CommonResult<CommonPage<UmsAdmin>> list(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
List<UmsAdmin> adminList = adminService.list(keyword, pageSize, pageNum);
return CommonResult.success(CommonPage.restPage(adminList));
} @RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public CommonResult<UmsAdmin> getItem(@PathVariable Long id) {
UmsAdmin admin = adminService.getItem(id);
return CommonResult.success(admin);
}
}

@RestController

用于表示controller层的组件,与@Controller注解的不同在于,相当于在每个请求处理方法上都添加了@ResponseBody注解,这些方法都将返回JSON格式数据。

#@GetMapping

用于表示GET请求方法,等价于@RequestMapping(method = RequestMethod.GET)

#@PostMapping

用于表示POST请求方法,等价于@RequestMapping(method = RequestMethod.POST)

#REST风格注解示例

/**
* @auther macrozheng
* @description 后台用户管理Controller
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@RestController
@RequestMapping("/admin")
public class UmsAdminController { @PostMapping("/register")
public CommonResult<UmsAdmin> register(@RequestBody UmsAdminParam umsAdminParam) {
UmsAdmin umsAdmin = adminService.register(umsAdminParam);
if (umsAdmin == null) {
return CommonResult.failed();
}
return CommonResult.success(umsAdmin);
} @GetMapping("/list")
public CommonResult<CommonPage<UmsAdmin>> list(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
List<UmsAdmin> adminList = adminService.list(keyword, pageSize, pageNum);
return CommonResult.success(CommonPage.restPage(adminList));
}
}

配置相关注解

#@Configuration

用于声明一个Java形式的配置类,SpringBoot推荐使用Java配置,在该类中声明的Bean等配置将被SpringBoot的组件扫描功能扫描到。

/**
* @auther macrozheng
* @description MyBatis相关配置
* @date 2019/4/8
* @github https://github.com/macrozheng
*/
@Configuration
@MapperScan({"com.macro.mall.mapper","com.macro.mall.dao"})
public class MyBatisConfig {
}

@EnableAutoConfiguration

启用SpringBoot的自动化配置,会根据你在pom.xml添加的依赖和application-dev.yml中的配置自动创建你需要的配置。

@Configuration
@EnableAutoConfiguration
public class AppConfig {

@ComponentScan

启用SpringBoot的组件扫描功能,将自动装配和注入指定包下的Bean实例。

@Configuration
@ComponentScan({"xyz.erupt","com.macro.mall.tiny"})
public class EruptConfig {
}

@SpringBootApplication

用于表示SpringBoot应用中的启动类,相当于@EnableAutoConfiguration@EnableAutoConfiguration@ComponentScan三个注解的结合体。

@SpringBootApplication
public class MallTinyApplication { public static void main(String[] args) {
SpringApplication.run(MallTinyApplication.class, args);
}
}

@EnableCaching

当添加Spring Data Redis依赖之后,可用该注解开启Spring基于注解的缓存管理功能。

/**
* @auther macrozheng
* @description Redis配置类
* @date 2020/3/2
* @github https://github.com/macrozheng
*/
@EnableCaching
@Configuration
public class RedisConfig extends BaseRedisConfig { } 

@value

用于注入在配置文件中配置好的属性,例如我们可以在application.yml配置如下属性:

jwt:
tokenHeader: Authorization #JWT存储的请求头
secret: mall-admin-secret #JWT加解密使用的密钥
expiration: 604800 #JWT的超期限时间(60*60*24*7)
tokenHead: 'Bearer ' #JWT负载中拿到开头

然后在Java类中就可以使用@Value注入并进行使用了。

public class JwtTokenUtil {
@Value("${jwt.secret}")
private String secret;
@Value("${jwt.expiration}")
private Long expiration;
@Value("${jwt.tokenHead}")
private String tokenHead;
}

@ConfigurationProperties

用于批量注入外部配置,以对象的形式来导入指定前缀的配置,比如这里我们在application.yml中指定了secure.ignored为前缀的属性:

secure:
ignored:
urls: #安全路径白名单
- /swagger-ui/
- /swagger-resources/**
- /**/v2/api-docs
- /**/*.html
- /**/*.js
- /**/*.css
- /**/*.png
- /**/*.map
- /favicon.ico
- /actuator/**
- /druid/**

然后在Java类中定义一个urls属性就可以导入配置文件中的属性了。

/**
* @auther macrozheng
* @description SpringSecurity白名单资源路径配置
* @date 2018/11/5
* @github https://github.com/macrozheng
*/
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "secure.ignored")
public class IgnoreUrlsConfig { private List<String> urls = new ArrayList<>(); }

@Conditional

用于表示当某个条件满足时,该组件或Bean将被Spring容器创建,下面是几个常用的条件注解。

    • @ConditionalOnBean:当某个Bean存在时,配置生效。
    • @ConditionalOnMissingBean:当某个Bean不存在时,配置生效。
    • @ConditionalOnClass:当某个类在Classpath存在时,配置生效。
    • @ConditionalOnMissingClass:当某个类在Classpath不存在时,配置生效。
/**
* @auther macrozheng
* @description Jackson相关配置,配置json不返回null的字段
* @date 2018/8/2
* @github https://github.com/macrozheng
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}
}

数据库事务相关注解

#@EnableTransactionManagement

启用Spring基于注解的事务管理功能,需要和@Configuration注解一起使用。

/**
* @auther macrozheng
* @description MyBatis相关配置
* @date 2019/4/8
* @github https://github.com/macrozheng
*/
@Configuration
@EnableTransactionManagement
@MapperScan({"com.macro.mall.mapper","com.macro.mall.dao"})
public class MyBatisConfig {
}

@Transactional

表示方法和类需要开启事务,当作用与类上时,类中所有方法均会开启事务,当作用于方法上时,方法开启事务,方法上的注解无法被子类所继承。

/**
* @auther macrozheng
* @description 前台订单管理Service
* @date 2018/8/30
* @github https://github.com/macrozheng
*/
public interface OmsPortalOrderService { /**
* 根据提交信息生成订单
*/
@Transactional
Map<String, Object> generateOrder(OrderParam orderParam);
}

SpringSecurity相关注解

#@EnableWebSecurity

启用SpringSecurity的Web功能。

#@EnableGlobalMethodSecurity

启用SpringSecurity基于方法的安全功能,当我们使用@PreAuthorize修饰接口方法时,需要有对应权限的用户才能访问。

#SpringSecurity配置示例

/**
* @auther macrozheng
* @description SpringSecurity配置
* @date 2019/10/8
* @github https://github.com/macrozheng
*/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig{ }

全局异常处理注解

#@ControllerAdvice

常与@ExceptionHandler注解一起使用,用于捕获全局异常,能作用于所有controller中。

#@ExceptionHandler

修饰方法时,表示该方法为处理全局异常的方法。

#全局异常处理示例

/**
* @auther macrozheng
* @description 全局异常处理
* @date 2020/2/27
* @github https://github.com/macrozheng
*/
@ControllerAdvice
public class GlobalExceptionHandler { @ResponseBody
@ExceptionHandler(value = ApiException.class)
public CommonResult handle(ApiException e) {
if (e.getErrorCode() != null) {
return CommonResult.failed(e.getErrorCode());
}
return CommonResult.failed(e.getMessage());
}
}

AOP相关注解

#@Aspect

用于定义切面,切面是通知和切点的结合,定义了何时、何地应用通知功能。

#@Before

表示前置通知(Before),通知方法会在目标方法调用之前执行,通知描述了切面要完成的工作以及何时执行。

#@After

表示后置通知(After),通知方法会在目标方法返回或抛出异常后执行。

#@AfterReturning

表示返回通知(AfterReturning),通知方法会在目标方法返回后执行。

#@AfterThrowing

表示异常通知(AfterThrowing),通知方法会在目标方法返回后执行。

#@Around

表示环绕通知(Around),通知方法会将目标方法封装起来,在目标方法调用之前和之后执行自定义的行为。

#@Pointcut

定义切点表达式,定义了通知功能被应用的范围。

#@Order

用于定义组件的执行顺序,在AOP中指的是切面的执行顺序,value属性越低优先级越高。

#AOP相关示例

/**
* @auther macrozheng
* @description 统一日志处理切面
* @date 2018/4/26
* @github https://github.com/macrozheng
*/
@Aspect
@Component
@Order(1)
public class WebLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class); @Pointcut("execution(public * com.macro.mall.tiny.controller.*.*(..))")
public void webLog() {
} @Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
} @AfterReturning(value = "webLog()", returning = "ret")
public void doAfterReturning(Object ret) throws Throwable {
} @Around("webLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
WebLog webLog = new WebLog();
//省略日志处理操作...
Object result = joinPoint.proceed();
LOGGER.info("{}", JSONUtil.parse(webLog));
return result;
} }

测试相关注解

#@SpringBootTest

用于指定测试类启用Spring Boot Test功能,默认会提供Mock环境。

#@Test

指定方法为测试方法。

#测试示例

/**
* @auther macrozheng
* @description JUnit基本测试
* @date 2022/10/11
* @github https://github.com/macrozheng
*/
@SpringBootTest
public class FirstTest {
@Test
public void test() {
int a=1;
Assertions.assertEquals(1,a);
}
}

总结

这些SpringBoot注解基本都是我平时做项目常用的注解,在我的电商实战项目mall中基本都用到了,这里做了一番整理归纳,希望对大家有所帮助!

最新文章

  1. 跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题
  2. Java Iterator, ListIterator 和 foreach语句使用
  3. vijos 1512
  4. java中的动态代理
  5. TRUNC函数,ORA-01898 精度说明符过多
  6. tarjan总结
  7. fil_space_create
  8. shell脚本中&gt;/dev/null的含义
  9. dump iot表
  10. Python Tutorial 学习(四)--More Control Flow Tools
  11. android 5.0 受欢迎的API简介
  12. Ajax 跨域 异步 CORS
  13. 源码实现 --&gt; strcmp
  14. 【工具篇】抓包中的王牌工具—Fiddler (1-环境搭建)
  15. iOS中图片拉伸,类似Android中的点9图片
  16. WEB站点服务器安全配置
  17. 如何使用C语言的面向对象
  18. C#实现一张塔松叶
  19. VirtualBox 报错VERR_VD_IMAGE_READ_ONLY
  20. C点滴成海------Ubuntu怎么运行C语言

热门文章

  1. Surp Suite入门
  2. Jmeter在结果树中查看响应数据为空
  3. Lombok介绍和配置
  4. AWVS漏洞扫描器的使用
  5. windows装机小经验
  6. 11、ON DUPLICATE KEY UPDATE实现插入更新操作
  7. Python实验报告(第8章)
  8. [python] 基于chardet识别字符编码
  9. 《Kubernetes Operator 开发进阶》- 作者絮絮叨
  10. 数据库服务器CPU不能全部利用原因分析