数据
1. SQL 数据库
Spring Framework 为使用 SQL 数据库提供了广泛的支持,通过使用JdbcTemplate
完成 Hibernate 等 “对象关系映射” 技术。Spring Data 提供了另一个级别的功能:创建Repository
直接从接口实现,并使用约定从您的方法名称生成查询。
1.1. 配置 DataSource
Java 的javax.sql.DataSource
interface 提供了一种使用数据库连接的标准方法。
传统上,一个DataSource
使用URL
以及用于建立数据库连接的一些凭证。
有关更高级的示例,请参阅“作方法”部分,通常是为了完全控制 DataSource 的配置。 |
1.1.1. 嵌入式数据库支持
使用内存中嵌入式数据库开发应用程序通常很方便。 显然,内存数据库不提供持久存储。 您需要在应用程序启动时填充数据库,并准备好在应用程序结束时丢弃数据。
“作方法”部分包括有关如何初始化数据库的部分。 |
Spring Boot 可以自动配置嵌入式 H2、HSQL 和 Derby 数据库。
您无需提供任何连接 URL。
您只需包含对要使用的嵌入式数据库的构建依赖项。
如果 Classpath 上有多个嵌入式数据库,请将spring.datasource.embedded-database-connection
configuration 属性来控制使用哪一个。
将属性设置为none
禁用嵌入式数据库的自动配置。
如果您在测试中使用此功能,您可能会注意到,无论您使用多少个应用程序上下文,整个测试套件都会重用同一个数据库。
如果要确保每个上下文都有一个单独的嵌入式数据库,则应将 |
例如,典型的 POM 依赖项如下所示:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
您需要依赖spring-jdbc 以自动配置嵌入式数据库。
在此示例中,它是通过spring-boot-starter-data-jpa . |
如果出于某种原因,您确实为嵌入式数据库配置了连接 URL,请注意确保禁用数据库的自动关闭。
如果使用 H2,则应使用DB_CLOSE_ON_EXIT=FALSE 执行此作。
如果您使用 HSQLDB,则应确保shutdown=true 未使用。
禁用数据库的自动关闭可以让 Spring Boot 控制数据库何时关闭,从而确保在不再需要访问数据库时进行关闭。 |
1.1.3. DataSource 配置
DataSource 配置由spring.datasource.*
.
例如,您可以在application.properties
:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
您至少应该通过设置spring.datasource.url 财产。
否则, Spring Boot 会尝试自动配置嵌入式数据库。 |
Spring Boot 可以从 URL 中推断出大多数数据库的 JDBC 驱动程序类。
如果需要指定特定类,可以使用spring.datasource.driver-class-name 财产。 |
对于池化DataSource 要创建,我们需要能够验证有效的Driver class 可用,因此我们在执行任何作之前都会检查一下。
换句话说,如果您将spring.datasource.driver-class-name=com.mysql.jdbc.Driver ,则该类必须是可加载的。 |
看DataSourceProperties
了解更多支持的选项。
这些是无论实际实现如何都有效的标准选项。
还可以使用各自的前缀 (spring.datasource.hikari.*
,spring.datasource.tomcat.*
,spring.datasource.dbcp2.*
和spring.datasource.oracleucp.*
).
有关更多详细信息,请参阅您正在使用的连接池实现的文档。
例如,如果您使用 Tomcat 连接池,则可以自定义许多其他设置,如以下示例所示:
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.test-on-borrow=true
spring:
datasource:
tomcat:
max-wait: 10000
max-active: 50
test-on-borrow: true
这会将池设置为在没有可用连接的情况下等待 10000 毫秒,然后再引发异常,将最大连接数限制为 50 个,并在从池中借用连接之前验证连接。
1.1.4. 支持的连接池
Spring Boot 使用以下算法来选择特定实现:
-
我们更喜欢 HikariCP 的性能和并发性。 如果 HikariCP 可用,我们总是会选择它。
-
否则,如果 Tomcat 池化
DataSource
可用,我们使用它。 -
否则,如果 Commons DBCP2 可用,我们就会使用它。
-
如果 HikariCP、Tomcat 和 DBCP2 都不可用,并且 Oracle UCP 可用,则使用它。
如果您使用spring-boot-starter-jdbc 或spring-boot-starter-data-jpa “starters”,则会自动获得对HikariCP . |
您可以完全绕过该算法,并通过设置spring.datasource.type
财产。
如果您在 Tomcat 容器中运行应用程序,这一点尤其重要,因为tomcat-jdbc
默认提供。
其他连接池始终可以手动配置,使用DataSourceBuilder
.
如果您定义自己的DataSource
bean,则不会进行自动配置。
以下连接池由DataSourceBuilder
:
-
光CP
-
Tomcat 池
Datasource
-
共享 DBCP2
-
Oracle UCP 和
OracleDataSource
-
Spring 框架的
SimpleDriverDataSource
-
H2 系列
JdbcDataSource
-
PostgreSQL 数据库
PGSimpleDataSource
-
C3P0
1.1.5. 连接到 JNDI 数据源
如果将 Spring Boot 应用程序部署到 Application Server,则可能需要使用 Application Server 的内置功能来配置和管理 DataSource,并使用 JNDI 访问它。
这spring.datasource.jndi-name
属性可以用作spring.datasource.url
,spring.datasource.username
和spring.datasource.password
属性以访问DataSource
从特定的 JNDI 位置。
例如,以下部分application.properties
演示如何访问定义的 JBoss ASDataSource
:
spring.datasource.jndi-name=java:jboss/datasources/customers
spring:
datasource:
jndi-name: "java:jboss/datasources/customers"
1.2. 使用 JdbcTemplate
Spring的JdbcTemplate
和NamedParameterJdbcTemplate
类是自动配置的,你可以@Autowire
将它们直接放入您自己的 bean 中,如以下示例所示:
@Component
public class MyBean {
private final JdbcTemplate jdbcTemplate;
public MyBean(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void doSomething() {
this.jdbcTemplate ...
}
}
@Component
class MyBean(private val jdbcTemplate: JdbcTemplate) {
fun doSomething() {
jdbcTemplate.execute("delete from customer")
}
}
您可以使用spring.jdbc.template.*
属性,如以下示例所示:
spring.jdbc.template.max-rows=500
spring:
jdbc:
template:
max-rows: 500
这NamedParameterJdbcTemplate 重复使用相同的JdbcTemplate 实例。
如果有多个JdbcTemplate ,并且不存在主要候选者,则NamedParameterJdbcTemplate 未自动配置。 |
1.3. JPA 和 Spring Data JPA
Java Persistence API 是一种标准技术,允许您将对象 “映射” 到关系数据库。
这spring-boot-starter-data-jpa
POM 提供了一种快速入门方法。
它提供以下关键依赖项:
-
Hibernate:最流行的 JPA 实现之一。
-
Spring Data JPA:帮助您实施基于 JPA 的存储库。
-
Spring ORM:来自 Spring 框架的核心 ORM 支持。
我们在这里不详细介绍 JPA 或 Spring Data。 您可以按照 spring.io 中的“使用 JPA 访问数据”指南进行作,并阅读 Spring Data JPA 和 Hibernate 参考文档。 |
1.3.1. 实体类
传统上,JPA“实体”类在persistence.xml
文件。
使用 Spring Boot 时,此文件不是必需的,而是使用 “Entity Scanning” 代替。
默认情况下,将扫描自动配置包。
任何带有@Entity
,@Embeddable
或@MappedSuperclass
被考虑。
典型的实体类类似于以下示例:
@Entity
public class City implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
// ... additional members, often include @OneToMany mappings
protected City() {
// no-args constructor required by JPA spec
// this one is protected since it should not be used directly
}
public City(String name, String state) {
this.name = name;
this.state = state;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
// ... etc
}
@Entity
class City : Serializable {
@Id
@GeneratedValue
private val id: Long? = null
@Column(nullable = false)
var name: String? = null
private set
// ... etc
@Column(nullable = false)
var state: String? = null
private set
// ... additional members, often include @OneToMany mappings
protected constructor() {
// no-args constructor required by JPA spec
// this one is protected since it should not be used directly
}
constructor(name: String?, state: String?) {
this.name = name
this.state = state
}
}
您可以使用@EntityScan 注解。
请参阅“howto.html”作方法。 |
1.3.2. Spring Data JPA 存储库
Spring Data JPA 存储库是您可以定义用于访问数据的接口。
JPA 查询是根据您的方法名称自动创建的。
例如,CityRepository
interface 可能会声明findAllByState(String state)
方法查找给定州中的所有城市。
对于更复杂的查询,您可以使用 Spring Data 的Query
注解。
Spring Data 存储库通常从Repository
或CrudRepository
接口。
如果使用自动配置,则会在自动配置包中搜索存储库。
您可以使用@EnableJpaRepositories . |
以下示例显示了典型的 Spring Data 存储库接口定义:
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(String name, String state);
}
interface CityRepository : Repository<City?, Long?> {
fun findAll(pageable: Pageable?): Page<City?>?
fun findByNameAndStateAllIgnoringCase(name: String?, state: String?): City?
}
Spring Data JPA 存储库支持三种不同的引导模式:default、deferred 和 lazy。
要启用延迟或延迟引导,请将spring.data.jpa.repositories.bootstrap-mode
property 设置为deferred
或lazy
分别。
当使用延迟或延迟引导时,自动配置的EntityManagerFactoryBuilder
将使用上下文的AsyncTaskExecutor
(如果有)作为 Bootstrap 执行程序。
如果存在多个 Cookie,则名为applicationTaskExecutor
将被使用。
使用延迟或延迟引导时,请确保在应用程序上下文引导阶段之后推迟对 JPA 基础设施的任何访问。
您可以使用 |
我们只是触及了 Spring Data JPA 的皮毛。 有关完整详细信息,请参阅 Spring Data JPA 参考文档。 |
1.3.3. Spring Data Envers 存储库
如果 Spring Data Envers 可用,则 JPA 存储库将自动配置为支持典型的 Envers 查询。
要使用 Spring Data Envers,请确保您的存储库从RevisionRepository
如以下示例所示:
public interface CountryRepository extends RevisionRepository<Country, Long, Integer>, Repository<Country, Long> {
Page<Country> findAll(Pageable pageable);
}
interface CountryRepository :
RevisionRepository<Country?, Long?, Int>,
Repository<Country?, Long?> {
fun findAll(pageable: Pageable?): Page<Country?>?
}
有关更多详细信息,请查看 Spring Data Envers 参考文档。 |
1.3.4. 创建和删除 JPA 数据库
默认情况下,只有当您使用嵌入式数据库(H2、HSQL 或 Derby)时,才会自动创建 JPA 数据库。
您可以使用spring.jpa.*
性能。
例如,要创建和删除表,您可以将以下行添加到application.properties
:
spring.jpa.hibernate.ddl-auto=create-drop
spring:
jpa:
hibernate.ddl-auto: "create-drop"
Hibernate 自己的内部属性名称(如果你记得更清楚的话)是hibernate.hbm2ddl.auto .
你可以通过使用spring.jpa.properties.* (在将它们添加到 Entity Manager 之前,前缀会被去除)。
以下行显示了为 Hibernate 设置 JPA 属性的示例: |
spring.jpa.properties.hibernate[globally_quoted_identifiers]=true
spring:
jpa:
properties:
hibernate:
"globally_quoted_identifiers": "true"
前面示例中的行将true
对于hibernate.globally_quoted_identifiers
属性添加到 Hibernate 实体管理器中。
默认情况下,DDL 执行(或验证)会延迟到ApplicationContext
已启动。
1.3.5. 在 View 中打开 EntityManager
如果您正在运行 Web 应用程序,则 Spring Boot 默认会注册OpenEntityManagerInViewInterceptor
以应用“在 View 中打开 EntityManager”模式,以允许在 Web 视图中进行延迟加载。
如果您不希望此行为,则应将spring.jpa.open-in-view
自false
在application.properties
.
1.4. Spring Data JDBC
Spring Data 包括对 JDBC 的存储库支持,并将为CrudRepository
.
对于更高级的查询,一个@Query
annotation 提供。
当必要的依赖项位于 Classpath 上时, Spring Boot 将自动配置 Spring Data 的 JDBC 存储库。
它们可以添加到您的项目中,只需对spring-boot-starter-data-jdbc
.
如有必要,你可以通过添加@EnableJdbcRepositories
annotation 或AbstractJdbcConfiguration
子类添加到应用程序。
有关 Spring Data JDBC 的完整详细信息,请参阅参考文档。 |
1.5. 使用 H2 的 Web 控制台
-
您正在开发基于 servlet 的 Web 应用程序。
-
com.h2database:h2
位于 Classpath 上。 -
您正在使用 Spring Boot 的开发人员工具。
如果您没有使用 Spring Boot 的开发人员工具,但仍想使用 H2 的控制台,则可以配置spring.h2.console.enabled 值为true . |
H2 控制台仅用于开发期间,因此您应该注意确保spring.h2.console.enabled 未设置为true 在生产中。 |
1.5.2. 在受保护的应用程序中访问 H2 控制台
H2 控制台使用框架,因为它仅用于开发,所以不实施 CSRF 保护措施。 如果您的应用程序使用 Spring Security,则需要将其配置为
-
对针对控制台的请求禁用 CSRF 保护,
-
设置标题
X-Frame-Options
自SAMEORIGIN
on responses 来自控制台。
有关 CSRF 和标头 X-Frame-Options 的更多信息,请参见 Spring Security Reference Guide。
在简单设置中,SecurityFilterChain
可以使用如下:
@Profile("dev")
@Configuration(proxyBeanMethods = false)
public class DevProfileSecurityConfiguration {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SecurityFilterChain h2ConsoleSecurityFilterChain(HttpSecurity http) throws Exception {
http.securityMatcher(PathRequest.toH2Console());
http.authorizeHttpRequests(yourCustomAuthorization());
http.csrf((csrf) -> csrf.disable());
http.headers((headers) -> headers.frameOptions((frame) -> frame.sameOrigin()));
return http.build();
}
}
@Profile("dev")
@Configuration(proxyBeanMethods = false)
class DevProfileSecurityConfiguration {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
fun h2ConsoleSecurityFilterChain(http: HttpSecurity): SecurityFilterChain {
return http.authorizeHttpRequests(yourCustomAuthorization())
.csrf { csrf -> csrf.disable() }
.headers { headers -> headers.frameOptions { frameOptions -> frameOptions.sameOrigin() } }
.build()
}
}
H2 控制台仅用于开发期间。 在生产环境中,关闭 CSRF 保护或允许 Web 站点框架可能会造成严重的安全风险。 |
PathRequest.toH2Console() 当控制台的路径已自定义时,也会返回正确的 Request Matcher。 |
1.6. 使用 jOOQ
jOOQ 面向对象查询 (jOOQ) 是 Data Geekery 的一款热门产品,它从您的数据库生成 Java 代码,并允许您通过其 Fluent API 构建类型安全的 SQL 查询。 商业版和开源版都可以与 Spring Boot 一起使用。
1.6.1. 代码生成
为了使用 jOOQ 类型安全的查询,您需要从数据库架构生成 Java 类。
您可以按照 jOOQ 用户手册中的说明进行作。
如果您使用jooq-codegen-maven
plugin 中,您还可以使用spring-boot-starter-parent
“parent POM”,您可以安全地省略插件的<version>
标记。
您还可以使用 Spring Boot 定义的版本变量(例如h2.version
) 来声明插件的数据库依赖项。
下面的清单显示了一个示例:
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
...
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:~/yourdatabase</url>
</jdbc>
<generator>
...
</generator>
</configuration>
</plugin>
1.6.2. 使用 DSLContext
jOOQ 提供的 Fluent API 是通过org.jooq.DSLContext
接口。
Spring Boot 会自动配置DSLContext
作为 Spring Bean 并将其连接到您的应用程序DataSource
.
要使用DSLContext
,您可以注入它,如下例所示:
@Component
public class MyBean {
private final DSLContext create;
public MyBean(DSLContext dslContext) {
this.create = dslContext;
}
}
@Component
class MyBean(private val create: DSLContext) {
}
jOOQ 手册倾向于使用一个名为create 按住DSLContext . |
然后,您可以使用DSLContext
构建查询,如以下示例所示:
public List<GregorianCalendar> authorsBornAfter1980() {
return this.create.selectFrom(AUTHOR)
.where(AUTHOR.DATE_OF_BIRTH.greaterThan(new GregorianCalendar(1980, 0, 1)))
.fetch(AUTHOR.DATE_OF_BIRTH);
fun authorsBornAfter1980(): List<GregorianCalendar> {
return create.selectFrom<Tables.TAuthorRecord>(Tables.AUTHOR)
.where(Tables.AUTHOR?.DATE_OF_BIRTH?.greaterThan(GregorianCalendar(1980, 0, 1)))
.fetch(Tables.AUTHOR?.DATE_OF_BIRTH)
}
1.7. 使用 R2DBC
反应式关系数据库连接 (R2DBC) 项目为关系数据库带来了反应式编程 API。
R2DBC 的io.r2dbc.spi.Connection
提供使用非阻塞数据库连接的标准方法。
连接通过使用ConnectionFactory
,类似于DataSource
与 JDBC 一起使用。
ConnectionFactory
配置由spring.r2dbc.*
.
例如,您可以在application.properties
:
spring.r2dbc.url=r2dbc:postgresql://localhost/test
spring.r2dbc.username=dbuser
spring.r2dbc.password=dbpass
spring:
r2dbc:
url: "r2dbc:postgresql://localhost/test"
username: "dbuser"
password: "dbpass"
你不需要指定驱动程序类名,因为 Spring Boot 从 R2DBC 的 Connection Factory 发现中获取驱动程序。 |
至少应该提供 url。
URL 中指定的信息优先于单个属性,即name ,username ,password 和池化选项。 |
“作方法”部分包括有关如何初始化数据库的部分。 |
要自定义由ConnectionFactory
,即设置您不希望(或不能)在中央数据库配置中配置的特定参数,则可以使用ConnectionFactoryOptionsBuilderCustomizer
@Bean
.
以下示例显示了如何手动覆盖数据库端口,而其余选项则从应用程序配置中获取:
@Configuration(proxyBeanMethods = false)
public class MyR2dbcConfiguration {
@Bean
public ConnectionFactoryOptionsBuilderCustomizer connectionFactoryPortCustomizer() {
return (builder) -> builder.option(ConnectionFactoryOptions.PORT, 5432);
}
}
@Configuration(proxyBeanMethods = false)
class MyR2dbcConfiguration {
@Bean
fun connectionFactoryPortCustomizer(): ConnectionFactoryOptionsBuilderCustomizer {
return ConnectionFactoryOptionsBuilderCustomizer { builder ->
builder.option(ConnectionFactoryOptions.PORT, 5432)
}
}
}
以下示例显示如何设置一些 PostgreSQL 连接选项:
@Configuration(proxyBeanMethods = false)
public class MyPostgresR2dbcConfiguration {
@Bean
public ConnectionFactoryOptionsBuilderCustomizer postgresCustomizer() {
Map<String, String> options = new HashMap<>();
options.put("lock_timeout", "30s");
options.put("statement_timeout", "60s");
return (builder) -> builder.option(PostgresqlConnectionFactoryProvider.OPTIONS, options);
}
}
@Configuration(proxyBeanMethods = false)
class MyPostgresR2dbcConfiguration {
@Bean
fun postgresCustomizer(): ConnectionFactoryOptionsBuilderCustomizer {
val options: MutableMap<String, String> = HashMap()
options["lock_timeout"] = "30s"
options["statement_timeout"] = "60s"
return ConnectionFactoryOptionsBuilderCustomizer { builder ->
builder.option(PostgresqlConnectionFactoryProvider.OPTIONS, options)
}
}
}
当ConnectionFactory
bean 可用,则常规 JDBCDataSource
auto-configuration 回退。
如果要保留 JDBCDataSource
auto-configuration,并且愿意承担在响应式应用程序中使用阻塞 JDBC API 的风险,请将@Import(DataSourceAutoConfiguration.class)
在@Configuration
类以重新启用它。
1.7.1. 嵌入式数据库支持
与 JDBC 支持类似, Spring Boot 可以自动配置嵌入式数据库以进行反应式使用。 您无需提供任何连接 URL。 您只需包含要使用的嵌入式数据库的构建依赖项,如以下示例所示:
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>runtime</scope>
</dependency>
如果您在测试中使用此功能,您可能会注意到,无论您使用多少个应用程序上下文,整个测试套件都会重用同一个数据库。
如果要确保每个上下文都有一个单独的嵌入式数据库,则应将 |
1.7.2. 使用 DatabaseClient
一个DatabaseClient
bean 是自动配置的,您可以@Autowire
它直接集成到您自己的 bean 中,如以下示例所示:
@Component
public class MyBean {
private final DatabaseClient databaseClient;
public MyBean(DatabaseClient databaseClient) {
this.databaseClient = databaseClient;
}
}
@Component
class MyBean(private val databaseClient: DatabaseClient) {
}
1.7.3. Spring Data R2DBC 存储库
Spring Data R2DBC 存储库是您可以定义以访问数据的接口。
查询是根据您的方法名称自动创建的。
例如,CityRepository
interface 可能会声明findAllByState(String state)
方法查找给定州中的所有城市。
对于更复杂的查询,您可以使用 Spring Data 的Query
注解。
Spring Data 存储库通常从Repository
或CrudRepository
接口。
如果使用自动配置,则会在自动配置包中搜索存储库。
以下示例显示了典型的 Spring Data 存储库接口定义:
public interface CityRepository extends Repository<City, Long> {
Mono<City> findByNameAndStateAllIgnoringCase(String name, String state);
}
interface CityRepository : Repository<City?, Long?> {
fun findByNameAndStateAllIgnoringCase(name: String?, state: String?): Mono<City?>?
}
我们只是触及了 Spring Data R2DBC 的皮毛。有关完整详细信息,请参阅 Spring Data R2DBC 参考文档。 |
2. 使用 NoSQL 技术
Spring Data 提供了其他项目,可帮助您访问各种 NoSQL 技术,包括:
其中,Spring Boot 为 Cassandra、Couchbase、Elasticsearch、LDAP、MongoDB、Neo4J 和 Redis 提供了自动配置。 此外,Spring Boot for Apache Geode 还提供 Apache Geode 的自动配置。 您可以使用其他项目,但必须自己配置它们。 请参阅 spring.io/projects/spring-data 中的相应参考文档。
Spring Boot 还为 InfluxDB 客户端提供了自动配置。
2.1. Redis
Redis 是一个缓存、消息代理和功能丰富的键值存储。 Spring Boot 为 Lettuce 和 Jedis 客户端库以及 Spring Data Redis 提供的抽象提供了基本的自动配置。
有一个spring-boot-starter-data-redis
“Starter” 用于以方便的方式收集依赖项。
默认情况下,它使用 Lettuce。
该Starters可以处理传统和响应式应用程序。
我们还提供spring-boot-starter-data-redis-reactive “Starter” 用于与其他具有反应式支持的 store 保持一致。 |
2.1.1. 连接到 Redis
您可以注入一个自动配置的RedisConnectionFactory
,StringRedisTemplate
或 vanillaRedisTemplate
实例。
下面的清单显示了这种 bean 的一个示例:
@Component
public class MyBean {
private final StringRedisTemplate template;
public MyBean(StringRedisTemplate template) {
this.template = template;
}
}
@Component
class MyBean(private val template: StringRedisTemplate) {
}
默认情况下,实例尝试连接到localhost:6379
.
您可以使用spring.data.redis.*
属性,如以下示例所示:
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.database=0
spring.data.redis.username=user
spring.data.redis.password=secret
spring:
data:
redis:
host: "localhost"
port: 6379
database: 0
username: "user"
password: "secret"
您还可以注册任意数量的 bean 来实现LettuceClientConfigurationBuilderCustomizer 以获取更高级的自定义。ClientResources 也可以使用ClientResourcesBuilderCustomizer .
如果您使用 Jedis,JedisClientConfigurationBuilderCustomizer 也可用。
或者,您可以注册RedisStandaloneConfiguration ,RedisSentinelConfiguration 或RedisClusterConfiguration 以完全控制配置。 |
如果您添加自己的@Bean
中,它将替换默认的(除了RedisTemplate
,当排除项基于 Bean 名称时,redisTemplate
,而不是其类型)。
默认情况下,如果满足以下条件,则会自动配置池连接工厂commons-pool2
位于 Classpath 上。
自动配置的RedisConnectionFactory
可以通过设置以下示例所示的属性,将 SSL 配置为使用 SSL 与服务器通信:
spring.data.redis.ssl.enabled=true
spring:
data:
redis:
ssl:
enabled: true
自定义 SSL 信任材料可以在 SSL 捆绑包中配置,并应用于RedisConnectionFactory
如以下示例所示:
spring.data.redis.ssl.bundle=example
spring:
data:
redis:
ssl:
bundle: "example"
2.2. MongoDB 数据库
MongoDB 是一个开源 NoSQL 文档数据库,它使用类似 JSON 的架构,而不是传统的基于表的关系数据。
Spring Boot 为使用 MongoDB 提供了多种便利,包括spring-boot-starter-data-mongodb
和spring-boot-starter-data-mongodb-reactive
“Starters”。
2.2.1. 连接到 MongoDB 数据库
要访问 MongoDB 数据库,您可以注入一个自动配置的org.springframework.data.mongodb.MongoDatabaseFactory
.
默认情况下,实例尝试连接到 MongoDB 服务器mongodb://localhost/test
.
以下示例显示如何连接到 MongoDB 数据库:
@Component
public class MyBean {
private final MongoDatabaseFactory mongo;
public MyBean(MongoDatabaseFactory mongo) {
this.mongo = mongo;
}
}
@Component
class MyBean(private val mongo: MongoDatabaseFactory) {
}
如果您定义了自己的MongoClient
,它将用于自动配置合适的MongoDatabaseFactory
.
自动配置的MongoClient
是使用MongoClientSettings
豆。
如果您定义了自己的MongoClientSettings
,它将在不加修改的情况下使用,并且spring.data.mongodb
properties 将被忽略。
否则,一个MongoClientSettings
将被自动配置,并将具有spring.data.mongodb
属性。
无论哪种情况,您都可以声明一个或多个MongoClientSettingsBuilderCustomizer
bean 来微调MongoClientSettings
配置。
每个都将按顺序调用,并使用MongoClientSettings.Builder
用于构建MongoClientSettings
.
您可以设置spring.data.mongodb.uri
属性更改 URL 并配置其他设置,例如副本集,如以下示例所示:
spring.data.mongodb.uri=mongodb://user:[email protected]:27017,mongoserver2.example.com:23456/test
spring:
data:
mongodb:
uri: "mongodb://user:[email protected]:27017,mongoserver2.example.com:23456/test"
或者,您可以使用 discrete 属性指定连接详细信息。
例如,您可以在application.properties
:
spring.data.mongodb.host=mongoserver1.example.com
spring.data.mongodb.port=27017
spring.data.mongodb.additional-hosts[0]=mongoserver2.example.com:23456
spring.data.mongodb.database=test
spring.data.mongodb.username=user
spring.data.mongodb.password=secret
spring:
data:
mongodb:
host: "mongoserver1.example.com"
port: 27017
additional-hosts:
- "mongoserver2.example.com:23456"
database: "test"
username: "user"
password: "secret"
自动配置的MongoClient
可以通过设置以下示例所示的属性,将 SSL 配置为使用 SSL 与服务器通信:
spring.data.mongodb.uri=mongodb://user:[email protected]:27017,mongoserver2.example.com:23456/test
spring.data.mongodb.ssl.enabled=true
spring:
data:
mongodb:
uri: "mongodb://user:[email protected]:27017,mongoserver2.example.com:23456/test"
ssl:
enabled: true
自定义 SSL 信任材料可以在 SSL 捆绑包中配置,并应用于MongoClient
如以下示例所示:
spring.data.mongodb.uri=mongodb://user:[email protected]:27017,mongoserver2.example.com:23456/test
spring.data.mongodb.ssl.bundle=example
spring:
data:
mongodb:
uri: "mongodb://user:[email protected]:27017,mongoserver2.example.com:23456/test"
ssl:
bundle: "example"
如果 您还可以使用 |
如果您不使用 Spring Data MongoDB,则可以注入MongoClient bean 而不是使用MongoDatabaseFactory .
如果您想完全控制建立 MongoDB 连接,您还可以声明自己的MongoDatabaseFactory 或MongoClient 豆。 |
如果您使用的是反应式驱动程序,则 SSL 需要 Netty。 如果 Netty 可用并且要使用的工厂尚未自定义,则自动配置会自动配置此工厂。 |
2.2.2. MongoTemplate
Spring Data MongoDB 提供了一个MongoTemplate
类,该类在设计上与 Spring 的JdbcTemplate
.
与 一样JdbcTemplate
,Spring Boot 会自动配置一个 bean 供你注入模板,如下所示:
@Component
public class MyBean {
private final MongoTemplate mongoTemplate;
public MyBean(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
}
@Component
class MyBean(private val mongoTemplate: MongoTemplate) {
}
请参阅MongoOperations
Javadoc了解完整详情。
2.2.3. Spring Data MongoDB 存储库
Spring Data 包括对 MongoDB 的存储库支持。 与前面讨论的 JPA 存储库一样,基本原则是查询是根据方法名称自动构造的。
事实上,Spring Data JPA 和 Spring Data MongoDB 共享相同的通用基础设施。
您可以采用前面的 JPA 示例,并假设City
现在是 MongoDB 数据类,而不是 JPA@Entity
,它的工作方式相同,如以下示例所示:
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(String name, String state);
}
interface CityRepository :
Repository<City?, Long?> {
fun findAll(pageable: Pageable?): Page<City?>?
fun findByNameAndStateAllIgnoringCase(name: String?, state: String?): City?
}
通过扫描找到存储库和文档。
默认情况下,将扫描自动配置包。
您可以使用@EnableMongoRepositories
和@EntityScan
分别。
有关 Spring Data MongoDB 的完整详细信息,包括其丰富的对象映射技术,请参阅其参考文档。 |
2.3. Neo4j
Neo4j 是一个开源的 NoSQL 图形数据库,它使用由第一类关系连接的节点的丰富数据模型,与传统的 RDBMS 方法相比,它更适合连接的大数据。
Spring Boot 为使用 Neo4j 提供了多种便利,包括spring-boot-starter-data-neo4j
“Starters”。
2.3.1. 连接到 Neo4j 数据库
要访问 Neo4j 服务器,您可以注入一个自动配置的org.neo4j.driver.Driver
.
默认情况下,实例尝试连接到 Neo4j 服务器localhost:7687
使用 Bolt 协议。
以下示例显示了如何注入 Neo4jDriver
这使您能够访问Session
:
@Component
public class MyBean {
private final Driver driver;
public MyBean(Driver driver) {
this.driver = driver;
}
}
@Component
class MyBean(private val driver: Driver) {
}
您可以使用spring.neo4j.*
性能。
以下示例显示如何配置要使用的 uri 和凭证:
spring.neo4j.uri=bolt://my-server:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=secret
spring:
neo4j:
uri: "bolt://my-server:7687"
authentication:
username: "neo4j"
password: "secret"
自动配置的Driver
是使用ConfigBuilder
.
要微调其配置,请声明一个或多个ConfigBuilderCustomizer
豆。
每个都将按顺序调用,并使用ConfigBuilder
用于构建Driver
.
2.3.2. Spring Data Neo4j 存储库
Spring Data 包括对 Neo4j 的存储库支持。 有关 Spring Data Neo4j 的完整详细信息,请参阅参考文档。
与许多其他 Spring Data 模块一样,Spring Data Neo4j 与 Spring Data JPA 共享公共基础设施。
您可以采用前面的 JPA 示例并定义City
作为 Spring Data Neo4j@Node
而不是 JPA@Entity
存储库抽象的工作方式相同,如以下示例所示:
public interface CityRepository extends Neo4jRepository<City, Long> {
Optional<City> findOneByNameAndState(String name, String state);
}
interface CityRepository : Neo4jRepository<City?, Long?> {
fun findOneByNameAndState(name: String?, state: String?): Optional<City?>?
}
这spring-boot-starter-data-neo4j
“Starter” 启用存储库支持以及事务管理。
Spring Boot 支持经典和反应式 Neo4j 存储库,使用Neo4jTemplate
或ReactiveNeo4jTemplate
豆。
当 Project Reactor 在 Classpath 上可用时,反应式样式也会自动配置。
通过扫描找到存储库和实体。
默认情况下,将扫描自动配置包。
您可以使用@EnableNeo4jRepositories
和@EntityScan
分别。
在使用响应式样式的应用程序中, Java
Kotlin
|
2.4. Elasticsearch
Elasticsearch 是一个开源的分布式 RESTful 搜索和分析引擎。 Spring Boot 为 Elasticsearch 客户端提供了基本的自动配置。
Spring Boot 支持多个客户端:
-
官方的低级 REST 客户端
-
官方 Java API 客户端
-
这
ReactiveElasticsearchClient
由 Spring Data Elasticsearch 提供
Spring Boot 提供了一个专用的 “Starter”,spring-boot-starter-data-elasticsearch
.
2.4.1. 使用 REST 客户端连接到 Elasticsearch
Elasticsearch 提供了两个不同的 REST 客户端,您可以使用它们来查询集群:来自org.elasticsearch.client:elasticsearch-rest-client
模块和 Java API 客户端。co.elastic.clients:elasticsearch-java
模块。
此外,Spring Boot 还为来自org.springframework.data:spring-data-elasticsearch
模块。
默认情况下,客户端将localhost:9200
.
您可以使用spring.elasticsearch.*
properties 进一步优化客户端的配置方式,如以下示例所示:
spring.elasticsearch.uris=https://search.example.com:9200
spring.elasticsearch.socket-timeout=10s
spring.elasticsearch.username=user
spring.elasticsearch.password=secret
spring:
elasticsearch:
uris: "https://search.example.com:9200"
socket-timeout: "10s"
username: "user"
password: "secret"
使用 RestClient 连接到 Elasticsearch
如果你有elasticsearch-rest-client
在 Classpath 上,Spring Boot 将自动配置并注册一个RestClient
豆。
除了前面描述的属性之外,要微调RestClient
您可以注册任意数量的 bean 来实现RestClientBuilderCustomizer
以获取更高级的自定义。
要完全控制客户端的配置,请定义RestClientBuilder
豆。
此外,如果elasticsearch-rest-client-sniffer
位于 Classpath 上,则Sniffer
自动配置为从正在运行的 Elasticsearch 集群中自动发现节点,并在RestClient
豆。
您可以进一步调整Sniffer
,如以下示例所示:
spring.elasticsearch.restclient.sniffer.interval=10m
spring.elasticsearch.restclient.sniffer.delay-after-failure=30s
spring:
elasticsearch:
restclient:
sniffer:
interval: "10m"
delay-after-failure: "30s"
使用 ElasticsearchClient 连接到 Elasticsearch
如果你有co.elastic.clients:elasticsearch-java
在 classpath 上,Spring Boot 将自动配置并注册一个ElasticsearchClient
豆。
这ElasticsearchClient
使用依赖于前面描述的RestClient
.
因此,前面描述的属性可用于配置ElasticsearchClient
.
此外,您还可以定义TransportOptions
bean 来进一步控制 transport 的行为。
使用 ReactiveElasticsearchClient 连接到 Elasticsearch
Spring Data Elasticsearch 附带ReactiveElasticsearchClient
用于以反应方式查询 Elasticsearch 实例。
如果你在 Classpath 上有 Spring Data Elasticsearch 和 Reactor,则 Spring Boot 将自动配置并注册一个ReactiveElasticsearchClient
.
这ReactiveElasticsearchclient
使用依赖于前面描述的RestClient
.
因此,前面描述的属性可用于配置ReactiveElasticsearchClient
.
此外,您还可以定义TransportOptions
bean 来进一步控制 transport 的行为。
2.4.2. 使用 Spring Data 连接到 Elasticsearch
要连接到 Elasticsearch,请使用ElasticsearchClient
bean 的
由 Spring Boot 自动配置或由应用程序手动提供(请参阅前面的部分)。
有了这个配置,一个ElasticsearchTemplate
可以像任何其他 Spring bean 一样注入,
如以下示例所示:
@Component
public class MyBean {
private final ElasticsearchTemplate template;
public MyBean(ElasticsearchTemplate template) {
this.template = template;
}
}
@Component
class MyBean(private val template: org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate ) {
}
在场spring-data-elasticsearch
和 Reactor 中,Spring Boot 还可以自动配置ReactiveElasticsearchClient和ReactiveElasticsearchTemplate
作为豆子。
它们是其他 REST 客户端的反应式等效项。
2.4.3. Spring Data Elasticsearch 存储库
Spring Data 包括对 Elasticsearch 的存储库支持。 与前面讨论的 JPA 存储库一样,基本原则是根据方法名称自动为您构建查询。
事实上,Spring Data JPA 和 Spring Data Elasticsearch 共享相同的通用基础架构。
您可以采用前面的 JPA 示例,并假设City
现在是 Elasticsearch@Document
类而不是 JPA@Entity
,它的工作方式相同。
通过扫描找到存储库和文档。
默认情况下,将扫描自动配置包。
您可以使用@EnableElasticsearchRepositories
和@EntityScan
分别。
有关 Spring Data Elasticsearch 的完整详细信息,请参阅参考文档。 |
Spring Boot 支持经典和反应式 Elasticsearch 存储库,使用ElasticsearchRestTemplate
或ReactiveElasticsearchTemplate
豆。
如果存在所需的依赖项,这些 bean 很可能是由 Spring Boot 自动配置的。
如果您希望使用自己的模板来支持 Elasticsearch 存储库,您可以添加自己的模板ElasticsearchRestTemplate
或ElasticsearchOperations
@Bean
,只要它被命名为"elasticsearchTemplate"
.
这同样适用于ReactiveElasticsearchTemplate
和ReactiveElasticsearchOperations
,其中 bean 名称"reactiveElasticsearchTemplate"
.
您可以选择使用以下属性禁用存储库支持:
spring.data.elasticsearch.repositories.enabled=false
spring:
data:
elasticsearch:
repositories:
enabled: false
2.5. Cassandra
Cassandra 是一个开源的分布式数据库管理系统,旨在处理跨许多商用服务器的大量数据。
Spring Boot 为 Cassandra 提供了自动配置,以及 Spring Data Cassandra 提供的抽象。
有一个spring-boot-starter-data-cassandra
“Starter” 用于以方便的方式收集依赖项。
2.5.1. 连接到 Cassandra
您可以注入一个自动配置的CassandraTemplate
或 CassandraCqlSession
实例。
这spring.cassandra.*
properties 可用于自定义连接。
通常,您需要提供keyspace-name
和contact-points
以及本地数据中心名称,如以下示例所示:
spring.cassandra.keyspace-name=mykeyspace
spring.cassandra.contact-points=cassandrahost1:9042,cassandrahost2:9042
spring.cassandra.local-datacenter=datacenter1
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1:9042,cassandrahost2:9042"
local-datacenter: "datacenter1"
如果所有联系点的端口都相同,则可以使用快捷方式,并且仅指定主机名,如以下示例所示:
spring.cassandra.keyspace-name=mykeyspace
spring.cassandra.contact-points=cassandrahost1,cassandrahost2
spring.cassandra.local-datacenter=datacenter1
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
这两个示例与默认为9042 .
如果需要配置端口,请使用spring.cassandra.port . |
自动配置的CqlSession
可以通过设置以下示例所示的属性,将 SSL 配置为使用 SSL 与服务器通信:
spring.cassandra.keyspace-name=mykeyspace
spring.cassandra.contact-points=cassandrahost1,cassandrahost2
spring.cassandra.local-datacenter=datacenter1
spring.cassandra.ssl.enabled=true
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
ssl:
enabled: true
自定义 SSL 信任材料可以在 SSL 捆绑包中配置,并应用于CqlSession
如以下示例所示:
spring.cassandra.keyspace-name=mykeyspace
spring.cassandra.contact-points=cassandrahost1,cassandrahost2
spring.cassandra.local-datacenter=datacenter1
spring.cassandra.ssl.bundle=example
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
ssl:
bundle: "example"
Cassandra 驱动程序有自己的配置基础设施,用于加载 默认情况下,Spring Boot 不会查找此类文件,但可以使用 对于更高级的驱动程序自定义,您可以注册任意数量的 bean 来实现 |
如果您使用CqlSessionBuilder 创建多个CqlSession bean,请记住 builder 是可变的,因此请确保为每个会话注入一个新的副本。 |
下面的代码清单显示了如何注入 Cassandra bean:
@Component
public class MyBean {
private final CassandraTemplate template;
public MyBean(CassandraTemplate template) {
this.template = template;
}
}
@Component
class MyBean(private val template: CassandraTemplate) {
}
如果您添加自己的@Bean
的类型CassandraTemplate
,它将替换默认值。
2.6. Couchbase
Couchbase 是一个开源、分布式、多模型、面向文档的 NoSQL 数据库,针对交互式应用程序进行了优化。
Spring Boot 为 Couchbase 提供了自动配置,并且 Spring Data Couchbase 提供了它之上的抽象。
有spring-boot-starter-data-couchbase
和spring-boot-starter-data-couchbase-reactive
“Starters” 用于以方便的方式收集依赖项。
2.6.1. 连接到 Couchbase
您可以获得Cluster
通过添加 Couchbase SDK 和一些配置。
这spring.couchbase.*
properties 可用于自定义连接。
通常,您需要提供连接字符串、用户名和密码,如以下示例所示:
spring.couchbase.connection-string=couchbase://192.168.1.123
spring.couchbase.username=user
spring.couchbase.password=secret
spring:
couchbase:
connection-string: "couchbase://192.168.1.123"
username: "user"
password: "secret"
也可以自定义一些ClusterEnvironment
设置。
例如,以下配置更改了超时以打开新的Bucket
并通过引用已配置的 SSL 捆绑包来启用 SSL 支持:
spring.couchbase.env.timeouts.connect=3s
spring.couchbase.env.ssl.bundle=example
spring:
couchbase:
env:
timeouts:
connect: "3s"
ssl:
bundle: "example"
检查spring.couchbase.env.* properties 了解更多详情。
要获得更多控制权,请一个或多个ClusterEnvironmentBuilderCustomizer 可以使用 beans。 |
2.6.2. Spring Data Couchbase 存储库
Spring Data 包括对 Couchbase 的存储库支持。
通过扫描找到存储库和文档。
默认情况下,将扫描自动配置包。
您可以使用@EnableCouchbaseRepositories
和@EntityScan
分别。
有关 Spring Data Couchbase 的完整详细信息,请参阅参考文档。
您可以注入一个自动配置的CouchbaseTemplate
实例,前提是CouchbaseClientFactory
Bean 可用。
这种情况发生在Cluster
可用,并且已指定存储桶名称:
spring.data.couchbase.bucket-name=my-bucket
spring:
data:
couchbase:
bucket-name: "my-bucket"
以下示例显示了如何注入CouchbaseTemplate
豆:
@Component
public class MyBean {
private final CouchbaseTemplate template;
public MyBean(CouchbaseTemplate template) {
this.template = template;
}
}
@Component
class MyBean(private val template: CouchbaseTemplate) {
}
您可以在自己的配置中定义一些 bean 来覆盖 auto-configuration 提供的 bean:
-
一个
CouchbaseMappingContext
@Bean
替换为 namecouchbaseMappingContext
. -
一个
CustomConversions
@Bean
替换为 namecouchbaseCustomConversions
. -
一个
CouchbaseTemplate
@Bean
替换为 namecouchbaseTemplate
.
为避免在您自己的配置中对这些名称进行硬编码,您可以重复使用BeanNames
由 Spring Data Couchbase 提供。
例如,您可以自定义要使用的转换器,如下所示:
@Configuration(proxyBeanMethods = false)
public class MyCouchbaseConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
public CouchbaseCustomConversions myCustomConversions() {
return new CouchbaseCustomConversions(Arrays.asList(new MyConverter()));
}
}
@Configuration(proxyBeanMethods = false)
class MyCouchbaseConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
fun myCustomConversions(): CouchbaseCustomConversions {
return CouchbaseCustomConversions(Arrays.asList(MyConverter()))
}
}
2.7. LDAP 协议
LDAP(轻量级目录访问协议)是一种开放的、供应商中立的行业标准应用协议,用于通过 IP 网络访问和维护分布式目录信息服务。 Spring Boot 为任何兼容的 LDAP 服务器提供自动配置,并支持来自 UnboundID 的嵌入式内存 LDAP 服务器。
LDAP 抽象由 Spring Data LDAP 提供。
有一个spring-boot-starter-data-ldap
“Starter” 用于以方便的方式收集依赖项。
2.7.1. 连接到 LDAP 服务器
要连接到 LDAP 服务器,请确保声明对spring-boot-starter-data-ldap
“Starter” 或spring-ldap-core
然后在 application.properties 中声明服务器的 URL,如以下示例所示:
spring.ldap.urls=ldap://myserver:1235
spring.ldap.username=admin
spring.ldap.password=secret
spring:
ldap:
urls: "ldap://myserver:1235"
username: "admin"
password: "secret"
如果您需要自定义连接设置,可以使用spring.ldap.base
和spring.ldap.base-environment
性能。
一LdapContextSource
根据这些设置进行自动配置。
如果DirContextAuthenticationStrategy
bean 可用,则它与自动配置的LdapContextSource
.
如果您需要自定义它,例如使用PooledContextSource
,您仍然可以注入自动配置的LdapContextSource
.
请务必标记您的自定义ContextSource
如@Primary
这样 auto-configuredLdapTemplate
使用它。
2.7.2. Spring Data LDAP 存储库
Spring Data 包括对 LDAP 的存储库支持。
通过扫描找到存储库和文档。
默认情况下,将扫描自动配置包。
您可以使用@EnableLdapRepositories
和@EntityScan
分别。
有关 Spring Data LDAP 的完整详细信息,请参阅参考文档。
您还可以注入一个自动配置的LdapTemplate
实例,如以下示例所示:
@Component
public class MyBean {
private final LdapTemplate template;
public MyBean(LdapTemplate template) {
this.template = template;
}
}
@Component
class MyBean(private val template: LdapTemplate) {
}
2.7.3. 嵌入式内存 LDAP 服务器
出于测试目的, Spring Boot 支持从 UnboundID 自动配置内存中的 LDAP 服务器。
要配置服务器,请将依赖项添加到com.unboundid:unboundid-ldapsdk
并声明一个spring.ldap.embedded.base-dn
属性,如下所示:
spring.ldap.embedded.base-dn=dc=spring,dc=io
spring:
ldap:
embedded:
base-dn: "dc=spring,dc=io"
可以定义多个 base-dn 值,但是,由于专有名称通常包含逗号,因此必须使用正确的表示法来定义它们。 在 yaml 文件中,您可以使用 yaml 列表表示法。在属性文件中,必须将索引作为属性名称的一部分包含在内: 性能
Yaml
|
默认情况下,服务器在随机端口上启动并触发常规 LDAP 支持。
无需指定spring.ldap.urls
财产。
如果存在schema.ldif
file 中,它用于初始化服务器。
如果要从其他资源加载初始化脚本,还可以使用spring.ldap.embedded.ldif
财产。
默认情况下,使用标准 schema 进行验证LDIF
文件。
您可以通过将spring.ldap.embedded.validation.enabled
财产。
如果您有自定义属性,则可以使用spring.ldap.embedded.validation.schema
以定义自定义属性类型或对象类。
2.8. InfluxDB 数据库
InfluxDB 是一个开源时间序列数据库,针对作监控、应用程序指标、物联网传感器数据和实时分析等领域的时间序列数据的快速、高可用性存储和检索进行了优化。
2.8.1. 连接到 InfluxDB
Spring Boot 会自动配置InfluxDB
实例,前提是influxdb-java
client位于 Classpath 上,并且设置了数据库的 URL,如以下示例所示:
spring.influx.url=https://172.0.0.1:8086
spring:
influx:
url: "https://172.0.0.1:8086"
如果连接到 InfluxDB 需要用户名和密码,则可以设置spring.influx.user
和spring.influx.password
属性。
InfluxDB 依赖于 OkHttp。
如果您需要调优 http 客户端InfluxDB
在后台使用,您可以注册一个InfluxDbOkHttpClientBuilderProvider
豆。
如果您需要对配置进行更多控制,请考虑注册一个InfluxDbCustomizer
豆。
3. 下一步要读什么
现在,您应该对如何将 Spring Boot 与各种数据技术结合使用有所了解。 从这里,您可以了解 Spring Boot 对各种消息传递技术的支持,以及如何在您的应用程序中启用它们。