[运行程序]
三种方式启动项目 [more]
首发于慕课网 , 欢迎关注Github获取更多内容)java
File
=> New
=> Project
,能够看到下图弹出建立窗口,左侧默认指向Spring Initializr,右侧Choose Initializr Service Url 默认指向 start.spring.io/ ,这是Spring官方提供的,在这里也能够建立工程项目。Next
进入下一步,Group: 本身能够根据本身的喜好命名,本身的名字等均可以;Name:咱们这里设置为user;Type:选择Maven;更多参数设置参考如下图片示例Next
进入下一步,能够看到不少Spring的组件供咱们选择,这里只选择Web。Next
进入下步,选择项目的存储位置,点击Finish完成整个工程的构建经过以上步骤完成了项目的建立,下面让咱们来看下基本的项目结构:git
├── src 业务代码目录 ├── main ├── java 程序入口 ... ├── resources 资源配置文件 ... ├── test 单元测试目录 ├── ├── pom.xml
pom.xml
github
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
...
复制代码
建立 HelloControllerl
类,内容以下web
package com.angelo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say() {
return "Hello SpringBoot!!!";
}
}
复制代码
启动有多种方式,让咱们分别看下spring
Run 'UserApplication'
mvn spring-boot:run
mvn install
user-0.0.1-SNAPSHOT.jar
文件$ cd target
$ ls
classes maven-archiver test-classes
generated-sources maven-status user-0.0.1-SNAPSHOT.jar
generated-test-sources surefire-reports user-0.0.1-SNAPSHOT.jar.original
复制代码
java -jar user-0.0.1-SNAPSHOT.jar
打开浏览器访问http://localhost:8080/hello
,能够看到页面输出Hello SpringBoot!!!
浏览器
源码地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-1tomcat
SpringBoot默认使用 application.properties
文件,位于/src/main/resources
目录下,项目的默认启动端口是8080,下面对此进行修改springboot
application.propertiesapp
server.port=8081
server.servlet.context-path=/user
复制代码
还可使用.yml
文件写,优势在于更简洁,推荐此格式maven
删除application.properties
文件,新建application.yml
文件
application.yml
server:
port: 8081
servlet:
context-path: /user
复制代码
经过以上配置在重启咱们的项目,能够看到如下提示,Tomcat started on port(s): 8081 (http) with context path '/user'
2018-10-21 16:31:51.003 INFO 14696 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '/user'
2018-10-21 16:31:51.008 INFO 14696 --- [ main] com.angelo.UserApplication : Started UserApplication in 2.999 seconds (JVM running for 4.054)
复制代码
在浏览器运行此次须要加上咱们的前缀进行访问 http://localhost:8081/user/hello
项目开发中一般还会须要自定义一些配置文件,格式和上面同样,让咱们来设置一些访问该网站的用户信息
各参数之间也可相互引用,例以下面info经过${}在括号里引用了user.age
application.yml
server:
port: 8081
servlet:
context-path: /user
user:
nickName: 张三
age: 18
info: 我今年${user.age}。
复制代码
/src/main/resources
目录下新建UserProperties.java
文件
UserProperties.java
package com.angelo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "user") // 获取前缀是user的配置
public class UserProperties {
private String nickName;
private String info;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
复制代码
修改HelloController.java
package com.angelo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class HelloController {
@Autowired
private UserProperties userProperties;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say() {
return "我是 " + userProperties.getNickName() + userProperties.getInfo();
}
}
复制代码
启动,浏览器运行http://localhost:8081/user/hello
源码地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-2
一个项目在开发中,至少会有两个环境:开发环境、生产环境分别来管理数据连接地址,接口请求地址等,那么对于这种多环境配置咱们该怎么操做呢?
SpringBoot中多环境配置须要知足 application-{profile}.yml
格式,例如咱们本次实例中即将要介绍的:
application-dev.yml
:开发环境server:
port: 8080
servlet:
context-path: /user
user:
nickName: 张三
age: 18
info: 我今年${user.age},目前访问的是dev环境。
复制代码
application-pro.yml
:生产环境server:
port: 8081
servlet:
context-path: /user
user:
nickName: 李四
age: 19
info: 我今年${user.age},目前访问的是pro环境。
复制代码
至于哪一个文件会被加载,须要对spring.profiles.active
属性进行设置。 修改application.yml
文件,会默认加载application-dev.yml
配置文件
spring:
profiles:
active: dev
复制代码
经过java -jar
的方式启动
进入项目根目录,执行命令进行编译 mvn install
开启了两个终端分别执行命令:
开启dev环境 java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
开启pro环境java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
如下为两个终端的启动信息,能够看到分别开启了8080端口、8081端口
浏览器端一样开启两个窗口分别执行:
分别返回不一样环境对应的配置信息,
经过以上实例,能够总结出如下3点:
application-{profile}.yml
配置不一样环境的内容java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
这种方式激活当前须要运行的环境信息源码地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-3
做者:五月君
连接:www.imooc.com/article/255…
来源:慕课网
Github: Spring Boot实战系列