Construction of a Spring Boot project

table of Contents

 

Spring Boot Overview

What is Spring Boot

With the popularity of dynamic languages, java development is particularly heavy, written many profiles, a large low development efficiency, complex deployment processes and third-party technology integration difficult.

Spring Boot came into being under the above environment, the purpose of Spring Boot is designed to simplify the Spring application to build , develop , debug , deploy a series of problems, we can clearly feel the complicated configuration at the time of application development using Spring, Spring Boot JavaConfig full advantage of the patterns and the philosophy of "convention over configuration", can greatly simplify Web-based applications and Spring MVC REST service development, and faster and easier to build Spring applications.

The core features of Spring Boot

  • It can operate as a jar package, run a SpringBoot project only needs to be run by java -jar xx.jar.
  • Built-Servlet container, Spring Boot can choose Tomcat, Jetty, so we do not need to deploy the project in the form of war package
  • Maven simplified configuration, Spring Boot provides a series of starter pom Maven to simplify the load dependent.
  • Spring Boot will be based in the classpath jar packages, classes, classes for the jar package automatically configured bean. This will greatly reduce the configuration we want to use.
  • Spring Boot offers based on http, ssh, telnet project run-time monitoring.
  • It is achieved without the aid of code generation, but the condition is achieved by annotation. It also features new in Spring 4.x of. It does not require any xml configuration can be achieved all the configuration of Spring.  

Spring Boot project to build

By creating a new Spring Spring Boot application becomes very easy, just a few simple steps you can create an application.

When you create a project using Spring Initializr (IDEA), created the project is structured as follows

Copy the code
- src 
    -main 
        -java 
            -package 
                # startup class 
                -SpringbootApplication     
        -resouces 
            # store static resources such as js / css / images, etc. 
            - statics 
            # store html template files 
            - Templates 
            # main configuration file, SpringBoot start time will automatically load application.yml /application.properties         
            - application.yml 
    # test file storage directory         
    -test 
 # pom.xml file Maven build the foundation, which contains the information that we rely on and Plugin of JAR 
- pom.xml
Copy the code

pom.xml dependence

Copy the code
<?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>

    <name>spring-boot-web</name>
    <description>Demo project for Spring Boot</description>

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

    <groupId>com.winner</groupId>
    <artifactId>spring-boot-web</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <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>
        <!--默认内嵌Tomcat容器,可以根据需要进行替换-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--用来进行热部署的插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

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

</project>
Copy the code

Main function entry

Remember not to have multiple main function of a project, or in the package when the spring-boot-maven-plugin will not find the main function!

Copy the code
package com.winner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * 主函数启动类
 * @author winner_0715
 * @date 2018/11/28
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.out.println(" springApplication run !");
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            System.out.println("SpringBoot默认为我们提供的Bean:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }
}
Copy the code

Write our Controller

Copy the code
package com.winner.web;

import com.winner.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author winner_0715
 * @description  HelloController
 * @date 2018/11/28
 */
@RestController
public class HelloController {

    @RequestMapping("/hello/string")
    public String helloString(){
        return "hello world";
    }

    /**
     * 因为使用了@RestController
     * 所以不需要加@ResponseBody注解
     * @RestController=@Controller+@ResponseBody
     * @return
     */
    @RequestMapping("/hello/model")
    public User helloModel(){
        return new User.Builder()
                .userName("name")
                .email("email")
                .build();
    }
}
Copy the code

Direct operation start classes, or may be " mvn Spring-Boot: RUN " on the command line to start the application. It will launch an embedded Tomcat server running on port 8080. Visit http: // localhost: 8080 you can see the display "Hello World!" On the page.

In addition, in the POM file to add plug-ins.

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

After adding a plug-in, when running "mvn clean package" to package, will be packaged into a jar package can run directly using " the Java -jar " command can be run directly.

Spring Boot recognized master configuration file

application.properties

Can be found from the start log, SpringBoot default port is 8080, if the port is occupied we can be resolved by modifying the configuration file!

Tomcat started on port(s): 8080 (http) with context path ''

