springboot(1) Build the first SpringBoot project

title

The design purpose of spring boot is to simplify development as an example, enable various automatic assembly, and introduce relevant dependencies to quickly build a web project. It takes the convention of building a production-ready application over configuration.


construction engineering

Open Idea-> new Project -> Spring Initializr -> fill in group (company domain name flashback), artifact (project name) -> hook on web (open web function) -> click next.


Project directory

After the project is created, the directory structure of the project is as follows:

 - src
    -main
        -java
            -package
                -SpringbootApplication
        -resouces
            - statics
            - templates
            - application.yml
    -test
 - pom
  • The pom file is the basic dependency management file
  • resources resource file
    • statics static resources templates template resources
    • application.yml configuration file
    • application.yml configuration file
  • The entry of the SpringbootApplication program

pom.xml dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fubin</groupId>
    <artifactId>springboot1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot1</name>
    <description>Demo project for Spring Boot</description>

    <!--继承springboot的父包-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>

    <!--依赖的jar包坐标-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!--maven仓库-->
    <repositories>
        <repository>
            <id>maven-central</id>
            <name>maven-central</name>
            <url>http://localhost:8081/repository/maven-central/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>Releases</name>
            <url>http://localhost:8081/repository/nexus-releases</url>
        </repository>
    </distributionManagement>
</project>

Among them, spring-boot-starter-web not only includes spring-boot-starter, but also automatically opens the web function.

Demo

Create a controller:

package com.fubin.springboot1.web.simpleEg;

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

@RestController
public class HelloController {

    @RequestMapping(value = "/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}

Start the main method of SpringbootFirstApplication, open the browser localhost:8080, and the browser displays:

Greetings from Spring Boot!

The magic:

  • You didn't do any web.xml configuration.
  • You didn't do any configuration of sping mvc; springboot did it for you.
  • You don't have tomcat configured; springboot embeds tomcat.

Start the springboot method

cd to the project home directory:

mvn clean  
mvn package  编译项目的jar
  • mvn spring-boot: run start
  • cd to the target directory, java -jar project.jar

What beans does springboot inject for us at startup

@SpringBootApplication
public class SpringbootFirstApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootFirstApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }
}
Let’s inspect the beans provided by Spring Boot: 
basicErrorController 
beanNameHandlerMapping 
beanNameViewResolver 
characterEncodingFilter 
commandLineRunner 
conventionErrorViewResolver 
defaultServletHandlerMapping 
defaultViewResolver 
dispatcherServlet 
dispatcherServletRegistration 
duplicateServerPropertiesDetector 
embeddedServletContainerCustomizerBeanPostProcessor 
error 
errorAttributes 
errorPageCustomizer 
errorPageRegistrarBeanPostProcessor
…. 
….

When the program starts, springboot automatically injects 40-50 beans.

unit test

Open annotations via @RunWith() @SpringBootTest:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {

    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    }

    @Test
    public void getHello() throws Exception {
        ResponseEntity<String> response = template.getForEntity(base.toString(),
                String.class);
        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
    }
}

Running it will first start the sprigboot project, and then test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648544&siteId=291194637