2021-04-20简要总结SpringBoot原理、目录

Maven创建标准的spring boot目录:
image.png
  • 其中,src/main/java目录存放我们编写的源码,springboot要求main()方法入口类必须放在根包,并在开头注解@SpringBootApplication,该注解包含了:
@SpringBootConfiguration
   '相当于@Configuration'
@EnableAutoConfiguration  启动自动配置
@AutoConfigurationPackage 启动自动扫描
   ' 相当于@ComponentScan'
  • 我们再来看看src/main/resources,springboot把自身配置application.yml、logback日志配置、返回前端所需要的static静态文件目录和模板目录templates都统一在一个文件夹,这样使得springboot项目结构比spring框架项目来的简单易读:

.yml格式是一种层级格式,和spring的.properties很容易转换(用两者之一进行配置都可以),而且yml省略了大量前缀:

# application.yml

spring:
  application:
    name: ${APP_NAME:unnamed}
  datasource:
    url: jdbc:hsqldb:file:testdb
    username: sa
    password:
    driver-class-name: org.hsqldb.jdbc.JDBCDriver
    hikari:
      auto-commit: false
      connection-timeout: 3000
      validation-timeout: 3000
      max-lifetime: 60000
      maximum-pool-size: 20
      minimum-idle: 1
  • 注意:配置时使用的类${DB_HOST:localhost} ,这种带有${...}的意思是:首先从环境变量查找DB_HOST,如果环境变量定义了,那么使用环境变量的值,否则,使用默认值localhost。

  • 然后,我们再来看看springboot的pom.xml:

<project ...>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itranswarp.learnjava</groupId>
    <artifactId>springboot-hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <java.version>11</java.version>
        <pebble.version>3.1.2</pebble.version>
    </properties>

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

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

        <!-- 集成Pebble View -->
        <dependency>
            <groupId>io.pebbletemplates</groupId>
            <artifactId>pebble-spring-boot-starter</artifactId>
            <version>${pebble.version}</version>
        </dependency>

        <!-- JDBC驱动 -->
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
    </dependencies>
</project>

可以看到,springboot配置了一个<parent>,表示从spring-boot-starter-parent继承,我们引入了依赖spring-boot-starter-web和spring-boot-starter-jdbc,它们分别引入了Spring MVC相关依赖和Spring JDBC相关依赖,无需指定版本号,因为引入的<parent>内已经引入了预制配置。
留个疑问:WebMvcConfigurer这个Bean的作用是什么?

还有,我们前面需要自己创建的DataSource、JdbcTemplate不用在配置类编写@bean了?那它们是如何被导入业务bean的?
答:因为AutoConfiguration。当我们引入spring-boot-starter-web时,启动springboot会自动扫描所有的XxxAutoConfiguration,如DataSourceAutoConfiguration:自动创建一个DataSource,其中配置项从application.yml的spring.datasource读取。
所以,AutoConfiguration功能通过自动扫描+条件装配实现的。

与SpringWeb的区别:

Springweb需要单独一个webapp目录,需要手动在web.xml配置dispatcherservlet,让servlet读取Appconfig来启动IoC容器,但springboot不用,而且项目结构更简单,.yml简化配置,AutoConfiguration可以在我们引入依赖之后,启动程序读取AppConfig时,自动扫描扫描所有的XxxAutoConfiguration,自动帮我们创建DataSource、JdbcTemplate、DispatcherServlet。所以直接用SpringBoot开发项目就好了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容