@SpringBootApplication这个注解会在SpringBoot启动类上。这个注解实际上包含3个注解@SpringBootConfiguration,@EnableAutoConfiguration和@ComponentScan
@SpringBootConfiguration继承@Configuration两个注解的功能一样。也就是标注当前类是配置类。会将当前类中声明的一个或者多个@Bean注解标记的方法的实例注入到Spring容器中。实例名就是对象名。@EnableAutoConfiguration是SpringBoot的自动配置注解。可以将符合条件的@Configuration加载到SpringBoot,并生成对应配置类的Bean,加载到Srping容器。@ComponentScan扫描当前包和子包下被@Component,@Controller,@Service,@Respository注解的类并注入到Spring容器。@Controller
@Controller注解在类上,表示这是一个控制层bean。
@RestController
@RestController注解在类上,表示这是一个控制层bean,相当于@ResponseBody和·@Controller的组合注解。使用此注解无法返回jsp,html页面,InternalResourceViewResolver不起作用。返回的内容就是return的内容。
@RequestMapping
@RequestMapping用来处理请求地址映射的注解。可以声明在类或者方法上。用在类上表示所有的请求的父路径。
@RequestMapping注解有6个属性
application/json和text/html@RequestBody和@ResponseBody@RequestBody会将request请求头中的body转换成string字符串类型。这个注解会出现在请求方法的参数上
@ResponseBody会将Map对象转化成json格式输出到HTTP中,这个注解会出现在请求的方法上。
@PathVariable,@RequestParam,@ModelAttribute,@RequestAttribute@PathVariable
@RequestMapping(value = "/index7/{id}",method = RequestMethod.GET)从请求的URI中提取id。
@RequestParam从请求头中获取参数
@RequestAttribute ,@ModelAttribute
@RequestAttribute注解取的参数是项目中解析出来的。不是从前端传过来的。可以通过ModeAttribute或HandlerInterceptor中预存。
@ModelAttribute
x-www-form-urlencoded@GetMapping
GetMapping注解已经默认封装了@RequestMapping(method = RequestMethod.GET)
@PostMapping
PostMapping注解已经默认封装了@RequestMapping(method = RequestMethod.POST)
@GetMapping和@PostMapping要配合@RestController使用,否则会报404。
@Service注解在类上,标注这是一个服务层@Component它是一个通用注解,,不属于
@Controller和@Service的组件,我们就可以用@Componment。它可以标注这个类被Spring容器管理。
@Configuration标注在类上,配置spring容器(应用上下文)。相当于把该类作为spring的xml配置文件中的<beans>。@Configuration注解的类中,使用@Bean注解标注的方法,返回的类型都会直接注册为bean。@Configuration注解基于@Component,所以他们的功能是一样的,但是意义有所不同。
@Bean该注解在该类的方法上,
AnnotationConfigApplicationContext将配置类中标注了@Bean的方法的返回值识别为Spring Bean,并注册到容器中,归入IoC容器管理。
@Autowired默认是按照类型注入的(属于Sping的注解),默认情况下要求被依赖的对象必须存在,如果要允许null值,可以设置它的required属性为false(@Autowired(required=false) )
@Resource默认根据名称进行依赖注入(属于J2EE的注解),默认情况下根据名称注入,名称可以通过name属性进行指定,如果没有指定name属性,默认取字段名进行安装名称查找.
@Qualifier这个是Spring中的一个注解,如果有多个类型相同的Bean,就可以使用
@Qualifier依据名字区分注入。
@Value通过
@Value将配置文件中定义的值注入到Bean中系列文章
第一节:创建SpringBoot项目并运行HelloWorld
第二节:SpingBoot单元测试
第三节:SpringBoot中web项目推荐目录结构
第四节:SpringBoot中web模版数据渲染展示
本小结源码地址:
介绍