【SpringBoot】常用注解
@EnableAutoConfiguration
启动自动装载:使用了这个注解之后,所有引入的jar的starters都会被自动注入。这个类的设计就是为starter工作的。
@RestController
这个注解专门用于写RESTful的接口的,里面集成了@Controller和@ResponseBody注解。
@ResponseBody 这个注解会自动利用默认的Jackson将return的对象序列化成json格式。
@RequestMapping 、@GetMapping、@PostMapping
这些注解主要是配置路由信息。
@Import、@ImportResource、@Configuration 、@PropertySources
@Configuration :标识当前类是一个Java配置类
@Import:用于手动注入Java config类。
@ImportResource:用于注入XML配置类。
@PropertySources :用于注入properties的配置文件。
@Value、 @ConfigurationProperties
@Value 这个注解会通过设定的key自动注入 properties文件里面配置的Property属性值。比如
@Value(“${test.name}”) 会自动引入properties文件里面配置的test.name的值。 @ConfigurationProperties的作用和@Value类似,但是使用起来稍微麻烦,就不做讲解了。
查看自动加载的类:
If you need to find out what auto-configuration is currently being applied, and why, start your application with the –debug switch. This will enable debug logs for a selection of core loggers and log an auto- configuration report to the console.
@SpringBootApplication
@SpringBootApplication注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。 这里有一点需要注意:如果我们需要disable一些特殊的自动装载的类。可以使用@EnableAutoConfiguration的exclude属性,去掉自动装载的类。
当然,这个属性加在@SpringBootApplication注解上也是可以的。 @ComponentScan
组件扫描,如果加载Application这个类上,就不需要参数,自动扫面Application所在的路径和其下面的包下。不然需要加扫描包路径。
自动扫描:@Repository、@Service、@Controller、@Component 组件。
@Repository、@Service、@Controller、@Component
组件的标注:在Annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个Spring容器管理的类。即该类已经拉入到Spring的管理中了。
而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。 @Repository注解:用于标注数据访问组件,即DAO组件
@Service注解:用于标注业务层组件
@Controller注解:用于标注控制层组件(如struts中的action)
@Component注解:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Profile
这个注解主要加在@Component or @Configuration上面。标识当前profile环境。对于不同的环境可以可以选择是否加载这些配置。 然后在application.properties中选择环境,用逗号分隔。
spring.profiles.active=xxxx
@ServletComponentScan
@ServletComponentScan 注解加上后拦截器失效; 去掉后过滤器和监听器失效
@EnableCaching
开启Cache缓存支持;
@PathVariable:
路径变量。
@Conditional以及其元注解
@Conditional:满足特定条件创建一个Bean,SpringBoot就是利用这个特性进行自动配置的;
@ConditionalOnProperty:指定的属性是否有指定的值;
@ConditionalOnClass:当类路径下有指定类的条件下
@Autowired和@Resource
@Autowired :默认按照类型加载
@Resource: 默认按照bean的Name进行加载 不过上面两种都可以通过@qualifier 注解设置注入bean的Name
@Mapper
Mapper接口声明
【SpringBoot】常用注解的更多相关文章
- SpringBoot 常用注解(持续更新)
SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...
- SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置
一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...
- 接近8000字的Spring/SpringBoot常用注解总结!安排!
0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...
- Spring/SpringBoot常用注解总结
转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...
- SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解
SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...
- 菜鸟的springboot常用注解总结
菜鸟的springboot常用注解总结 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用 ...
- SpringBoot常用注解的介绍及使用 - 转载
常用注解 @springBootApplication 系统启动类注解,此注解是个组合注解,包括了:@SpringBootConfiguration,@EnableAutoConfiguration, ...
- 结合参数接收响应转换原理讲解SpringBoot常用注解
一.常用注解回顾 1.1 @RequestBody与@ResponseBody //注意并不要求@RequestBody与@ResponseBody成对使用. public @ResponseBody ...
- springboot系列五、springboot常用注解使用说明
一.controller相关注解 1.@Controller 控制器,处理http请求. 2.@RespController Spring4之后新加的注解,原来返回json需要@ResponseBod ...
- SpringBoot常用注解(三)
最全的Java常用开发注解 转 https://blog.csdn.net/weixin_40753536/article/details/81285046 Spring ...
随机推荐
- 后台拼接json字符串,传到前台时注意特殊符号处理
1.后台拼接常用的形式: sb.AppendFormat("\"字段名\":\"{0}\"", i + 1); 这个要注意字符串截断 ...
- Centos 6.5 freeswitch 编译mod_shout
1. yum install -y patch 2. yum install -y libshout-devel lame-devel libmpg123-devel 3. make install ...
- centOS docker运行Asp.net Core程序
[root@localhost chenhua]# docker run -it --rm -p : --name aspnetcore_sample microsoft/dotnet-samples ...
- Android中验证输入是否为汉字、手机号及邮箱
1,验证是否为汉字 Code// 验证昵称 private boolean verifyNickname() { String nickname = edt_username.getText().to ...
- 前端 -----js 定时器
定时器 在js中的定时器分两种:1.setTimeout() 2.setInterval() 1.setTimeOut() 只在指定时间后执行一次 /定时器 异步运行 function hello ...
- java按照关键字指定的key删除redis(支持模糊删除)
pom依赖: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</a ...
- Java替换中使用正则表达式实现中间模糊匹配
使用“.+?”实现中间模糊匹配的代码: public class Test { public static void main(String[] args) { String str="总会 ...
- response.setContentType()的String参数及对应类型
response.addHeader("Content-Disposition", "attachment;filename="+ filename); res ...
- 搭建 RabbitMQ Server 高可用集群【转】
阅读目录: 准备工作 搭建 RabbitMQ Server 单机版 RabbitMQ Server 高可用集群相关概念 搭建 RabbitMQ Server 高可用集群 搭建 HAProxy 负载均衡 ...
- iOS -- Effective Objective-C 阅读笔记 (3)
1: 理解 属性 的概念 属性会自动生成存取方法, 可以利用点语法调用, 若不想编译器自动合成存取方法, 可以自己实现, 还有另外一种方法, 就是使用 @dynamic 关键字, 它会告诉编译器, ...