Java常用注解
1.@component
作用:类被spring管理
2.@aspect
相当于aop:aspect/表示通知类在当前方法中
3.@Data
注解是由Lombok库提供的,会生成getter、setter以及equals()、hashCode()、toString()等方法)
4.@SpringBootApplication
SpringBoot提供的,标记这是一个SpringBoot的应用程序启动类。
5.@Resource
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。
@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。
所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。
如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
6.@Service
@Service注解用于类上,标记当前类是一个service类,加上该注解会将当前类自动注入到Spring容器中,不需要再在applicationContext.xml文件定义bean了。
7.@RestController
相当于 @Controller + @ResponseBody 两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
8.@Controller
@Controller是@Component注解的一个延伸,Spring会自动扫描并配置被该注解标注的类。此注解用于标注Spring MVC的控制器。下面是使用此注解的示例代码:
9.@getMapping
详细规定了只获取get()请求的参数,和@requestMapping相近
10.@postMapping
详细规定了只获取post()请求的参数,和@requestMapping相近
11.@RequestParam
参数名和请求参数名称不同时使用,可以设置默认值
@PostMapping @GetMapping
是@RequestMapping(method = RequestMethod.POST),@RequestMapping(method = RequestMethod.GET)的缩写
@PostMapping(value = "/payment/create")
public int create(@RequestBody Payment payment) {
return 1;
}
@GetMapping(value = "/payment/get/{id}")
public int getPaymentById(@PathVariable("id") Long id) {
return 1;
}

浙公网安备 33010602011771号