一、新建项目选择 Spring Initializr ,右边的配置能够选择jdk的版本,其余选项不变java
二、设置项目的基本信息web
三、选择web模块spring
此处能够看到spring boot 的版本为:1.5.3浏览器
四、设置项目名称tomcat
五、修改maven仓库地址为阿里云提供的仓库。app
<!--设置maven 仓库为阿里云提供的maven仓库 国外的maven仓库慢--> <repositories> <repository> <id>nexus-aliyun</id> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </repository> </repositories>
六、启动项目maven
spring boot 内置的tomcat默认端口是8080;ide
在浏览器访问:http://localhost:8080工具
七、建立一个控制器 测试
@RestController public class HelloApiController { @GetMapping(value = "hello") public String hello(){ return "hello spring boot"; } }
在浏览器输入:http://localhost:8080/hello
返回:hello spring boot
八、打包启动
使用idea的maven工具来打包项目为jar
进入 jar所在的目录执行:java -jar helloword-0.0.1-SNAPSHOT.jar
在浏览器输入:http://localhost:8080/hello
一切都是正常的。
第一个spring boot 项目已经建立完成啦……
在resource目录下新建一个application.yml文件,同时删除application.properties文件
一、application.yml
server: port: 8888 student: name: 张三 age: 12 address: 张家沟
这里注意文件编码统一为utf-8
二、现金Student 类
@Component//加入到spring 容器中 @ConfigurationProperties(prefix = "student") public class Student { private String name; private Integer age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } }
若是属性名称时驼峰命名的话,在配置文件中使用中划线链接单词。
三、在控制器中使用:
@RestController public class HelloApiController { Logger logger= LoggerFactory.getLogger(HelloApiController.class); @Autowired private Student student; @GetMapping(value = "hello") public String hello(){ logger.info("你好1"); return "hello spring boot" +student.toString(); } }
四、访问接口:
一、新建application-dev.yml
server: port: 8081 student: name: 张三-开发环境 age: 12 address: 张家沟
这里将开发环境的端口设置成了 8081
二、新建application-prod.yml
server: port: 8082 student: name: 张三-生产环境 age: 12 address: 张家沟
这里将生产环境的端口设置成了 8082
三、修改application.yml文件
设置为开发环境:
spring: profiles: active: dev
启动项目 http://localhost:8081/hello
设置为生产环境:
spring: profiles: active: prod
启动项目 http://localhost:8082/hello
上面配置分别配置了生产环境和测试环境的yml文件,而后在application.yml文件中经过配置
spring: profiles: active: prod
来启动项目,若是要同时启动生产环境和测试环境,则能够经过jar方式来启动:
进入 jar所在的目录执行:
启动生产环境:java -jar helloword-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
启动测试环境:java -jar helloword-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
只要端口不一样就能够。
设置为后台部署:
java -jar helloword-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod &
nohup java -jar yourapp.jar &
[root@localhost spring]# java -jar eureka-client-0.0.1-SNAPSHOT.jar & [1] 2384 [root@localhost spring]# nohup java -jar eureka-client-0.0.1-SNAPSHOT.jar & [1] 2366 [root@localhost spring]# nohup: 忽略输入并把输出追加到"nohup.out"
使用 tail -f nohup.out能够查看控制台日志
启动成功后会返回当前的进程id
查看当前java应用有哪些在运行中:
[root@localhost spring]# ps -ef |grep java root 2384 2288 9 09:40 pts/0 00:00:19 java -jar eureka-client-0.0.1-SNAPSHOT.jar root 2422 2288 0 09:43 pts/0 00:00:00 grep --color=auto java
中止进程:kill -9 进程id