JiaHe

相遇即是缘

1. Overview

In our previous guide to @ConfigurationProperties, we learned how to set up and use the @ConfigurationProperties annotation with Spring Boot for working with external configuration.

In this tutorial, we'll show how to test configuration classes that rely on the @ConfigurationProperties annotation to make sure that our configuration data is loaded and bound correctly to their corresponding fields.

2. Dependencies

In our Maven project, we'll use the [*spring-boot-starter*](https://search.maven.org/classic/#search|ga|1|g%3A"org.springframework.boot" AND a%3A"spring-boot-starter") and [*spring-boot-starter-test*](https://search.maven.org/classic/#search|ga|1|g%3A"org.springframework.boot" AND a%3A"spring-boot-starter-test") dependencies to enable the core spring API and Spring's test API. Additionally, we'll use [*spring-boot-starter-validation*](https://search.maven.org/classic/#search|ga|1|g%3A"org.springframework.boot" AND a%3A"spring-boot-starter-validation") as the bean validation dependency:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
阅读全文 »

1. Overview

Collectors were added in Java 8 which helped accumulate input elements into mutable containers such as Map, List, and Set.

In this article, we're going to explore two new collectors added in Java 9: Collectors.filtering and Collectors.flatMapping used in combination with Collectors.groupingBy providing intelligent collections of elements.

2. Filtering Collector

The Collectors.filtering is similar to the Stream filter(); it's used for filtering input elements but used for different scenarios. The Stream's filter is used in the stream chain whereas the filtering is a Collector which was designed to be used along with grouping By.

Collectors.filteringStream filter() 有点像,它用于过滤输入元素,但用于不同的场景下。Stream's filter 被用于stream链,而 Collectors.filtering 被设计为与 grouping By结合使用。

With Stream's filter, the values are filtered first and then it's grouped. In this way, the values which are filtered out are gone and there is no trace of it. If we need a trace then we would need to group first and then apply filtering which actually the Collectors.filtering does.

使用 Stream's filter时,输入元素首先会被过滤掉,然后才会被分组。如果我们需要在分组之后才对其进行过滤的话,应该使用 Collectors.filtering

The Collectors.filtering takes a function for filtering the input elements and a collector to collect the filtered elements:

Collectors.filtering 使用函数来过滤和收集过滤后的元素:

@Test
public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
List<Integer> numbers = List.of(1, 2, 3, 5, 5);

Map<Integer, Long> result = numbers.stream()
// 此时stream中只有[5, 5]
.filter(val -> val > 3)
// 然后分组统计,这将会失去对过滤掉元素的统计结果,即使为0
.collect(Collectors.groupingBy(i -> i, Collectors.counting()));

// result => { 5:2 }
assertEquals(1, result.size());

result = numbers.stream()
// 先进行分组[ [1], [2], [3], [5, 5] ]
.collect(Collectors.groupingBy(i -> i,
// 然后过滤掉出每组中大于3的值进行统计
Collectors.filtering(val -> val > 3, Collectors.counting())));

// result => { 1:0, 2:0, 3:0, 5:2 }
assertEquals(4, result.size());
}
阅读全文 »

原文地址:https://www.baeldung.com/java-9-modularity

以下中文翻译为作者根据自己的理解+Google+YouDao作为参考翻译而得,存在偏颇之处请读者查看对应原文,自行理解。

1. Overview

Java 9 introduces a new level of abstraction above packages, formally known as the Java Platform Module System (JPMS), or “Modules” for short.

Java 9基于package引入了一个新的抽象级别,正式称为Java平台模块系统(JPMS)或“模块”。

In this tutorial, we'll go through the new system and discuss its various aspects.

在本教程中,我们将通过一个新系统来探索其各个方面。

We'll also build a simple project to demonstrate all concepts we'll be learning in this guide.

我们也将构建一个简单的工程来演示我们在本教程中的学到的所有的概念。

阅读全文 »

原文地址:https://www.baeldung.com/project-jigsaw-java-modularity

以下中文翻译为作者根据自己的理解+Google+YouDao作为参考翻译而得,存在偏颇之处请读者查看对应原文,自行理解。

1. Introduction

Project Jigsaw is an umbrella project with the new features aimed at two aspects:

Project Jigsaw 作为一个大项目包含以下两个方面的新特性:

  • the introduction of module system in the Java language
  • 在Java中引入模块系统
  • and its implementation in JDK source and Java runtime
  • 以及JDK源码和Java运行环境的具体实现

In this article, we'll introduce you to the Jigsaw project and its features and finally wrap it up with a simple modular application.

本文中,我们将介绍Jigsaw及其一些特性,并且通过一个简单的模块化应用来入门。

阅读全文 »

<dependencies>
<!-- jdk9+ requires following dependencies to use jaxb -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
module-info.java
module module.name {

requires static transitive java.naming;
requires transitive com.sun.xml.bind;
requires java.annotation;

// 使用到JAXB相关的包暴露给jaxb
opens cn.xiaojianzheng.xxx to com.sun.xml.bind, com.sun.xml.bind.core, jakarta.xml.bind;

}