美文网首页
一、SpringBoot基础

一、SpringBoot基础

作者: tiantianlyc | 来源:发表于2019-03-07 15:20 被阅读0次

    学习SpringCloud之前,需要对Maven以及SpringBoot有一定的了解,运用JDK1.8及以上。

    1 建立一个Maven的项目,目录结构参考如下:

    image.png

    2 pom文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.lyc.spring.cloud</groupId>
        <artifactId>springCloud1</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>spring-cloud-one</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.8.RELEASE</version>
            <relativePath/>
        </parent>
    
        <properties>
            <project.build.sourceEnncoding>UTF-8</project.build.sourceEnncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <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>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    3 properties脚本

    在项目的resources目录中可以看到是三个,运行时加载的是第一个。application.properties:

    spring.profiles.active=dev
    

    意为加载application.properties时,application-dev.properties生效,具体原理自行查看springBoot的profiles配置。application-dev.properties的代码如下:

    server.port=1111
    spring.application.name=hello
    debug=true
    
    # 默认只公开了/health和/info端点,要想暴露所有端点只需设置成星号即可
    management.endpoints.web.exposure.include=*
    # 显式启用/shutdown端点
    management.endpoint.shutdown.enabled=true
    # 要公开所有(已启用)网络端点除env端点之外
    management.endpoints.web.exposure.exclude=env
    # 设置管理服务的上下文路径,默认值为 “”
    management.server.servlet.context-path=
    # 设置管理服务的端口
    management.server.port=
    # 设置管理端点的基本路径,默认值为:actuator
    management.endpoints.web.base-path
    

    如上图所示,开通了debug模式,同时将系统性能管理的actuator模块儿也加再进来了

    4 启动类

    HelloApplication.java代码:

    package com.lyc.spring;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloApplication.class, args);
        }
    }
    

    5 对应的Controller里HelloController代码如下:

    package com.lyc.spring.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String index(){
            return "Hello World!";
        }
    
    

    6 在IDE里直接Run这个启动类,浏览器中查看:http://localhost:1111/hello即可看到浏览器里出现了Hello World的字样:

    image.png

    相关文章

      网友评论

          本文标题:一、SpringBoot基础

          本文链接:https://www.haomeiwen.com/subject/nvvcpqtx.html