Modify the default configuration (also hinted, is to force, equipped with hot deployment, then modify the configuration file will restart to take effect)

# Modify the default port 8080 
the server.port = 8888 
# define context path 
server.servlet.context-path = / spring-boot -web

Look at the console output at this time

Tomcat started on port(s): 8888 (http) with context path '/spring-boot-web'

In this case access will need to bring the item name!

related information

Spring Boot的spring-boot-starter

Spring Boot provides many " out of the box " dependent modules, which are in accordance with the spring-boot-starter- {xx} named manner. Here are some common modules.

  • spring-boot-starter-logging: Spring Boot using default logging framework logback.
  • spring-boot-starter-log4j: log4j add support of.
  • spring-boot-starter-web: Support Web application development, including Tomcat and spring-mvc.
  • spring-boot-starter-tomcat: Spring Boot using the default Tomcat as the application server.
  • spring-boot-starter-jetty: use Jetty as an application server.
  • spring-boot-starter-test: required dependencies include commonly used tests, such as JUnit, Hamcrest, Mockito spring-test, and the like.
  • spring-boot-starter-aop: spring-aop comprising support and AspectJ Aspect Oriented Programming (AOP).
  • spring-boot-starter-security :包含 spring-security。
  • spring-boot-starter-jdbc: support the use of JDBC to access the database.
  • spring-boot-starter-redis: support the use Redis.
  • spring-boot-starter-data-mongodb: comprising a spring-data-mongodb support MongoDB.
  • spring-boot-starter-data-jpa: comprising a spring-data-jpa, spring-orm and to support Hibernate JPA.
  • spring-boot-starter-amqp: supports AMQP by spring-rabbit.
  • spring-boot-starter-actuator: add functionality suitable production environment, such as performance monitoring and other functions.

Java Config Automatic Configuration

Spring Boot recommended configurations based on Java Config (ie notes), rather than the traditional XML. For example, @ Configuration, @ Bean, @ EnableAutoConfiguration, @ CompomentScan, @ PropertySource, @ Repository, @ Service, @ RestController and so on.

 

Spring Boot Overview

What is Spring Boot

With the popularity of dynamic languages, java development is particularly heavy, written many profiles, a large low development efficiency, complex deployment processes and third-party technology integration difficult.

Spring Boot came into being under the above environment, the purpose of Spring Boot is designed to simplify the Spring application to build , develop , debug , deploy a series of problems, we can clearly feel the complicated configuration at the time of application development using Spring, Spring Boot JavaConfig full advantage of the patterns and the philosophy of "convention over configuration", can greatly simplify Web-based applications and Spring MVC REST service development, and faster and easier to build Spring applications.

The core features of Spring Boot

  • It can operate as a jar package, run a SpringBoot project only needs to be run by java -jar xx.jar.
  • Built-Servlet container, Spring Boot can choose Tomcat, Jetty, so we do not need to deploy the project in the form of war package
  • Maven simplified configuration, Spring Boot provides a series of starter pom Maven to simplify the load dependent.
  • Spring Boot will be based in the classpath jar packages, classes, classes for the jar package automatically configured bean. This will greatly reduce the configuration we want to use.
  • Spring Boot offers based on http, ssh, telnet project run-time monitoring.
  • It is achieved without the aid of code generation, but the condition is achieved by annotation. It also features new in Spring 4.x of. It does not require any xml configuration can be achieved all the configuration of Spring.  

Spring Boot project to build

By creating a new Spring Spring Boot application becomes very easy, just a few simple steps you can create an application.

When you create a project using Spring Initializr (IDEA), created the project is structured as follows

Copy the code
- src 
    -main 
        -java 
            -package 
                # startup class 
                -SpringbootApplication     
        -resouces 
            # store static resources such as js / css / images, etc. 
            - statics 
            # store html template files 
            - Templates 
            # main configuration file, SpringBoot start time will automatically load application.yml /application.properties         
            - application.yml 
    # test file storage directory         
    -test 
 # pom.xml file Maven build the foundation, which contains the information that we rely on and Plugin of JAR 
- pom.xml
Copy the code

pom.xml dependence

