李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
SpringBoot常用注解知识总结
Leefs
2020-03-18 PM
2616℃
1条
# SpringBoot常用注解知识总结 ### 一、@Autowired和@Resources注解区别 @Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果null值,可以设置它required属性为false。 @Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定Name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。 **总结** @Resources按名字,是JDK的,@Autowired按类型,是Spring的。 ### 二、启动注解@SpringBootApplication ```java @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { // ... 此处省略源码 } ``` ①. @SpringBootApplication是一个复合注解,包含了**@SpringBootConfiguration,@EnableAutoCofiguration,@ComponentScan这三个注解** ②. @SpringBootConfiguration继承自@Configuration,二者功能一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。 ③.**@EnableAutoConfiguration 注解,开启自动配置功能** @EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IOC容器。借助于Spring框架原有的一个工具类:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自动配置功效才得以大功告成 ④.**@ComponentScan 注解,主要用于组件扫描和自动装配** @ComponentScan的功能其实就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中。我们可以通过basePackages等属性指定@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现从声明@ComponentScan所在类的package进行扫描,默认情况下是不指定的,所以SpringBoot的启动类最好放在root package下。 ### 三、Controller 相关注解 @Controller:控制器,处理http请求。 **@RestController 复合注解** @RestController注解相当于@ResponseBody、@Controller合在一起的作用,RestController使用的效果是将方法返回的对象直接在浏览器上展示成json格式. **@RequestBody** 通过HttpMessageConverter读取Request Body并反序列化为Object(泛指)对象 **@RequestMapping** @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一。这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上 **@GetMapping用于将HTTP get请求映射到特定处理程序的方法注解** **@PostMapping用于将HTTP post请求映射到特定处理程序的方法注解** ### 四、取请求参数值 **@PathVariable:获取url中的数据** **@RequestParam:获取请求参数的值** ```java @Controller @RequestMapping("/User") public class HelloWorldController { @RequestMapping("/getUser/{uid}") public String getUser(@PathVariable("uid")Integer id, Model model) { System.out.println("id:"+id); return "user"; } } ``` 请求示例:http://localhost:8080/User/getUser/123 **@RequestParam:获取请求参数的值** ```java @Controller @RequestMapping("/User") public class HelloWorldController { @RequestMapping("/getUser") public String getUser(@RequestParam("uid")Integer id, Model model) { System.out.println("id:"+id); return "user"; } } ``` 请求示例:http://localhost:8080/User/getUser?uid=123 **@RequestHeader 把Request请求header部分的值绑定到方法的参数上** **@CookieValue 把Request header中关于cookie的值绑定到方法的参数上** ### 五、注入bean相关 **@Repository** DAO层注解,DAO层中接口继承JpaRepository
,需要在build.gradle中引入相关jpa的一个jar自动加载。 **@Service** + 1.@Service是@Component注解的一个特例,作用在类上 + 2.@Service注解作用域默认为单例 + 3.使用注解配置和类路径扫描时,被@Service注解标注的类会被Spring扫描并注册为Bean + 4.@Service用于标注服务层组件,表示定义一个bean - @Service使用时没有传参数,Bean名称默认为当前类的类名,首字母小写 - @Service(“serviceBeanId”)或@Service(value=”serviceBeanId”)使用时传参数,使用value作为Bean名字 **@Scope作用域注解** @Scope作用在类上和方法上,用来配置 spring bean 的作用域,它标识 bean 的作用域 **@Entity实体类注解** @Table(name ="数据库表名"),这个注解也注释在实体类上,对应数据库中相应的表。 @Id、@Column注解用于标注实体类中的字段,pk字段标注为@Id,其余@Column。 **@Bean产生一个bean的方法** @Bean明确地指示了一种方法,产生一个bean的方法,并且交给Spring容器管理。支持别名@Bean("xx-name") **@Autowired 自动导入** - @Autowired注解作用在构造函数、方法、方法参数、类字段以及注解上 - @Autowired注解可以实现Bean的自动注入 **@Component** 把普通pojo实例化到spring容器中,相当于配置文件中的 虽然有了@Autowired,但是我们还是要写一堆bean的配置文件,相当麻烦,而@Component就是告诉spring,我是pojo类,把我注册到容器中吧,spring会自动提取相关信息。那么我们就不用写麻烦的xml配置文件了 ### 六、功能注解 **@Async与@EnableAsync** 其中@Async表示这个方法为异步方法;@EnableAsync这个注解需要加在启动类上,表示支持异步操作;如果不加,则@Async将不起作用。 **@Scheduled与@EnableScheduling** 定时任务。@EnableScheduling这个注解需要加在启动类上,表示支持定时任务 ```java @Scheduled(cron = "0/5 * * * * ? ") public void doTest() { System.out.println(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date())); } ``` **@CrossOrigin** 跨域注解 ```java @CrossOrigin @RestController public class person{ @RequestMapping(method = RequestMethod.GET) public String add() { // 若干代码 } } ``` *附:参考文章链接* *https://www.cnblogs.com/tqlin/p/11687811.html*
标签:
SpringBoot
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/747.html
上一篇
Java集合相关知识总结
下一篇
MySQL性能优化知识总结
取消回复
评论啦~
提交评论
唉呀 ~ 仅有一条评论
test11
测试哈哈哈
回复
2021-07-16 10:06
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
47
标签云
Netty
Flume
Kafka
Thymeleaf
散列
Hive
Python
数学
Spark SQL
Spring
Elastisearch
MyBatis-Plus
Golang
栈
SQL练习题
JVM
Spark RDD
DataX
Spark
NIO
MyBatisX
SpringCloud
数据结构和算法
数据结构
锁
Beego
Elasticsearch
JavaWEB项目搭建
国产数据库改造
Azkaban
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞
测试哈哈哈