1. Getting started with springboot

There are generally two ways to build a spring boot project: gradle and maven

maven way

  pom.xml

    • spring-boot-starter: Core modules, including auto-configuration support, logging, and YAML
    • spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito
    • spring-boot-starter-web: Web module

gradle way

  build.gradle

    compile("org.springframework.boot:spring-boot-starter-web:1.4.1.BUILD-SNAPSHOT")

The startup class of springboot

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

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

SpringBootApplication annotation source code

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    Class<?>[] exclude() default {};

}
@Configuration : Indicates that Application exists as a sprig configuration file
@EnableAutoConfiguration: Start spring boot's built-in automatic configuration
@ComponentScan : scan beans, the path is the package where the Application class is located and the sub-path under the package

controller service class

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

    @RequestMapping(value = "/{name}", method = RequestMethod.GET) public String sayWorld(@PathVariable("name") String name) { return "Hello " + name; } }

访问:http://localhost:8080/springboot/wonder

Spring Boot provides many "out-of-the-box" dependency modules, all named after spring-boot-starter-xx. Some commonly used modules are listed below.

spring-boot-starter- logging : Use Spring Boot's default logging framework, Logback.
spring -boot-starter- log4j : Add support for Log4j.
spring -boot-starter-web: Supports Web application development, including Tomcat and spring- mvc.
spring -boot-starter- tomcat : Use Spring Boot's default Tomcat as the application server.
spring -boot-starter- jetty : Use Jetty instead of the default Tomcat as the application server.
spring -boot-starter-test : Contains dependencies required for common tests, such as JUnit, Hamcrest, Mockito, and spring- test.
spring -boot-starter-aop : Contains spring- aop and AspectJ to support Aspect Oriented Programming (AOP).
spring-boot-starter-security :包含 spring-security。
spring -boot-starter- jdbc : Supports database access using JDBC.
spring -boot-starter- redis : Supports the use of Redis.
spring-boot-starter-data-mongodb :包含 spring-data-mongodb 来支持 MongoDB。
spring -boot-starter-data-jpa : Contains spring-data-jpa, spring- orm and Hibernate to support JPA.
spring -boot-starter-amqp : Support AMQP via spring-rabbit.
spring -boot-starter-actuator : Add production-friendly features such as performance metrics and monitoring.

 

Guess you like

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