Spring in Action(5th) --- part1(Foundation Spring) 1.Getting started with Spring

目的

只是为了记录看 Spring in Action 第五版这本书的一些笔记和心得。

章节大纲

  • Spring and Spring Boot essentials
  • Initializing a Spring project
  • An overview of the Spring landscape

什么是Spring

Spring通常提供一个容器,这个容器通常被认为是Spring application context(Spring应用的上下文环境),用于创建和管理应用组件。这些组件或者称为beans的东西, 被装配在Spring application context 中,类似于建造房子所需要的原材料般。

创建一个Spring项目

采用了自动化配置

Spring 项目结构

Spring in Action(5th) --- part1(Foundation Spring) 1.Getting started with Spring_第1张图片
以上是一个典型的maven 项目, 应用源码 放在 src/main/java 目录下,测试代码放在src/test/java 目录下,non-Java resources(其他代码) 应放在 src/main/resources 目录下。
在这个结构下,应该注意一下几个子项。

  • mvnw and mvnw.cmd—These are Maven wrapper scripts(Maven 封装的脚本). 通过使用这个脚本来构建项目就不必安装Maven.
  • pom.xml—This is the Maven build specification(Maven 构建项目说明书).
  • TacoCloudApplication.java—This is the Spring Boot main class that bootstraps the project. (SpringBoot 启动项目的入口)
  • application.properties—This file is initially empty, but offers a place where you
    can specify configuration properties.
  • static—This folder is where you can place any static content (images, stylesheets,
    JavaScript, and so forth) that you want to serve to the browser. It’s initially
    empty.
  • templates—This folder is where you’ll place template files that will be used to
    render content to the browser. It’s initially empty, but you’ll add a Thymeleaf
    template soon.
  • TacoCloudApplicationTests.java—This is a simple test class that ensures that
    the Spring application context loads successfully. You’ll add more tests to the
    mix as you develop the application.

The initial Maven build specification(初始状态的pom.xml)

jar 默认打包方式是jar,需要可以更改其他打包方式,如常用的war

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        
        <version>2.1.3.RELEASEversion>
        <relativePath/> 
    parent>
	
	....
    <dependencies>
    	
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

SpringInActionApplication.java 文件(一个启动文件)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringInActionApplication {
    // 这里就可以启动Springboot 项目
    public static void main(String[] args) {
        SpringApplication.run(SpringInActionApplication.class, args);
    }
}

需要关注的点:
@SpringBootApplication 表示当前类是SpringBoot 项目的启动类。@SpringBootApplication 是一个合成应用由其他三个注解进行合成。

  • @SpringBootConfiguration—Designates this class as a configuration class.
    Although there’s not much configuration in the class yet, you can add Javabased Spring Framework configuration to this class if you need to. This annotation is, in fact, a specialized form of the @Configuration annotation.
  • @EnableAutoConfiguration—Enables Spring Boot automatic configuration.
    We’ll talk more about autoconfiguration later. For now, know that this annotation tells Spring Boot to automatically configure any components that it thinks
    you’ll need.
  • @ComponentScan—Enables component scanning. This lets you declare other
    classes with annotations like @Component, @Controller, @Service, and others,
    to have Spring automatically discover them and register them as components in
    the Spring application context.

未完待续…

你可能感兴趣的:(读书笔记)