小编Nir*_*ane的帖子

在java 10中使用局部变量类型推断的限制

Java 10引入了局部变量类型推断功能JEP-286.

我们可以使用Local-Variable Type Inference var作为保留类型名称

但是使用它有一些限制.

有人可以总结一下我将无法使用var的情况吗?

java java-10

6
推荐指数
1
解决办法
1436
查看次数

与 CompletableFuture 中的异常处理等效的 finally 块

我有CompletableFuture哪个可以返回结果或异常。我想在出现异常和正常结果的情况下执行一些通用代码。类似于try catch finally

当前实施

CompletableFuture<Integer> future= CompletableFuture.supplyAsync(this::findAccountNumber)
                             .thenApply(this::calculateBalance)                      
                             .thenApply(this::notifyBalance)
                             .exceptionally(ex -> {
                                 //My Exception Handling logic 
                                 return 0;
                             });
Run Code Online (Sandbox Code Playgroud)

我可以把我的最终逻辑放在哪里?

java-8 completable-future

5
推荐指数
1
解决办法
1892
查看次数

测试容器;在 docker 中运行 @Testcontainers 测试 [在 Docker 中运行 Docker]

如何@Testcontainers在 docker 容器内运行基于测试用例?

我有一个简单的 Spring Boot 应用程序,它具有使用Testcontainers. 测试用例从外部容器(本地机器)破坏得很好。

我们在容器中运行所有东西,构建在 docker jenkins 镜像上运行。Docker 文件正在创建 jar,然后创建映像。@Testcontainers找不到安装的docker。下面是我的 docker 文件。

FROM maven:3.6-jdk-11-openj9
VOLUME ["/var/run/docker.sock"]
RUN apt-get update
RUN apt-get -y install docker.io
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN mvn -Dmaven.repo.local=/root/m2 --batch-mode -f pom.xml clean package
EXPOSE 8080
CMD ["/bin/bash"]
Run Code Online (Sandbox Code Playgroud)

在运行构建时,我低于错误

org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy - ping failed with configuration Environment variables, system properties and defaults. Resolved dockerHost=unix:///var/run/docker.sock due to org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception

处理这种情况的最佳方法是什么?我想在 mvn 构建阶段使用 docker 文件运行我的组件级集成测试。 …

testcontainers

5
推荐指数
1
解决办法
4844
查看次数

如何使用Spring Batch解析固定长度格式的文件(无分隔符的文件)

如何为固定长度格式的文件配置spring-batch阅读器(无分隔符的文件)。

每个元素都是根据其开始和结束位置确定的。

线样:

120180208FAILED
220180208SUCCES
120170208SUCCES
1:代码,20180208:日期,FAILED:状态

spring spring-batch

4
推荐指数
1
解决办法
4442
查看次数

stream.foreach中的java 8局部变量

我想在lambda函数中使用局部变量但我得到错误:请参阅代码中的1.和2.点.

class Foo {
    int d = 0; // 1. It compiles, but ugly, 2. doesnt compile
    public void findMax(List<List<Route>> routeLists) {
        int d = 0; // 2.Error : Local variable dd defined in an enclosing scope must be final or effectively final
        routeLists.forEach(e-> {
            e.forEach(ee -> {
                d+=ee.getDistance();    
            });

        });
        ... doing some other operation with d
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能使用它们将它们设置为全局变量?

java lambda local-variables java-8 java-stream

4
推荐指数
2
解决办法
6916
查看次数

需要公共VS之间有什么区别需要Java 9中的传递

我已经了解到,为了使用任何其他导出的模块,我们需要将该模块指定为requires:

module mymodule{
       requires othermodule.xyz;
}
Run Code Online (Sandbox Code Playgroud)

othermodule使用thirdmodulemymodule需要时, othermodule应该像这样定义传递依赖:

module othermodule {
       requires transitive thirdmodule  
}
Run Code Online (Sandbox Code Playgroud)

但是,我见过许多使用该public关键字的网站:

module othermodule {
       requires public  thirdmodule  
}
Run Code Online (Sandbox Code Playgroud)

两种形式之间有什么区别; 即requires publicrequires transitive

java java-9

3
推荐指数
1
解决办法
290
查看次数

如何在stream.collect(Collectors)中指定具体的集合类型

我们如何从 Stream.collect() 方法创建一些具体的集合类型

例如,对于下面的示例,我想创建 LinkedList 而不是通用列表。

List<Integer> list = Arrays.asList(10,20,30,40);
        List<Integer> collect3 = list.stream().filter(i ->i%2==0).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

我该如何指定,我需要创建LinkedList

java-8 java-stream

3
推荐指数
1
解决办法
1189
查看次数

Spring 5 :optional injection points using @Nullable

Spring 5 have added support Null-safety of Spring APIs. Now We can also use @Nullable to indicate optional injection points.

But i am not able to understand use case when we should use @Nullable dependency ?

spring documentation does not have examples about the @Nullable dependency

@Component
    public class SomeClass {

        @Nullable
        @Autowired
        private MyService service;  

        public void someMethod()
        {
            service.someMethod();       
        } 

    }
Run Code Online (Sandbox Code Playgroud)

spring nullable

3
推荐指数
1
解决办法
2225
查看次数

java 9 OR和java 8 orElseGet之间的区别

Java9 .or为Optional 添加了方法.现有的不同之处是.orElseGet什么?

checkUserInMemory(userId).or(() -> checkUserInDB(userId));
Run Code Online (Sandbox Code Playgroud)

java optional java-8 java-9

3
推荐指数
2
解决办法
556
查看次数

如何使用在JUnit 5的其他类中定义的@MethodSource

有没有办法使用@MethodSource 其他类中定义的方法?

例如下面的代码有效,因为该stringProvider方法在同一类中定义。

 @ParameterizedTest
    @MethodSource("stringProvider")
    void methodSourceFromOtherClass(String word)
    {
        System.out.println(word);
        assertNotNull(word);
    }

public static Stream<Arguments> stringProvider()
    {
        return Stream.of(
                Arguments.of("Values"),
                Arguments.of("From"),
                Arguments.of("MethodSource"));

    }
Run Code Online (Sandbox Code Playgroud)

我有一些实用程序类,可提供测试数据。如何在中使用外部类中的方法@methodSource

junit5

3
推荐指数
1
解决办法
258
查看次数