springboot入门

一、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 : 添加适用于生产环境的功能,如性能指标和监测等功能。

原文来源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值