学习笔记:JavaEE开发的颠覆者SpringBoot实战(五)spring boot基础

一.概述

1.什么是spring boot

它使用“习惯优于配置”的理念让你的项目快速运行起来。使用它很容易创建一个独立运行,准生产级别的基于spring框架的项目,使用它可以不用或者只需很少的spring配置。

2.spring boot核心功能

  • 独立运行的spring项目:可以以jar包独立运行,运行命令:java -jar xx.jar
  • 内嵌servlet容器:可以选择内嵌tomcat,jetty,故无须war部署
  • 提供starter简化maven配置:提供一系列的starter pom来简化maven依赖加载(即相关的jar包加载)。如,spring-bootstarter-web
  • 自动配置spring:根据在类路径中的jar包,类,为jar包里的类自动配置bean
  • 准生产的应用控制:提供基于http,ssh,telnet对运行时的项目进行监控
  • 无代码生生和xml配置:通过java配置和注解配置组合使用来实现

3.spring boot的优缺点

优点:

  • 快速构建项目
  • 对主流开发框架的无配置集成
  • 项目可独立运行,无须外部依赖servlet容器
  • 提供运行时的监控
  • 与云计算的天然集成

缺点:

  • 文档较少
  • 必须以spring为基础框架

 

二.spring boot快速搭建

1.web端部署

http://start.spring.io

2.工具部署(IDEA)

Spring Tool Suite :新建Spring Starter Project项目

IDEA :新建Spring Initializr项目

三.Maven手工构建spring boot

1 新建一个空的maven项目

2 修改pom.xml

2.1 添加spring boot的父级依赖spring-boot-starter-parent

它用来提供相关的maven默认依赖,相关依赖可以省去version标签.具体相关依赖,可查看maven本地库.m2\repository\org\springframework\boot\spring-boot\spring-boot-dependencies\2.1.6RELEASE\spring-boot-dependencies-2.1.6RELEASE.pom

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

2.2 在dependencies添加web支持的starter pom

这样就添加了web的依赖

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

2.3 添加spring boot的编译插件

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

2.4.简单演示

@RestController
@SpringBootApplication //spring boot项目的核心注解,主要目的是开启自动配置
public class DemoApplication{

    @RequestMapping("/")
    public String index(){
        return "hello spring boot";
    }

    //main方法,主要作用是作为项目启动的入口
    public static void main(String[] args){
        SpringApplication.run(DemoApplication.class,args);
    }    

}

maven名利运行:

mvn spring-boot:run

或者在该类上右键Run

运行后访问:localhost:8080

 

四.基本配置

1.入口类和@SpringBootApplication

spring boot 通常由要给名为*Application的入口类,入口类里有要给main方法,这个main方法其实就是要给标准的java应用的入口方法。在main方法中使用SpringApplication.run(DemoApplication.class,args),启动项目。

@SringBootApplication 是spring boot的核心注解,它是一个组合注解,源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration  //让spring boot根据类路径中的jar包依赖为当前项目进行自动配置
@ComponentScan
public @interface SpirngBootApplication{
    Class<?>[] exclude() default{};
    String[] excludeName() default{};
}

2.关闭特定的自动配置

@SpringBootApplication(exclude={DataSOurceAutoConfiguration.class})

3.定制banner

3.1 修改banner

在src/main/resources下新建一个banner.txt,文件里的图案可以替换默认的。

3.2 关闭banner

main里的内容修改为:

SpringApplication app = new SpringApplication(DemoApplication.class);
app.setShowBanner(false);
app.run(args);

4. spring boot的配置文件

spring boot使用要给全局的配置文件application.properties或application.yml,放置在src/main/resources目录胡总和类路径的/config下。

spring boot的全局配置文件的作用是对一些默认配置的配置值进行修改。如:将tomcat的默认端口8080修改为9090,并将默认的访问路径“/”修改为“/helloboot”

application.yml

server:
 port: 9090
 contextPaht: /helloboot

