常用注解

SpringBoot笔记(二)常用注解
96 世外大帝
2018.04.24 10:42 字数 881 阅读 85评论 2喜欢 1
@RestController
@RestController是Spring4之后新加入的注解,是@ResponseBody和@Controller的组合注解
@RequestMapping
@RequestMapping 是用来配置url映射的注解
它既可以作用在某个方法上,也可以作用在控制器类上:
在类级别上添加@RequestMapping注解时,会作用到该控制器的所有处理器方法上。
处理器方法上的@RequestMapping注解会对类级别上的@RequestMapping的声明进行补充。
@PathVariable
@PathVariable 获取url中的数据
比较适合restful风格的get
@GetMapping("/{id}/{name}/{sex}")
public String test(@PathVariable(“id”) Integer id, @PathVariable(“name”) String name, @PathVariable(“sex”) Boolean sex){
return “id=”+id +"\nname="+name+"\nsex="+sex;
}
@RequestParam
@RequestParam 获取请求参数的值
比较适合post,传统get格式也可以用
可以设置默认值和非必传参数
get

@GetMapping("/RequestParamTest")
public String RequestParamTest(@RequestParam(“id”) Integer id, @RequestParam(“name”) String name){
return “id=”+id +"\nname="+name;
}
post

@PostMapping("/RequestParamTest")
public User RequestParamTest(@RequestParam("id") Integer id, @RequestParam("name") String name){
    User user = new User();
    user.setId(id);
    user.setName(name);
    return user;
}

RequestParam源码中可以看到,是可以设置非必传和默认值的,只不过赋予了默认赋值了必传和一堆空格

注意!默认值是String类型

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor(“name”)
String value() default “”;

@AliasFor("value")
String name() default "";

boolean required() default true;

String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";

}

//这样调用
@PostMapping("/RequestParamTest")
public User RequestParamTest(@RequestParam(value = “id”, required = false, defaultValue = “1”) Integer id){
User user = new User();
user.setId(id);
user.setName(“zhangsan”);
return user;
}

而且发现没有?return一个对象出来的结果是json

@GetMapping
@GetMapping 是@RequestMapping(method = RequestMethod.GET)的缩写
@PostMapping
@PostMapping()是@RequestMapping(method = RequestMethod.POST)的缩写
@ResponseBody:
表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用
在使用@RequestMapping后,返回值通常解析为跳转路径,加上@ResponseBody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
异步获取json数据时,加上@ResponseBody后,会直接返回json数据。
@SpringBootApplication:
包含@Configuration、@EnableAutoConfiguration、@ComponentScan等
通常用在主类上。
@Repository:
用于标注数据访问组件,即DAO组件。

@Service:
用于标注业务层组件。

@Component:
泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Configuration:
指出该类是Bean配置的信息源,相当于XML中的,一般加在主类上。
@Bean:
产生一个bean,并交给spring管理。
相当于XML中的
一般作用在方法上
@ComponentScan:
组件扫描,一般作用与主类上
个人理解针对指定的包进行扫描并注册
@EnableAutoConfiguration:
让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,一般加在主类上。

@AutoWired和@Resource:
他俩都可以用来装配bean
都可以写在字段上,或setter方法上
@AutoWired
@Autowired默认按类型装配(spring的注解)
默认情况下必须要求依赖对象必须存在
如果要允许null值,可以设置它的required属性为false,否则报错
@Resource
@Autowired默认按名称装配(java的注解)
名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名
注解写在setter方法上默认取属性名进行装配
当找不到与名称匹配的bean时也会按照类型进行装配。
name属性一旦指定,就只会按照名称进行装配
@Qualifier:
当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。
与@Autowired配合使用
所以个人认为还不如直接用@Resource
@Profiles
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。
任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。
目前为止,我还没用过
@Configuration
@Profile(“prod”)
public class ProductionConfiguration {
// …
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值