一、HelloWorld
1.系统要求
我的电脑配置是:
jdk:8
idea:2019.3.3
maven:3.3.9
2.HelloWorld项目
2.1maven设置
2.2创建maven工程
创建好的文件如下:
2.3引入依赖
依赖在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>
2.4创建主程序
在Java里创建主程序HelloWorld
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//这三点和最上面的相等
//如果扫描不到包,那么用@SpringBootApplication(scanBasePackage='com.xxx')或者@ComponentScan来指定包路径
//这是一个spring应用
public class HelloWorld {
public static void main(String[] args) {
//alt+enter自动返回IOC容器
ConfigurableApplicationContext run = SpringApplication.run(HelloWorld.class);
//查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name :names){
System.out.println(name);
}
}
}
2.5编写controller层业务
package com.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle(){
return "hello!!!!!!";
}
}
2.6测试
1.运行主程序中的main方法
2.在网页网址栏输入localhost:8080/hello
默认是8080,如果想要更改访问端口,创建application.properties文件,写:
server.port=8888
运行成功,显示端口8080
2.7简化部署
1.在右边侧边栏选择maven->package
2.把项目打成jar包,在目标服务器执行
3.在maven执行打包后的target文件夹下打开cmd命令窗口输入 java -jar <打包文件名> 即可在cmd命令窗口中启动spring boot项目,访问方式与idea中相同
4.有的时候cmd命令启动不了springboot项目,可能是因为在属性中开启了【快速编辑模式】,取消方法:右击cmd控制台的窗口上方标题区域打开【选项】取消勾选【快去编辑模式】