SpringBoot注释
Bean/组件管理
注释名称 | 用途 |
@SpringBootApplication | 标记主程序类为springboot类 |
@SpringBootConfiguration |
配置类,类的方法可以返回一个Bean,需要用@Bean注释方法 使用该方式返回Bean,Bean类不需要使用Component注释 |
@Component |
把一个类标记为Bean,交由sprign容器管理 获取Bean的方式 1)使用context.getBean(xxx.class) ,多用于控制台程序 2)使用Autowired注解,或者方法依赖注入 |
@Service |
Service包含了注释@Component,那与Component区别在哪? Service标记的类是“服务”,最初由域驱动定义。也可以用作业务类。 这个注解是@Component的一个特化 |
@Repository |
Repository包含了注释@Component,那与Component区别在哪? Repository指示带注释的类是“存储库”,适用于JPA接口,mybatis接口,也可以用于DAO类 |
@Import 这个很少用,是springboot底层 |
注入Bean,使用该方式返回Bean,Bean类不需要使用Component注释 比如:@Import({XXX.class})
使用这种方式,相当于实现了ImportSelector或ImportBeanDefinitionRegistrar类,返回了需要注册的Bean |
SpringMVC注释
控制器注释
注释名称 | 用途 |
@Controller | 使用模板的控制器,控制器方法应返回String或者是ModelAndView类型。 |
@RestController | 控制器直接输出字符串,方法应返回String类型 |
@RequestMapping |
指定控制器(或控制器内的方法)路由,请求方法等等属性,例如: @RequestMapping(value = "/goods", method = RequestMethod.GET, produces={"application/json;charset=UTF-8"}) |
@PathVariable |
指定路由中变量定义,例如: @RequestMapping(value = "/{goodsId}") public String get(@PathVariable(value="goodsId") long goodsId ... |
@RequestBody |
获取请求体内容,该注解常用来处理Content-Type: 不是 @RequestBody String requestBody |
@ResponseBody | 效果等同于@RestController,但允许绑定在Controller的方法上。 |