5.starter pom

Spring Boot application starters

名称描述
spring-boot-starter核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator生产准备的特性,用于帮你监控和管理应用
spring-boot-starter-amqp对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa对”Java持久化API”的支持,包括spring-data-jpaspring-orm和Hibernate
spring-boot-starter-data-mongodb对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates对Groovy模板引擎的支持
spring-boot-starter-hateoas对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration对普通spring-integration模块的支持
spring-boot-starter-jdbc对JDBC数据库的支持
spring-boot-starter-jersey对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mailjavax.mail的支持
spring-boot-starter-mobilespring-mobile的支持
spring-boot-starter-mustache对Mustache模板引擎的支持
spring-boot-starter-redis对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-securityspring-security的支持
spring-boot-starter-social-facebookspring-social-facebook的支持
spring-boot-starter-social-linkedinspring-social-linkedin的支持
spring-boot-starter-social-twitterspring-social-twitter的支持
spring-boot-starter-test对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity对Velocity模板引擎的支持
spring-boot-starter-web对全栈web开发的支持,包括Tomcat和spring-webmvc
spring-boot-starter-websocket对WebSocket开发的支持
spring-boot-starter-ws对Spring Web服务的支持

6.使用xml配置

特殊要求下,可以通过@ImportResource来加载xml配置。如果:

@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})

五.外部配置

1.命令行参数配置

比如,修改tomcat端口号:

java -jar xx.jar --server.port=9090

2.常规属性配置

注入配置properties文件里的值,在spring boot里,我们只需要在配置文件里卖弄定义属性,直接使用@value注入即可。

application.yml

server:
 port: 9090
 contextPaht: /helloboot

book:
 author: shenhaiming
 name: spring boot

类注入

public class Demo{
    @Value("${book.author}")
    private String bookAuthor;
    
    @Value("${book.name}")
    private String bookName;

}

3.类型安全的配置

3.1 基于properties

配置mydemo.properties

author.name=shm
author.age=32

bean

@Component
@PropertySource(value="classpath:mydemo.properties")
@ConfigurationProperties(prefix="author")
public class AuthorSettings{
    private String name;
    private Long age;

    //set
    //get

}

3.2 基于yml

配置mydemo.yml

author:
  name: shm
  age: 32

bean

@Component
@PropertySource(value="classpath:mydemo.yml")
@ConfigurationProperties(prefix="author")
public class AuthorSettings{
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Long age;

    //set
    //get

}

三.日志配置

loggin.file=D:/mylog/log.log  #配置日志级别
loggin.level.org.springframework.web=DEBUG #配置日志文件

四.Profile配置

Profile是spring用来针对不同的环境对不同配置提供支持的,全局profile配置使用application-{profile}.properties

通过application.properties中设置spring.profiles.active=prod来指定活动的Profile

application-prod.properties

server.port=8080

application-dev.properties

server.port=8989

application.properties

spring.profiles.active=dev

五.spring boot运行原理

查看项目中已启动和未启动的自动配置报告

  • 运行jar是增加--debug参数:java -jar xx.jar --debug
  • 在配置application.properties中设置属性:debug=true
  • IDEA中选择Run Configurations --> Environment --> VM options : -Ddebug

1.运作原理

@EnableAutoConfiguration源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({EnableAutoConfigurationImportSelector.class,AutoConfigurationPackages.Registrat.class})
public @interface EnableAutoConfiguration{
    Class<?>[] exclude() default{};
    String[] excludeName() default{};
}

这里@Import注解导入的配置功能,EnableAutoConfigurationImportSelector使用SpringFactoriesLoader.loadFactoryNmaes方法来扫描具有META-INF/spring.factories文件的jar包,而我们的spring-boot-autoconfigure-xxx.jar里面就有个spring.factories文件,此文件中声明了又哪里自动配置(#auto configure)

2.核心注解

可见:https://blog.csdn.net/zwmnhao1980/article/details/80746877

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值