Fork me on Gitee

Springboot基础核心

1.Springboot介绍

 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架

2.Springboot项目搭建

(1).通过Eclipse进行构建

(2).通过Idea进行构建

(3).通过Spring网站进行构建(推荐)

  

访问https://start.spring.io/
选择构建工具Maven Project、Spring Boot版本
点击Generate Project下载项目压缩包

 

 3.Springboot项目结构

 

src/main/java  程序开发以及主程序入口
src/main/resources 配置文件
src/test/java  测试程序

 

4.Springboot配置文件

pom.xml文件中默认有两个模块:

spring-boot-starter:核心模块,包括自动配置支持、日志和YAML;

spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito。

Web模块引入  

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

 

 Springboot默认配置文件application.properties的常见配置属性

 使用spring boot可以非常方便、快速搭建项目,使我们不用关心框架之间的兼容性,适用版本等各种问题,我们想使用任何东西,仅仅添加一个配置就可以,所以使用sping boot非常适合构建微服务。

5.Springboot常用注解

  1. @SpringBootApplication 

    在系统启动类里面,都加入了此启动注解,此注解是个组合注解,包括@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan注解。

    @SpringBootConfiguration 继承至@Configuration,对于熟悉spring的开发者而言,此标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。

    @EnableAutoConfiguration 这个注解就是springboot能自动进行配置的魔法所在了。主要是通过此注解,能所有符合自动配置条件的bean的定义加载到spring容器中

    @ComponentScan 这个熟悉spring的开发者也应该熟悉,会扫描当前包及其子包下被@Component,@Controller,@Service,@Repository等注解标记的类并纳入到spring容器中进行管理。

  2. @Controller 和 @RestController 

    @RestController 是Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。而@Controller是用来创建处理http请求的对象,一般结合@RequestMapping使用。

  3. @RequestMapping 

    一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

    常用属性:value, method, consumes,produces, params,headers;
    value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);method: 指定请求的method类型, GET、POST、PUT、DELETE等;
    consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
    params: 指定request中必须包含某些参数值是,才让该方法处理。headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

    一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

    @GetMapping 等同于 @RequestMapping(method = RequestMethod.GET)
    @PostMapping 等同于 @RequestMapping(method = RequestMethod.POST)
    @PutMapping 等同于 @RequestMapping(method = RequestMethod.PUT)
    @DeleteMapping 等同于 @RequestMapping(method = RequestMethod.DELETE)
    @PatchMapping 等同于 @RequestMapping(method = RequestMethod.PATCH) 

  4. @RequestBody和@ResponseBody  

    @RequestBody注解允许request的参数在reqeust体中,常常结合前端POST请求,进行前后端交互。

    @ResponseBody注解支持将的参数在reqeust体中,通常返回json格式给前端。

  5. @PathVariable、@RequestParam 

    @PathVariable 
    当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

     

     

  6. @Component、@Service、@Repository 

    @Component 最普通的组件,可以被注入到spring容器进行管理
    @Repository 作用于持久层
    @Service 作用于业务逻辑层

posted @ 2021-02-23 18:16  JoePotter  阅读(132)  评论(0编辑  收藏  举报
``