1、@Resource和@Autowired区别
1)@Resource默认按照名称方式进行bean匹配,@Autowired默认按照类型方式进行bean匹配。
2)@Resource(import javax.annotation.Resource;)是J2EE的注解,@Autowired(import org.springframework.beans.factory.annotation.Autowired;)是Spring的注解。
例:定义一个接口A,它有两个实现类B和C,@Autowired就不知道注入哪一个实现类,必须搭配@Qualifier("b")来使用;只加@Resource也会报错,必须使用其name属性,@Resource(name = "b")。
注意:是b不是B,因为使用@Service,容器为我们创建bean时默认类名首字母小写
2、@RequestMapping和@GetMapping @PostMapping区别
1)@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
2)@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
3、@RestController和@Controller的区别
1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return里的内容。
2) 如果需要返回到指定页面,则需要用@Controller配合视图解析器InternalResourceViewResolver才行。如果需要返回JSON、XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
注:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。