jdk1.8.0_301
apache-maven-3.8.1
idea(开发工具。社区版的也可以)
1.新建Maven项目
2.修改pom.xml文件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.4.RELEASE</version> </plugin> </plugins> </build>
3. 创建主程序类MainApplication
注意包结构:
1 package com.yka.boot; 2 3 import ch.qos.logback.core.db.DBHelper; 4 import com.yka.boot.bean.Pet; 5 import com.yka.boot.bean.User; 6 import com.yka.boot.config.MyConfig; 7 import org.springframework.boot.SpringApplication; 8 import org.springframework.boot.autoconfigure.SpringBootApplication; 9 import org.springframework.context.ConfigurableApplicationContext; 10 11 /** 12 * 主程序类 13 * @SpringBootApplication: 这是一个SpringBoot应用 14 * */ 15 @SpringBootApplication 16 public class MainApplication { 17 public static void main(String[] args) { 18 //1.返回ioc容器 19 ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args); 20 //2、查看容器内的组件 21 String[] names = run.getBeanDefinitionNames(); 22 for(String name : names){ 23 System.out.println(name); 24 } 25 } 26 }
4.编写输出Hello SpringBoot
在controller包下创建:HelloController.class
1 package com.yka.boot.controller; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 12 @RestController 13 public class HelloController { 14 15 @RequestMapping("/hello") 16 public String handle01(){ 17 return "Hello,Spring Boot2"+" 你好"; 18 } 19 }
5.在主程序类中运行,默认端口为8080
运行起来后在浏览器输入:localhost:8080/hello
第一个SpringBoot程序运行完成
1.在右侧maven选择clean和packge运行打包
2.打的jar包在当前项目target文件夹下
3.在终端/cmd直接运行
$ java -jar MySpringBoot-1.0-SNAPSHOT.jar
运行起来后浏览器输入:localhost:8080/hello