最渣的 Spring Boot 文章

spring-boot-starter-parent

Maven的用户能够经过继承spring-boot-starter-parent项目来得到一些合理的默认配置。这个parent提供了如下特性:java

  • 默认使用Java 8
  • 使用UTF-8编码
  • 一个引用管理的功能,在dependencies里的部分配置能够不用填写version信息,这些version信息会从spring-boot-dependencies里获得继承。
  • 识别过来资源过滤(Sensible resource filtering.)
  • 识别插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  • 可以识别application.properties和application.yml类型的文件,同时也能支持profile-specific类型的文件(如: application-foo.properties and application-foo.yml,这个功能能够更好的配置不一样生产环境下的配置文件)。
  • maven把默认的占位符${…​}改成了@..@(这点你们仍是看下原文本身理解下吧,我我的用的也比较少
    since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).)

starter

启动器包含一些相应的依赖项, 以及自动配置等.mysql

Auto-configuration

Spring Boot 支持基于Java的配置, 尽管能够将 SpringApplication 与 xml 一块儿使用, 可是仍是建议使用 @Configuration.spring

能够经过 @Import 注解导入其余配置类, 也能够经过 @ImportResource 注解加载XML配置文件.sql

Spring Boot 自动配置尝试根据您添加的jar依赖项自动配置Spring应用程序. 例如, 若是项目中引入 HSQLDB jar, 而且没有手动配置任何数据库链接的bean, 则Spring Boot会自动配置内存数据库.数据库

您须要将 @EnableAutoConfiguration 或 @SpringBootApplication 其中一个注解添加到您的 @Configuration 类中, 从而选择进入自动配置.

禁用自动配置

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

若是该类不在classpath中, 你可使用该注解的excludeName属性, 并指定全限定名来达到相同效果. 最后, 你能够经过 spring.autoconfigure.exclude 属性 exclude 多个自动配置项(一个自动配置项集合)数组

@ComponentScan

SpringBoot在写启动类的时候若是不使用 @ComponentScan 指明对象扫描范围, 默认指扫描当前启动类所在的包里的对象.缓存

@SpringBootApplication

@Target(value=TYPE)
 @Retention(value=RUNTIME)
 @Documented
 @Inherited
 @SpringBootConfiguration
 @EnableAutoConfiguration
 @ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM,classes=TypeExcludeFilter.class),})
public @interface SpringBootApplication

使用 @SpringBootApplication 注解至关于使用了下面三个注解.app

@EnableAutoConfiguration: 启用 Spring Boot 的自动配置.
@ComponentScan: 对应用程序所在的包启用 @Component 扫描.
@Configuration: 容许在上下文中注册额外的bean或导入其余配置类.dom

ApplicationRunner or CommandLineRunner 区别

应用服务启动时,加载一些数据和执行一些应用的初始化动做。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库链接等。maven

一、SpringBoot提供了CommandLineRunner接口。当有该接口多个实现类时,提供了@order注解实现自定义执行顺序,也能够实现Ordered接口来自定义顺序。

注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类以前执行。

import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=1)
public class SpringDataInit implements CommandLineRunner {

    @Autowired
    private MyMethorClassService myMethorClassService;

    @Override
    public void run(String... strings) throws Exception {
        int result = myMethorClassService.add(8, 56);
        System.out.println("----------SpringDataInit1---------"+result);
    }
}

二、SpringBoot提供的ApplicationRunner接口也能够知足该业务场景。不一样点:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。想要更详细地获取命令行参数,那就使用ApplicationRunner接口

import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Component
public class SpringDataInit3 implements ApplicationRunner,Ordered {

    @Autowired
    private MyMethorClassService myMethorClassService;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        int result = myMethorClassService.add(10, 82);
        System.out.println("----------SpringDataInit3---------"+result);
    }

    @Override
    public int getOrder() {
        return 3;
    }
}

外部化配置

