最渣的 Spring Boot 文章

0次阅读

共计 6040 个字符,预计需要花费 16 分钟才能阅读完成。

spring-boot-starter-parent
Maven 的用户可以通过继承 spring-boot-starter-parent 项目来获得一些合理的默认配置。这个 parent 提供了以下特性:

默认使用 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
启动器包含一些相应的依赖项, 以及自动配置等.
Auto-configuration
Spring Boot 支持基于 Java 的配置, 尽管可以将 SpringApplication 与 xml 一起使用, 但是还是建议使用 @Configuration.
可以通过 @Import 注解导入其他配置类, 也可以通过 @ImportResource 注解加载 XML 配置文件.
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 注解相当于使用了下面三个注解.
@EnableAutoConfiguration: 启用 Spring Boot 的自动配置.@ComponentScan: 对应用程序所在的包启用 @Component 扫描.@Configuration: 允许在上下文中注册额外的 bean 或导入其他配置类.
ApplicationRunner or CommandLineRunner 区别
应用服务启动时,加载一些数据和执行一些应用的初始化动作。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。
1、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);
}
}
2、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 格式. 如下图.

spring.profiles.active 属性进行设置.
我们也可以把配置文件放在 jar 外面, 使用 spring.config.location 属性进行设置.

java -jar beetltest-0.0.1-SNAPSHOT.jar -spring.config.location=classpath:/application.properties

正文完
 0