Spring Boot实战系列(1)项目构建

快速导航

首发于慕课网 , 欢迎关注Github获取更多内容)java

IntelliJ IDEA 中的Spring Initializr快速构建SpringBoot工程

intellig编辑器建立

  • 菜单栏中选择 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.xmlgithub

  • spring-boot-starter-web: Web项目模块依赖
  • spring-boot-starter-test: 测试模块依赖
  • spring-boot-maven-plugin: Maven构建项目插件
...
    <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>
...
复制代码

编写一个hello-springboot-程序

建立 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
    • 进到target目录能够看到有个 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命令启动 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

项目属性配置

后缀properties文件配置

SpringBoot默认使用 application.properties文件,位于/src/main/resources目录下,项目的默认启动端口是8080,下面对此进行修改springboot

  • server.port:修改端口号
  • server.context-path:设置url前缀 SpringBoot2.0版本如下采用此方法
  • server.servlet.context-path:设置url前缀SpringBoot2.0版本以上使用

application.propertiesapp

server.port=8081
server.servlet.context-path=/user
复制代码

后缀yml文件配置

还可使用.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.yml 用来存放公共配置,设置spring.profiles.active=dev,默认开发环境配置
  • 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实战系列

相关文章
相关标签/搜索