@SpringBootApplication是SpringBoot的最核心的注解。
@SpringBootApplication注解在SpringBoot的主类上,标识是SpringBoot应用,用来开启SpringBoot的各项能力。由@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个注解组成。这三个注解是一起使用,所以SpringBoot提供了一个统一的注解@SpringBootApplication
SpringBoot的注解有很多,每个注解都有不同的功能,下面列举一些常用的注解及其功能1:
@SpringBootApplication:这是一个复合注解,它包含了@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan,通常用在主类上。
@EnableAutoConfiguration:启用SpringBoot的自动配置机制。
@ComponentScan:SpringBoot会自动扫描@SpringBootApplication所在类的同级包以及下级包里的Bean。
@Configuration:定义配置类,可替换xml配置文件。
@Bean:用在方法上,将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名。
@Controller:定义控制器类,在Spring 4.x版本之后,增加了@RestController注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。
@RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射。
@RequestParam:获取请求参数的值。
@Autowired:自动装配bean,可以省略set方法。
@Service:用于标注业务层组件。
@Repository:用于标注数据访问组件,即DAO组件。
@Entity:标注实体类,与数据库表对应。