springboot 使用教程


1.对于了解spring的人来说,学习门槛很低。提供推荐的基础 POM文件来简化 Maven配置,尽可能的根据项目依赖来自动配置 Spring框架,无需xml配置,也可以修改默认值来满足特定的需求。


 

2.提供了一些常见的非功能性特性,如嵌入式服务器(内置tomcatjetty)、安全、健康检测、外部配置等,不需要部署 WAR 文件


1.2一个spring boothelloworld程序

1.2.1 pom.xml配置

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

当运行 maven打包时,这个插件会额外打一个可以运行的jar包。使用Java -jar”命令就可以直接运行

 
1. Spring Boot推荐的基础POM 文件

 

名称

说明

spring-boot-starter

核心 POM,包含自动配置支持、日志库和对YAML配置文件的支持。

spring-boot-starter-amqp

通过 spring-rabbit支持AMQP

spring-boot-starter-aop

包含 spring-aopAspectJ来支持面向切面编程(AOP)。

spring-boot-starter-batch

支持 Spring Batch,包含HSQLDB

spring-boot-starter-data-jpa

包含 spring-data-jpaspring-ormHibernate来支持JPA

spring-boot-starter-data-mongodb

包含 spring-data-mongodb来支持MongoDB

spring-boot-starter-data-rest

通过 spring-data-rest-webmvc支持以REST方式暴露Spring Data仓库。

spring-boot-starter-jdbc

支持使用 JDBC 访问数据库。

spring-boot-starter-security

包含 spring-security

spring-boot-starter-test

包含常用的测试所需的依赖,如 JUnitHamcrestMockitospring-test等。

spring-boot-starter-velocity

支持使用 Velocity 作为模板引擎。

spring-boot-starter-web

支持 Web应用开发,包含Tomcatspring-mvc

spring-boot-starter-websocket

支持使用 Tomcat 开发 WebSocket应用。

spring-boot-starter-ws

支持 Spring Web Services

spring-boot-starter-actuator

添加适用于生产环境的功能,如性能指标和监测等功能。

spring-boot-starter-remote-shell

添加远程 SSH 支持。

spring-boot-starter-jetty

使用 Jetty而不是默认的Tomcat作为应用服务器。

spring-boot-starter-log4j

添加 Log4j的支持。

spring-boot-starter-logging

使用 Spring Boot默认的日志框架Logback

spring-boot-starter-tomcat

使用 Spring Boot默认的Tomcat作为应用服务器。

1.2.2 程序入口(位于主包下)


import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}


简单介绍@SpringBootApplication

@SpringBootApplication 是一个组合注解。等于同时

@Configuration,@EnableAutoConfiguration,@ComponentScan

 

@EnableAutoConfiguration

Spring Boot建议只有一个带有该注解的类。作用是Spring Boot会自动根据你jar包的依赖来自动配置项目。比如添加spring-boot-starter-web依赖,会自动添加tomcat和spring mvc的依赖,那么Spring Boot会对Tomcat和SpirngMVC进行自动配置。

@Configuration 等于在xml配置一个beans

@ComponentScan 等于配置 <context:component-scan>

1.3 spring boot的配置

 

 Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性文件、YAML文件、环境变量和命令行参数来定义优先级不同的配置值。

 

Spring Boot 所提供的配置优先级顺序比较复杂。

按照优先级从高到低的顺序,具体的列表如下所示:

 

1.命令行参数

2.来自java:comp/envJNDI属性

3.Java系统属性(System.getProperties())

4.操作系统环境变量

5.RandomValuePropertySource配置的random.*属性值

6.application-{profile}.properties或application.yml(spring.profile)配置文件

7.jar包外部的application.properties或application.yml(不带spring.profile)配置文件

8.@Configuration注解类上的@PropertySource

9.通过SpringApplication.setDefaultProperties指定的默认属性

 

说明

1.Spring Boot 的这个配置优先级看似复杂,其实是很合理的。比如命令行参数的优先级被设置为最高。这样的好处是可以在测试或生产环境中快速地修改配置参数值,而不需要重新打包和部署应用。

 

2. SpringApplication 类默认会把以“--”开头的命令行参数转化成应用中可以使用的配置参数,如果不需要这个功能,可以通过“SpringApplication.setAddCommandLineProperties(false)”禁用解析命令行参数。

   -----   java  -jar app.jar --name="Spring" --server.port=9090

 

3. RandomValuePropertySource 可以用来生成测试所需要的各种不同类型的随机值,从而免去了在代码中生成的麻烦。以“random.”作为前缀的配置属性名称由RandomValuePropertySource来生成

user.id=${random.value}

user.count=${random.int}

user.max=${random.long}

user.number=${random.int(100)}

user.range=${random.int[100, 1000]}

 

配置文件

application.propertiesapplication.yml

 

application.properties

 

name=springboot-helloworld

server.port=9090

 

application.yml

name: springboot-helloworld

server:

    port: 9090

 

 

 

 

官网链接

HTTP://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/common-application-properties.html

 

 

 

SpringApplication.setDefaultProperties

例如:

SpringApplication application = new SpringApplication(Application.class);

Map<String, Object> defaultMap = new HashMap<String, Object>();

defaultMap.put("name", "Isea-Blog");//还可以是Properties对象

application.setDefaultProperties(defaultMap);

application.run(args);

 

@ConfigurationProperties

Spring Boot 可以方便的将属性注入到一个配置对象中。例如:

my.name=Isea533

my.port=8080

my.servers[0]=dev.bar.com

my.servers[1]=foo.bar.com

 

对应对象:

@ConfigurationProperties(prefix="my")public class Config {

    private String name;

    private Integer port;

    private List<String> servers = new ArrayList<String>();

 

    public String geName(){

        return this.name;

    }

 

    public Integer gePort(){

        return this.port;

    }

    public List<String> getServers() {

        return this.servers;

    }

}

 

 

 

Prefiles的配置

application.properties

spring.profiles.active=dev

 

application-dev.properties

server.port=8081  

application-prd.properties

server.port=8082  

 

 

 java -jar xx.jar --spring.profiles.active=dev

1.4 spring boot集成mybatis

 

在pom.xml中添加依赖:


<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.0.1</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.3</version>
</dependency>


application.propertiesl中增加配置:

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.xx.model

mybatis.config-location=xx.xml  #可以不配置

1.5生产环境中的运维支持

当应用部署到生产环境时,需要各种运维相关的功能的支持,包括性能指标、运行信息和应用管理等。所有这些功能都有很多技术和开源库可以实现。Spring Boot对这些运维相关的功能进行了整合,形成了一个功能完备和可定制的功能集,称之为Actuator

 

只需要在 POM 文件中增加对“org.springframe.boot:spring-boot-starter-actuator”的依赖就可以添加ActuatorActuator在添加之后,会自动暴露一些HTTP服务来提供这些信息。

 

Spring Boot Actuator 所提供的 HTTP服务

 

名称

说明

是否包含敏感信息

autoconfig

显示 Spring Boot 自动配置的信息。

beans

显示应用中包含的 Spring bean 的信息。

configprops

显示应用中的配置参数的实际值。

dump

生成一个 thread dump

env

显示从 ConfigurableEnvironment 得到的环境配置信息。

health

显示应用的健康状态信息。

info

显示应用的基本信息。

metrics

显示应用的性能指标。

mappings

显示 Spring MVC 应用中通过
@RequestMapping”添加的路径映射。

shutdown

关闭应用。

trace

显示应用相关的跟踪(trace)信息。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值