SpringBoot初步了解

springboot 是什么

spring boot  能够快速构建和运行基于spring 的应用。spring boot为我们已经封装好了搭建spring框架lib版本, spring boot 只需要我们
采用最少的配置即可运行起来spring 的应用,为懒人提供了一种开发的便利。
spring boot 官网介绍:https://spring.io/projects/spring-boot

spring boot 特点

  • 创建独立的spring应用
  • 内置的tomcat jetty 可以让打好的jar包独立运行
  • 提供最优的依赖配置, 让构建spring应用更加的快捷方便
  • 自动配置,以及集成第三方lib
  • 提供生产的特性, 例如metrics监控,监控检查 , 额外的配置
  • 采用javaConfig ,不需要xml配置

快速构建spring boot 项目

1.ide file ->new Project 
2.选择spring inializer  点击next
3.输入gourp artifact 点击next
4.选择依赖 finish

dos输入tree:
D:.
├─.idea
│  └─inspectionProfiles
├─.mvn
│  └─wrapper
└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─example
    │  │          └─springbootstudy
    │  └─resources
    │      ├─static
    │      └─templates
    └─test
        └─java
            └─com
                └─example
                    └─springbootstudy

spring boot 配置

spring boot 的配置可以采用.properties 或者 .yml 文件 个人比较喜欢yml (层级展示, 结构清楚一点)
需要加入dependency依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
多环境配置  dev 开发环境, test 测试环境
在resources地下建立application-dev.yml
server:
  port: 8888
spring:
  application:
    name: spring-boot-study
 application-test.yml 配置
server:
  port: 8080
spring:
  application:
    name: spring-boot-study
在application.yml 指定启用那个环境的配置
spring:
  profiles:
    active: dev

在application 页面建立一个测试, 请求发现我们的端口是更加application.yml 
里面指定的spring.profiles.active决定的



自定义的属性配置
我们可以访问:http://localhost:8888/hello?name=%E4%B8%AD%E5%9B%BD 查看测试结果

server:
  port: 8888
spring:
  application:
    name: spring-boot-study

#自定义的属性配置

student:
  name: java
  score: 88
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//封装为对象
@Component
@ConfigurationProperties(prefix = "student")
public class StudentConfig {

    private String name;

    private int score;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "StudentConfig{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@RestController
public class SpringBootStudyApplication {

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

    //系统的属性配置
	@Value("${server.port}")
	private int port;

    //自定义的属性配置
	@Autowired
	StudentConfig studentConfig;


	@RequestMapping(value = "/hello")
	public Map<String,Object> hello(@RequestParam(value = "name", required = false, defaultValue = "springboot") String name){
		if(StringUtils.isBlank(name)){
			name = "java";
		}
		Map<String,Object> map = new HashMap<>();
		map.put("name",name);
		map.put("port",port);
		map.put("studentConfig",studentConfig);
		return map;
	}

}

监控微服务状态

微服务添加监控,让我们更好的了解微服务的健康状态
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动项目  spirng boot 的版本不一样 原生端点的结果也不一样
spring 版本:2.0.6.RELEASE
Exposing 2 endpoint(s) beneath base path '/actuator'
Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
spring 版本:1.5.4.RELEASE
Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
访问:
http://localhost:8888/actuator/health   {"status":"UP"}
http://localhost:8888/actuator/info     {}
http://localhost:8888/actuator         {"_links":{"self":{"href":"http://localhost:8888/actuator","templated":false},"health":{"href":"http://localhost:8888/actuator/health","templated":false},"info":{"href":"http://localhost:8888/actuator/info","templated":false}}}

spring-boot-study 访问地址:https://gitee.com/ninuxGithub/spring-boot-study.git