# springboot_2002
**Repository Path**: manalay/springboot_2002
## Basic Information
- **Project Name**: springboot_2002
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 13
- **Created**: 2021-11-09
- **Last Updated**: 2024-12-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# springboot 学习
## springboot 是什么
https://spring.io/projects/spring-boot#overview
还是属于spring 体系的一个框架
但是不是新框架,而是一个集成了各种常用内容的框架。
在我们使用spring 的时候,不需要引入一堆的依赖。
相对于ssm集成内容,spring boot 省掉了大量的配置文件,省掉了tomcat
省掉了引入各种jar
## 一个简单的开始
新建一个文件夹,加入pom.xml
```
    4.0.0
    com.example
    myproject
    0.0.1-wangzeng
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
    
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
        
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
```
#### 这里关于spring boot 的一共两个部分
```
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
    
```
这个部分我们来看一看源码:
```
  4.0.0
  
    org.springframework.boot
    spring-boot-dependencies
    2.1.6.RELEASE
    ../../spring-boot-dependencies
  
  spring-boot-starter-parent
  pom
  Spring Boot Starter Parent
  Parent pom providing dependency and plugin management for applications
		built with Maven
  https://projects.spring.io/spring-boot/#/spring-boot-starter-parent
  
    UTF-8
    1.8
    @
    ${java.version}
    UTF-8
    ${java.version}
  
  
    
      
        true
        ${basedir}/src/main/resources
        
          **/application*.yml
          **/application*.yaml
          **/application*.properties
        
      
      
        ${basedir}/src/main/resources
        
          **/application*.yml
          **/application*.yaml
          **/application*.properties
        
      
    
    
      
        
          org.jetbrains.kotlin
          kotlin-maven-plugin
          ${kotlin.version}
          
            
              compile
              compile
              
                compile
              
            
            
              test-compile
              test-compile
              
                test-compile
              
            
          
          
            ${java.version}
            true
          
        
        
          maven-compiler-plugin
          
            true
          
        
        
          maven-failsafe-plugin
          
            
              
                integration-test
                verify
              
            
          
          
            ${project.build.outputDirectory}
          
        
        
          maven-jar-plugin
          
            
              
                ${start-class}
                true
              
            
          
        
        
          maven-war-plugin
          
            
              
                ${start-class}
                true
              
            
          
        
        
          org.codehaus.mojo
          exec-maven-plugin
          
            ${start-class}
          
        
        
          maven-resources-plugin
          
            
              ${resource.delimiter}
            
            false
          
        
        
          pl.project13.maven
          git-commit-id-plugin
          
            
              
                revision
              
            
          
          
            true
            yyyy-MM-dd'T'HH:mm:ssZ
            true
            ${project.build.outputDirectory}/git.properties
          
        
        
          org.springframework.boot
          spring-boot-maven-plugin
          
            
              repackage
              
                repackage
              
            
          
          
            ${start-class}
          
        
        
          maven-shade-plugin
          
            
              package
              
                shade
              
              
                
                  
                    META-INF/spring.handlers
                  
                  
                    META-INF/spring.factories
                  
                  
                    META-INF/spring.schemas
                  
                  
                  
                    ${start-class}
                  
                
              
            
          
          
            
              org.springframework.boot
              spring-boot-maven-plugin
              2.1.6.RELEASE
            
          
          
            true
            true
            
              
                *:*
                
                  META-INF/*.SF
                  META-INF/*.DSA
                  META-INF/*.RSA
                
              
            
          
        
        
          org.eclipse.m2e
          lifecycle-mapping
          1.0.0
          
            
              
                
                  
                    org.codehaus.mojo
                    flatten-maven-plugin
                    [1.0.0,)
                    
                      flatten
                    
                  
                  
                    
                  
                
                
                  
                    org.apache.maven.plugins
                    maven-checkstyle-plugin
                    [3.0.0,)
                    
                      check
                    
                  
                  
                    
                  
                
              
            
          
        
      
    
  
```
以上内容 
- 定义了 Java 编译版本,如上为 1.8。
- 使用 UTF-8 格式编码
- maven源文件和目标文件编译的版本
- 执行打包操作的配置
- 自动化的资源过滤
- 自动化的插件配置
可以看到,是省掉了大量的配置,并且指定了后续内容的版本,可以在依赖引入时省略
### 第二部分
```
  
            org.springframework.boot
            spring-boot-starter-web
  
```
源码:
```
  4.0.0
  
    org.springframework.boot
    spring-boot-starters
    2.1.6.RELEASE
  
  org.springframework.boot
  spring-boot-starter-web
  2.1.6.RELEASE
  Spring Boot Web Starter
  Starter for building web, including RESTful, applications using Spring
		MVC. Uses Tomcat as the default embedded container
  https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web
  
    Pivotal Software, Inc.
    https://spring.io
  
  
    
      Apache License, Version 2.0
      https://www.apache.org/licenses/LICENSE-2.0
    
  
  
    
      Pivotal
      info@pivotal.io
      Pivotal Software, Inc.
      https://www.spring.io
    
  
  
    scm:git:git://github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web
    scm:git:ssh://git@github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web
    https://github.com/spring-projects/spring-boot/spring-boot-starters/spring-boot-starter-web
  
  
    Github
    https://github.com/spring-projects/spring-boot/issues
  
  
    
      org.springframework.boot
      spring-boot-starter
      2.1.6.RELEASE
      compile
    
    
      org.springframework.boot
      spring-boot-starter-json
      2.1.6.RELEASE
      compile
    
    
      org.springframework.boot
      spring-boot-starter-tomcat
      2.1.6.RELEASE
      compile
    
    
      org.hibernate.validator
      hibernate-validator
      6.0.17.Final
      compile
    
    
      org.springframework
      spring-web
      5.1.8.RELEASE
      compile
    
    
      org.springframework
      spring-webmvc
      5.1.8.RELEASE
      compile
    
  
```
可以看到,指定了各种依赖
tomcat sprinMVC hibernate 验证,json 等等,一系列内容。
这两个内容,已经解决了我们项目大部分的配置管理问题。
## 创建一个项目
在建立了文件夹和pom文件之后,我们可以直接使用命令创建
> mvn package
这样我们就可以得到一个jar包了
### 使用idea 创建项目
打开后,可以选文件->new project - maven project
然后复制上面pom中的内容替换当前的pom就可以
## 编码
### 程序入口:
在所有子包之上,建立启动类,原因是启动类会扫描当前包以及当前包的子包
如果其它的代码不在当前包与子包范围,那就无法识别。
```
@SpringBootApplication
public class App{
	public static void main(String[] args) {
	    SpringApplication.run(App.class,args);
	}
}
```
代码并不复杂,一个注解,一个静态调用。
@SpringBootApplication
是一个复合注解,包括了很多内容:
```
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication { 
	@AliasFor(annotation = EnableAutoConfiguration.class)
	Class>[] exclude() default {}; 
	@AliasFor(annotation = EnableAutoConfiguration.class)
	String[] excludeName() default {}; 
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class>[] scanBasePackageClasses() default {};
}
```
可以看到里面包括了配置读取自动扫描等等注解内容。
也可以理解 为一个简化后的注解。作用是扫描子包,扫描配置文件等操作。
## Controller
控制器  作为MVC的C的部分
可以单独使用注解了
普通的Controller如果需要返回实体或json需要指定responseBody
所以以下案例,都以RestController来完成。