JAVA常用注解
摘要:java引入注解后,编码节省了很多需要写代码的时间,而且精简了代码,本文主要罗列项目中常用注解。
1 main方法前(SpringApplication.run)注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClient
@EnableAsync
2 Spring Boot
Controller层
(1)类前注解:@RestController 返回JSON格式的数据;@Controller返回页面显示用
(2)方法前注解:@RequestMapping
@PostMapping
@GetMapping
@PutMapping
Service
@Bean
@Value
@Autowried
@Component 一种泛指,标记类是组件,Spring在扫描注解配置时,会标记这个类要被生成bean
@Configuration 把一个类作为一个IOC容器,它的某个方法上如果注解了@Bean,就会作为这个Spring容器的Bean。
@RefreshScope
捕获全局异常:@ControllerAdvice
3 数据
@XmlRootElement
@XmlElement
@PathVariable
4 事务相关
@Transactional 事务回滚,作用于数据库操作时,当操作数据失败,该操作可以还原