springBoot01 之HelloWorld

假装没事ソ 提交于 2020-11-23 08:51:09

1.创建一个maven项目。

2. 在pom.xml中引入spring-boot-start-parent,spring官方的解释叫什么stater poms,它可以提供dependency management,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了,后面可以看到。

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.4.1.RELEASE</version>

</parent>

3.在dependencies中加入依赖:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

 

4.加入插件,如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的。(我在测试的时候,如果不配置下面的plugin也是直接在Main中运行的)

<build>  

  <plugins>  

           <plugin>  

               <groupId>org.springframework.boot</groupId>  

              <artifactId>spring-boot-maven-plugin </artifactId>  

          </plugin>  

      </plugins>  

  </build> 

 

5.真正的程序开始啦,我们需要一个启动类,然后在启动类申明让spring boot自动给我们配置spring需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问hello world字样的例子

 

@SpringBootApplication

@ServletComponentScan

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class);

}

@RequestMapping("/")

public String index(){

return "hello,world";

}

}

 

其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan

@RestController返回json字符串的数据

运行我们的Application了,我们先介绍第一种运行方式。

之后打开浏览器输入地址:http://127.0.0.1:8080/ 就可以看到Hello world!了。

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!