Spring Boot容许你外部化你的配置,这样你就能够在不一样的环境中使用相同的应用程序代码,你可使用 properties 文件、YAML文件、环境变量和命令行参数来外部化配置,属性值能够经过使用 @Value 注解直接注入到你的bean中,经过Spring的 Environment 抽象访问,或者经过 @ConfigurationProperties 绑定到结构化对象。

@ConfigurationProperties("spring.datasource.username")

Spring Boot使用一种很是特殊的 PropertySource 命令, 该命令旨在容许对值进行合理的覆盖, 属性按如下顺序考虑:

  • Devtools全局设置属性在你的主目录( ~/.spring-boot-devtools.properties 当devtools处于激活状态时。
  • 测试中的 @TestPropertySource 注解
  • 测试中的 @SpringBootTest#properties 注解属性
  • 命令行参数
  • 来自 SPRING_APPLICATION_JSON(嵌入在环境变量或系统属性中的内联JSON)的属性
  • ServletConfig 初始化参数
  • ServletContext 初始化参数
  • java:comp/env 中的JNDI属性
  • Java系统属性(System.getProperties()
  • 操做系统环境变量
  • 一个只有random.*属性的RandomValuePropertySource
  • 在你的jar包以外的特殊配置文件的应用程序属性(application-{profile}.properties 和YAML 变体)
  • 在jar中打包的特殊配置文件的应用程序属性(application-{profile}.properties 和YAML 变体)
  • 在你的jar包以外的应用程序属性(application.properties 和YAML 变体)
  • 打包在jar中的应用程序属性(application.properties 和YAML 变体)
  • @PropertySource 注解在你的 @Configuration 类上
  • 默认属性(经过设置 SpringApplication.setDefaultProperties 指定)

访问命令行属性

在默认状况下, SpringApplication 会转换任何命令行选项参数 (也就是说,参数从 -- 开始, 像 --server.port=9000) 到一个 property, 并将它们添加到Spring Environment 中, 如前所述, 命令行属性老是优先于其余属性源.

若是不但愿将命令行属性添加到 Environment 中, 你可使用 SpringApplication.setAddCommandLineProperties(false) 禁用它们.

应用程序属性文件

SpringApplication 在如下位置从 application.properties 文件加载属性并将它们添加到Spring Environment :

  • 当前目录子目录的 /config
  • 当前目录
  • 类路径下 /config
  • 类路径的根目录

列表按优先顺序排序(在列表中较高的位置定义的属性覆盖在较低位置定义的属性).

特殊配置文件的属性

咱们可能在不一样环境下使用不一样的配置, 这些配置有多是在同一个文件中或不一样文件中.

1.在相同文件中

##################################### Determime which configuration be used
spring: 
      profiles: 
            active: "dev"
      # Mysql connection configuration(share)
      datasource: 
            platform: "mysql"
            driverClassName: "com.mysql.cj.jdbc.Driver"
            max-active: 50
            max-idle: 6
            min-idle: 2
            initial-size: 6
            
---
##################################### for dev environment
spring: 
      profiles: "dev"
      datasource: 
            # mysql connection user(dev)
            username: "root"
            # mysql connection password(dev)
            password: "r9DjsniiG;>7"
---
##################################### for product environment
spring: 
      profiles: "product"
      datasource: 
            # mysql connection user(product)
            username: "root"
            # mysql connection password(product)
            password: "root"
---
##################################### for test environment
spring: 
      profiles: "test"
      datasource: 
            # mysql connection user(test)
            username: "root"
            # mysql connection password(test)
            password: "root"

这样在配置完相同属性的时, 还能够对不一样的环境进行不一样的配置.

2.多个配置文件.

咱们能够把特定环境的配置, 放入多个配置文件中, 可是要按照 application-{profile}.properties 格式. 以下图.

clipboard.png

spring.profiles.active 属性进行设置.

咱们也能够把配置文件放在 jar 外面, 使用 spring.config.location 属性进行设置.

clipboard.png

java -jar beetltest-0.0.1-SNAPSHOT.jar -spring.config.location=classpath:/application.properties
相关文章
相关标签/搜索