快速入门1springboot

快速入门1springboot

一、第一个springBoot程序

1.创建项目:File-New Project:
选择 Spring Initializr:

1.jpg

2.jpg

3.jpg

推荐使用阿里云的maven镜像:

修改maven中的配置文件settings.xml:

<mirrors>
     <mirror>
          <id>nexus-aliyun</id>
          <mirrorOf>*</mirrorOf>
          <name>Nexus aliyun</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public</url>
     </mirror>
</mirrors>

2.pom.xml:

<?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.hcx</groupId>
    <artifactId>girl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>girl</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <!--web项目必须引入的依赖-->
        <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>
</project>

3.项目结构:

4.jpg

GirlApplication:启动文件,必须有@SpringBootApplication注解

package com.hcx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GirlApplication {

    public static void main(String[] args) {
        SpringApplication.run(GirlApplication.class, args);
    }
}

application.properties:配置文件

项目启动的三种方法:

 1、直接在IDEA中Application.class中右击 run;
 2、命令行在项目根目录下运行 mvn spring-boot:run (第一次运行时会现在支持文  件);
 3、命令行在项目根目录下运行 mvn install进行编译,会产生一个target临时文件夹,里面有一个本项目编译的jar包,通过 java -jar jar包名 的命令可以直接运行;

4.编写类HelloController:

@RestController
public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return "Hello Spring Boot";
    }
}

启动程序访问:http://127.0.0.1:8080/hello

5.jpg

二、属性配置

项目的配置文件application.properties:

server.port=8081
server.context-path=/girl

访问:http://127.0.0.1:8081/girl/hello

默认配置文件为.properties,单推荐使用.yml文件:

server:
  port: 8081
  context-path: /girl

注意:.yml的语法:每个属性后面必须有空格

例:选出cupSize为B,age为18的:

application.yml:

server:
  port: 8081
cupSize: B
age: 18

HelloController:

@RestController
public class HelloController {

    @Value("${cupSize}")
    private String cupSize;

    @Value("${age}")
    private Integer age;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return cupSize + age;
    }
}

注:在配置文件中不需要区分类型,只有在注入的时候标明类型即可

6.jpg

还可在配置中再使用配置:

application.yml:

server:
  port: 8081
cupSize: B
age: 18
content: "cupSize: ${cupSize}, age: ${age}"

HelloController:

@RestController
public class HelloController {

    @Value("${cupSize}")
    private String cupSize;

    @Value("${age}")
    private Integer age;

    @Value("${content}")
    private String content;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return content;
    }
}

7.jpg

当属性较多时:

新建一个属性类:

@Component //如果该类需要被注入则添加该注解
@ConfigurationProperties(prefix = "girl")//获取前置为girl的配置
public class GirlProperties {

    private String cupSize;
    private Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

HelloController:

@RestController
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return girlProperties.getCupSize();
    }
}

当生产环境与开发环境的配置不同时:可建多个配置文件:

application-dev.yml:

server:
  port: 8080
girl:
  cupSize: B
  age: 18

application-prod.yml:

server:
  port: 8081
girl:
  cupSize: F
  age: 18

application.yml:使用dev中的配置

spring:
  profiles:
    active: dev

也可用不同的启动方式同时启动两种配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值