Spring Cloud_2_Spring Boot简介与配置

Spring Boot简介与配置

  • SpringBoot简介
  • SpringBoot使用
  • Eeclips创建SpringBoot项目
  • Idea创建SpringBoot项目

1、SpringBoot简介

  • Spring Cloud是基于SpringBoot搭建的,本小节进行大致讲解

开发一个全新的项目,需要先进行开发环境的搭建
要确定技术框架以及版本,还要考虑各个框架之间的版本兼容问题,对新项目进行配置以及测试
受到Ruby On Rails、Node.js等技术的影响,JavaEE领域需要一种更为简单的开发方式,所以推出了SpringBoot

  • 可以使开发者快速搭建项目,使用可以更加专注于开发,无需关注哪个框架依赖哪个版本
  • 系统配置、基础代码、项目依赖的 jar 包,甚至是开发时所用到的应用服务器等,Spring Boot 已经帮我们准备好,只要在建立项目时,使用构建工具加入相应的 Spring Boot 依赖包,项目即可运行,使用者无需关心版本兼容等问题。

2、Eclipse创建SpringBoot

2.1、新建Maven项目

2.1.1、加入Web模块
  • 为了测试项目的可用性,加入SpringBoot的web启动模块,让该项目具有web容器的功能
  • /atm-test/pom.xml
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.atm.cloud</groupId>
    <artifactId>atm-test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>atm-test Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.4.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>atm-test</finalName>
    </build>
</project>
  • 配置完毕后,该依赖会自动在项目上加入其他Spring模块以及所依赖的第三方包,例如:Spring-core、Spring-beans、Spring-mvc等,其中还加入了内嵌的Tomcat

2.1.2、编写启动类

package com.atm.cloud;

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

@SpringBootApplication
public class MyApplication {

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

}
  • 执行启动类main方法

2.1.3、编写控制器

package com.atm.cloud;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello World";
    }

}

  • 加入的Spring-boot-start-web模块,默认集成了SpringMVC,因此只需要编写一个Controller,即可实现最简单的Hello world
  • @SpringBootApplication注解,该注解含有@ComponentScan的功能,因此@Controller会被扫描注册

2.2、发布REST WebService

  • SpringMVC支持直接发布REST风格的WebService,新建测试对象Person

package com.atm.cloud;

public class Person {

    private Integer id;

    private String name;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }

}
  • 修改控制器
package com.atm.cloud;

//import org.springframework.stereotype.Controller;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

//@Controller
@RestController
public class MyController {

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello World";
    }

    @RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("personId") Integer personId) {
        Person person = new Person();

        person.setId(personId);
        person.setName("atm");
        person.setAge(18);

        return person;
    }
}

2.3、Springboot配置文件

  • SpringCloud基于SpringBoot构建,很多模块的配置均放在SpringBoot的配置文件中
  • SpringBoot会按顺序读取各种配置,例如命令行参数、系统参数等
  • 本章只讲述配置文件的参数读取,默认情况下,SpringBoot会按顺序到以下目录读取application.properties或者application.yml
    1. 项目根目录的config目录
    2. 项目根目录
    3. 项目classpath下的config目录
    4. 项目classpath根目录

test.user.name=1
package com.atm.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PropController {

    @Autowired
    private ApplicationContext context;

    @GetMapping("/getProp")
    @ResponseBody
    public String getProp(){
        System.out.println("Come into getProp");
        return context.getEnvironment().getProperty("test.user.name");
    }

}

  • 如果去除第一个配置文件application.properties,则默认读取第二个的
2.3.1、指定配置文件
server.port=80

  • 修改启动类
package com.atm.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = new SpringApplicationBuilder(
                MyApplication.class)
                .properties(
                        "spring.config.location=classpath:/atm-test/application.properties")
                .run(args);

        /*

        ConfigurableApplicationContext context = new SpringApplicationBuilder(
                MyApplication.class)
                .properties(
                        "spring.profiles.active=linux")
                .run(args);
        */

        /*
        ConfigurableApplicationContext context = new SpringApplicationBuilder(
                MyApplication.class)
                .properties(
                        "spring.profiles.active=linux","server.port=8081")
                .run(args);
        */

        //SpringApplication.run(MyApplication.class, args);
    }

}

2.4、yml文件

  • YAML语言使用一种方便的格式来进行数据配置,通过配置分层、缩进,在很大程度上增强了配置文件的可读性
  • 使用YAML语言的配置文件以“.yml”为后缀

package com.atm.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = new SpringApplicationBuilder(
                MyApplication.class)
                .properties(
                        "spring.config.location=classpath:/atm-test/application.yml")
                .run(args);

        //SpringApplication.run(MyApplication.class, args);
    }

}

2.5、热部署

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.atm.cloud</groupId>
    <artifactId>atm-test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>atm-test Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- SpringBoot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.4.RELEASE</version>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>1.5.4.RELEASE</version>
            <optional>true</optional>
        </dependency>

    </dependencies>
    <build>
        <finalName>atm-test</finalName>
    </build>
</project>

3、IDEA

3.1、创建SpringBoot项目

3.2、配置文件

3.3、热部署

3.3.1、添加依赖
<?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.atm.cloud</groupId>
    <artifactId>atm_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>1.5.4.RELEASE</version>
            <optional>true</optional>
        </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>
                <configuration>
                    <fork>true</fork><!--注意要修改这里-->
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>
3.3.2、开启自动编译

  • Ctrl + Shift + A 键入 Registry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'嗯哼。

生生不息,“折腾”不止,Thx

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值