一、springboot入门

构建spring boot工程一般采用两种方式 gradle 、maven

maven方式

  pom.xml

    • spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
    • spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
    • spring-boot-starter-web:Web模块

gradle方式

  build.gradle

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

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注解源码

@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 : 表示Application作为sprig配置文件存在 
@EnableAutoConfiguration: 启动spring boot内置的自动配置 
@ComponentScan : 扫描bean,路径为Application类所在package以及package下的子路径

controller服务类

@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提供了很多”开箱即用“的依赖模块,都是以spring-boot-starter-xx作为命名的。下面列举一些常用的模块。

spring-boot-starter-logging :使用 Spring Boot 默认的日志框架 Logback。
spring-boot-starter-log4j :添加 Log4j 的支持。
spring-boot-starter-web :支持 Web 应用开发,包含 Tomcat 和 spring-mvc。
spring-boot-starter-tomcat :使用 Spring Boot 默认的 Tomcat 作为应用服务器。
spring-boot-starter-jetty :使用 Jetty 而不是默认的 Tomcat 作为应用服务器。
spring-boot-starter-test :包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。
spring-boot-starter-aop :包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。
spring-boot-starter-security :包含 spring-security。
spring-boot-starter-jdbc :支持使用 JDBC 访问数据库。
spring-boot-starter-redis :支持使用 Redis。
spring-boot-starter-data-mongodb :包含 spring-data-mongodb 来支持 MongoDB。
spring-boot-starter-data-jpa :包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。
spring-boot-starter-amqp :通过 spring-rabbit 支持 AMQP。
spring-boot-starter-actuator : 添加适用于生产环境的功能,如性能指标和监测等功能。

猜你喜欢

转载自www.cnblogs.com/soul-wonder/p/8966717.html