Spring Boot基础:Spring Boot简介与快速搭建(1)

1. Spring Boot简介

  Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的创建、运行、调试、部署等。

  Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。

2. Spring Boot快速搭建

2.1 Maven项目构建

  Maven构建网址:http://start.spring.io/

  Spring Boot基础结构:

    ◊ src/main/java:程序开发以及主程序入口

    ◊ src/main/resources:配置文件

    ◊ src/test/java:测试程序

  Spring Boot建议目录结构:

com
  +- example
    +- myproject
      +- Application.java
      |
      +- domain
      |  +- Customer.java
      |  +- CustomerRepository.java
      |
      +- service
      |  +- CustomerService.java
      |
      +- controller
      |  +- CustomerController.java
      |

其中:

  (1)Application.java:建议放到跟目录下面,主要用于做一些框架配置。

  (2)domain目录:主要用于实体(Entity)与数据访问层(Repository)

  (3)service:主要是业务类代码

  (4)controller:负责页面访问控制

2.2 项目结构及说明

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>libing</groupId>
    <artifactId>com-helloworld-api</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>com-helloworld-api Maven Webapp</name>
    <url>http://maven.apache.org</url>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <finalName>com-helloworld-api</finalName>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
pom.xml

其中:

  pom中parent设为 spring-boot-starter-parent ,建议使用最新的 RELEASE 版本。

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

package com.libing.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {

        SpringApplication.run(HelloWorldApplication.class, args);

    }

}
HelloWorldApplication.java

其中:

  @SpringBootApplication:等同于默认的属性 @Configuration,@EnableAutoConfiguration, @ComponentScan。

    @Configuration、@ComponentScan是spring框架的语法,用于代码方式创建配置信息和扫描包。

    在根包启动类添加@ComponentScan注解而不需要添加任何参数,Spring Boot会在根包下面搜索注有@Component, @Service,@Repository, @Controller注解的所有类,并将他们注册为Spring Beans。

    @EnableAutoConfiguration是spring boot语法,表示将使用自动配置。

  @EnableAutoConfiguration:根据pom配置(具体的依赖)来判断这是一个什么应用,并创建相应的环境。

  SpringApplication:启动管理器,用于从main方法启动Spring应用的类。

    默认执行以下步骤:

    (1)创建一个合适的ApplicationContext实例 (取决于classpath)。

    (2)注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。

    (3)刷新application context,加载所有单例beans。

    (4)激活所有CommandLineRunner beans。

    默认,直接使用SpringApplication 的静态方法run()。可以创建实例,并自行配置需要的设置。

package com.libing.helloworld.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/helloworld")
public class HelloWorldController {

    @GetMapping
    public String Index() {
        return "Hello World!";
    }
    
}
HelloWorldController.java

其中:

  @RestController:controller中的方法都以json格式输出

server.port=9000
application.properties

2.3 修改启动端口

  (1)通过实现EmbeddedServletContainerCustomizer接口

package com.libing.helloworld;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class HelloWorldApplication extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(HelloWorldApplication.class).web(true).run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(HelloWorldApplication.class);
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(9000);
    }

}

  (2)设置SpringApplication.run参数args

SpringApplication.run(HelloWorldApplication.class, "--server.port=9000");

  (3)application.properties配置文件

server.port=9000
原文地址:https://www.cnblogs.com/libingql/p/7346686.html