Copy the code
<?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>

    <name>spring-boot-web</name>
    <description>Demo project for Spring Boot</description>

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

    <groupId>com.winner</groupId>
    <artifactId>spring-boot-web</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <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>
        <!--默认内嵌Tomcat容器,可以根据需要进行替换-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--用来进行热部署的插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

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

</project>
Copy the code

Main function entry

Remember not to have multiple main function of a project, or in the package when the spring-boot-maven-plugin will not find the main function!

Copy the code
package com.winner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * 主函数启动类
 * @author winner_0715
 * @date 2018/11/28
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.out.println(" springApplication run !");
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            System.out.println("SpringBoot默认为我们提供的Bean:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }
}
Copy the code

Write our Controller

Copy the code
package com.winner.web;

import com.winner.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author winner_0715
 * @description  HelloController
 * @date 2018/11/28
 */
@RestController
public class HelloController {

    @RequestMapping("/hello/string")
    public String helloString(){
        return "hello world";
    }

    /**
     * 因为使用了@RestController
     * 所以不需要加@ResponseBody注解
     * @RestController=@Controller+@ResponseBody
     * @return
     */
    @RequestMapping("/hello/model")
    public User helloModel(){
        return new User.Builder()
                .userName("name")
                .email("email")
                .build();
    }
}
Copy the code

Direct operation start classes, or may be " mvn Spring-Boot: RUN " on the command line to start the application. It will launch an embedded Tomcat server running on port 8080. Visit http: // localhost: 8080 you can see the display "Hello World!" On the page.

In addition, in the POM file to add plug-ins.

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

After adding a plug-in, when running "mvn clean package" to package, will be packaged into a jar package can run directly using " the Java -jar " command can be run directly.

Spring Boot recognized master configuration file

application.properties

Can be found from the start log, SpringBoot default port is 8080, if the port is occupied we can be resolved by modifying the configuration file!

Tomcat started on port(s): 8080 (http) with context path ''

Modify the default configuration (also hinted, is to force, equipped with hot deployment, then modify the configuration file will restart to take effect)

# Modify the default port 8080 
the server.port = 8888 
# define context path 
server.servlet.context-path = / spring-boot -web

Look at the console output at this time

Tomcat started on port(s): 8888 (http) with context path '/spring-boot-web'

In this case access will need to bring the item name!

related information

Spring Boot的spring-boot-starter

Spring Boot provides many " out of the box " dependent modules, which are in accordance with the spring-boot-starter- {xx} named manner. Here are some common modules.

  • spring-boot-starter-logging: Spring Boot using default logging framework logback.
  • spring-boot-starter-log4j: log4j add support of.
  • spring-boot-starter-web: Support Web application development, including Tomcat and spring-mvc.
  • spring-boot-starter-tomcat: Spring Boot using the default Tomcat as the application server.
  • spring-boot-starter-jetty: use Jetty as an application server.
  • spring-boot-starter-test: required dependencies include commonly used tests, such as JUnit, Hamcrest, Mockito spring-test, and the like.
  • spring-boot-starter-aop: spring-aop comprising support and AspectJ Aspect Oriented Programming (AOP).
  • spring-boot-starter-security :包含 spring-security。
  • spring-boot-starter-jdbc: support the use of JDBC to access the database.
  • spring-boot-starter-redis: support the use Redis.
  • spring-boot-starter-data-mongodb: comprising a spring-data-mongodb support MongoDB.
  • spring-boot-starter-data-jpa: comprising a spring-data-jpa, spring-orm and to support Hibernate JPA.
  • spring-boot-starter-amqp: supports AMQP by spring-rabbit.
  • spring-boot-starter-actuator: add functionality suitable production environment, such as performance monitoring and other functions.

Java Config Automatic Configuration

Spring Boot recommended configurations based on Java Config (ie notes), rather than the traditional XML. For example, @ Configuration, @ Bean, @ EnableAutoConfiguration, @ CompomentScan, @ PropertySource, @ Repository, @ Service, @ RestController and so on.

Guess you like

Origin www.cnblogs.com/topgoking/p/11490785.html