SpringBoot入门

创建第一个Spring Boot项目

1:新建一个Maven工程

2:在pom.xml下添加依赖

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

 

spring-boot-starter-parent

Mave通过继承这个项目来获得一些合理的默认配置如下

1:默认使用java 8

2:默认使用UTF-8编码

3:引用管理功能(版本管理)

4:资源过滤(Sensible resource filtering)

5:识别插件配置(Sensible plugin configuration(exec plugin,surefire,git commit ID,shade))

6:能够识别application.properties和application.yml

7:默认占位符${...}改为了@...@

 

spring-boot-starter-web

默认嵌套tomcat的web应用,默认端口8080,可通过配置文件application.yml更改

 

3:新建一个helloController

@RestController
@SpringBootApplication
public class HelloController {
    @RequestMapping("/hello")
    String hello() {
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(HelloController.class, args);
    }
}

@RestController = @Controller + @RequestBody

 

@SpringBootApplication = @Configuration @EnableAutoConfiguration @ComponentScan

 

1:@Configuration 注册bean到Spring容器管理

 

2:@EnableAutoConfiguration 自动配置Spring的上下文,自动根据你的类路径和你的 bean定义自动配置。

 

3:@ComponentScan 自动扫包下标有@Component注解及其子注解 @Repository;@Controller;@Service

 

4:运行helloControllerl类

浏览器url输入:http://localhost:8080/hello

 

5:查看结果

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值