SpringBoot最最最常用的注解梳理

论坛 期权论坛     
选择匿名的用户   2021-5-30 00:21   655   0
<div id="js_content">
<p style="text-align: center">点击上方 &#34;<a href="http://mp.weixin.qq.com/s?__biz&#61;MzI5ODI5NDkxMw%3D%3D&amp;chksm&#61;eca95686dbdedf90db774b5e1b09caaffbb95e72513a579a5c37c16325ccde7e1d75f0a4e736&amp;idx&#61;2&amp;mid&#61;2247490920&amp;scene&#61;21&amp;sn&#61;84fa3d94375cac784eeb24472f885020#wechat_redirect"><strong>程序员小乐</strong></a>&#34;关注, 星标或置顶一起成长</p>
<p style="text-align: center">后台回复“<strong>大礼包</strong>”有惊喜礼包!</p>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-e3c1bbb29b8b03e0c60d06fa65fc8f85"></p>
<pre class="blockcode"><code class="language-php">关注订阅号「程序员小乐」,收看更多精彩内容</code></pre>
<p><strong>每日英文</strong></p>
<p>Sometimes there is no next time, no second chance, no time out. Sometimes it is now or never.</p>
<p>有时候,没有下一次,没有机会重来,没有暂停继续。有时候,错过了现在,就永远永远的没机会了。</p>
<p><strong>每日掏心</strong><strong></strong><strong>话</strong></p>
<p>幸福,就是当激情退去、容颜衰老,牵你的还是那双不怨悔的手;陪你的还是那颗不回头的心;暖你的还是那份不冷却的情。</p>
<p>来自:张伯毅 | 责编:乐乐</p>
<p>链接:s.yam.com/w6mfR</p>
<p><a href="http://mp.weixin.qq.com/s?__biz&#61;MzI5ODI5NDkxMw%3D%3D&amp;chksm&#61;eca95d82dbded494d33755649ad9879e32a3fe8b287cb2ecadb173238aa4ac65df3b6cf16aa7&amp;idx&#61;1&amp;mid&#61;2247489132&amp;scene&#61;21&amp;sn&#61;c15c4bf118abad5bea9afc287585f760#wechat_redirect">程序员小乐(ID:study_tech)</a><a href="http://mp.weixin.qq.com/s?__biz&#61;MzI5ODI5NDkxMw%3D%3D&amp;chksm&#61;eca95d82dbded494d33755649ad9879e32a3fe8b287cb2ecadb173238aa4ac65df3b6cf16aa7&amp;idx&#61;1&amp;mid&#61;2247489132&amp;scene&#61;21&amp;sn&#61;c15c4bf118abad5bea9afc287585f760#wechat_redirect">第 1033 次推文</a></p>
<p style="text-align: left">往日回顾:<a href="http://mp.weixin.qq.com/s?__biz&#61;MzI5ODI5NDkxMw%3D%3D&amp;chksm&#61;ecaae692dbdd6f847903101da56e44451e2574db8175dcb8e505592000234a875d871016cd80&amp;idx&#61;1&amp;mid&#61;2247511420&amp;scene&#61;21&amp;sn&#61;bdc6355d94720d01c60af86b4d08a062#wechat_redirect">955.WLB 不加班公司名单新增 6 家公司,移出 1 家公司!</a></p>
<p><strong>     </strong></p>
<p><strong>   正文   </strong></p>
<pre class="blockcode"><code class="language-go">一、注解(annotations)列表
&#64;SpringBootApplication: 包含了&#64;ComponentScan、&#64;Configuration和&#64;EnableAutoConfiguration注解。其中&#64;ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。
&#64;Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。
&#64;EnableAutoConfiguration 自动配置。
&#64;ComponentScan 组件扫描,可自动发现和装配一些Bean。
&#64;Component 可配合CommandLineRunner使用,在程序启动后执行一些基础任务。
&#64;RestController 注解是&#64;Controller和&#64;ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。
&#64;Autowired 自动导入。
&#64;PathVariable 获取参数。
&#64;JsonBackReference 解决嵌套外链问题。
&#64;RepositoryRestResourcepublic 配合spring-boot-starter-data-rest使用。
二、注解(annotations)详解&#64;SpringBootApplication: 申明让spring boot自动给程序进行必要的配置,这个配置等同于:&#64;Configuration ,&#64;EnableAutoConfiguration 和 &#64;ComponentScan 三个配置。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

&#64;SpringBootApplication // same as &#64;Configuration &#64;EnableAutoConfiguration &#64;ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
&#64;ResponseBody: 表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。在使用&#64;RequestMapping后,返回值通常解析为跳转路径,加上&#64;responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
比如异步获取json数据,加上&#64;responsebody后,会直接返回json数据。该注解一般会配合&#64;RequestMapping一起使用。
示例代码:
&#64;RequestMapping(“/test”)
&#64;ResponseBody
public String test(){
    return”ok”;
}
&#64;Controller: 用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解&#64;RequestMapping。
示例代码:
&#64;Controller
&#64;RequestMapping(“/demoInfo”)
publicclass DemoController {
    &#64;Autowired
    private DemoInfoService demoInfoService;

    &#64;RequestMapping(&#34;/hello&#34;)
    public String hello(Map&lt;String,Object&gt; map){
        System.out.println(&#34;DemoController.hello()&#34;);
        
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP