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.0modelVersion>
    <groupId>com.atm.cloudgroupId>
    <artifactId>atm-testartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>atm-test Maven Webappname>
    <url>http://maven.apache.orgurl>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>1.5.4.RELEASEversion>
        dependency>
    dependencies>
    <build>
        <finalName>atm-testfinalName>
    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.0modelVersion>
    <groupId>com.atm.cloudgroupId>
    <artifactId>atm-testartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>atm-test Maven Webappname>
    <url>http://maven.apache.orgurl>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>1.5.4.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <version>1.5.4.RELEASEversion>
            <optional>trueoptional>
        dependency>

    dependencies>
    <build>
        <finalName>atm-testfinalName>
    build>
project>

3、IDEA

3.1、创建SpringBoot项目

3.2、配置文件

3.3、热部署

3.3.1、添加依赖


<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.0modelVersion>

    <groupId>com.atm.cloudgroupId>
    <artifactId>atm_testartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>atm_testname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.10.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <version>1.5.4.RELEASEversion>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <fork>truefork>
                configuration>
            plugin>
        plugins>
    build>


project>

3.3.2、开启自动编译

  • Ctrl + Shift + A 键入 Registry

你可能感兴趣的:(Java,SpringCloud)