小编Nam*_*man的帖子

没有找到带有 Java 记录和 BeanPropertyRowMapper 的默认构造函数

我正在使用新的 Java 14 和 Spring Boot。对于数据持有者,我使用了新的很酷的记录,而不是常规的 Java 类。

public record City(Long id, String name, Integer population) {}
Run Code Online (Sandbox Code Playgroud)

稍后在我的服务类中,我使用 SpringBeanPropertyRowMapper来获取数据。

@Override
public City findById(Long id) {
    String sql = "SELECT * FROM cities WHERE id = ?";
    return jtm.queryForObject(sql, new Object[]{id},
            new BeanPropertyRowMapper<>(City.class));
}
Run Code Online (Sandbox Code Playgroud)

我最终出现以下错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zetcode.model.City]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zetcode.model.City.<init>()
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:145) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
Run Code Online (Sandbox Code Playgroud)

如何为记录添加默认构造函数或有其他方法可以解决此问题?

java spring-boot java-14 java-record

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

Maven 程序集插件 - 多版本:true

我的多 jar 应用程序在 Java 11 中运行并显示与 Log4j2 相关的警告:

警告:不支持 sun.reflect.Reflection.getCallerClass。这会影响性能。

它没有崩溃,但很困扰我,因为运营团队(AppDynamics 监视器)向我询问了它。我读到我需要在清单中使用“Multi-Release:true”条目,但我不知道如何告诉 Maven Assembly Plugin 添加它。

我没有在 pom.xml 中使用任何其他插件。我应该使用Maven Shade Plugin吗?

无论如何,这是我的 pom.xml 的 Maven Assembly Plugin 部分。

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.2.0</version>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我包含的库(我也写过)使用 Log4j 2 作为依赖项,如下所示:

<!-- Log4j 2 -->

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.12.1</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.12.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我怎样才能摆脱这个警告?

java maven log4j2 java-11

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

是否可以在创建它的同一线程中运行一个可完成的未来?

想知道 completablefuture 是否可以在创建它的线程中运行。您可能会问为什么我需要这样做,因为 completablefuture 是用于异步编程的。原因是我有一些异步任务和一些我想在生成线程中运行的任务,以便我可以使用 allOf 等并保持代码的一致性

java java-8 completable-future

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

无法在java 11中生成GC日志

我正在尝试在 java 11 中不存在的文件夹中创建 gc 日志。xxx 文件夹不存在。

C:\>java -Xlog:gc*:file=C:\Users\xxx\gc.log --version

[0.006s][error][logging] Error opening log file 'C:\Users\xxx\gc.log': No such file or directory
[0.006s][error][logging] Initialization of output 'file=C:\Users\xxx\gc.log' using options '(null)' failed.
Invalid -Xlog option '-Xlog:gc*:file=C:\Users\xxx\gc.log', see error log for details.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Run Code Online (Sandbox Code Playgroud)

如何获取gc不存在的文件夹中的日志?如果该文件夹存在,则工作正常。

java -version 

openjdk version "11.0.5" 2019-10-15 
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10) 
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)
Run Code Online (Sandbox Code Playgroud)

java garbage-collection java-11

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

从匿名类到 lambda 表达式

当使用下面的匿名类时,我们调用的变量x没有问题

interface Age { 
    int x = 21; 
    void getAge(); 
} 

class AnonymousDemo  { 
    public static void main(String[] args) { 
        Age oj1 = new Age() { 
            @Override
            public void getAge() { 
                // printing age 
                System.out.print("Age is "+x); 
            } 
        }; 
        oj1.getAge(); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

但是当我使用下面的 lambda 表达式相同的代码时,出现了异常:

interface Age { 
    int x = 21; 
    void getAge(); 
} 

class AnonymousDemo { 
    public static void main(String[] args) { 
        Age oj1 = () -> { System.out.print("Age is "+x); };
        oj1.getAge(); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

这里会出现什么问题呢?知道lambda表达式只是实现匿名类的缩写。

java lambda anonymous-class functional-interface

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

使用 if else 将每个转换为流

我有一个代码场景,我将每个转换为流,但我不确定这是否是我遵循的正确方法

这是每个循环:

List<ClassA> list=getList(); //Contains object of type ClassA
int a=0; int b=0; int c=0;
for(ClassA a: list) {
    String response=checkRespone(a.getId());
    if(reponse.equals("x"))  ++a;
    else if(reponse.equals("y")) ++b;
    else ++c;
}
Run Code Online (Sandbox Code Playgroud)

为了将其转换为流,我写了这个。有没有更好的方法来写这个?

AtomicInteger a = new AtomicInteger(0);
AtomicInteger b = new AtomicInteger(0);
AtomicInteger c = new AtomicInteger(0);

list.stream().forEach(ClassA->{if(checkRespone(a.getId()).equals("x")) {a.set(a.get()+1);} else if (checkRespone(a.getId()).equals("y")) {
    b.set(b.get()+1);
} else {
    c.set(c.get()+1);
}});
Run Code Online (Sandbox Code Playgroud)

java java-stream

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

为什么 Stream.mapToInt 不处理 NullPointerExceptions?

此代码将给出 NullPointerException。mapToInt (map) 方法不是应该处理 NPE 吗?

List<Integer> list = Arrays.asList(null, null);
OptionalDouble op = list.stream()
    .mapToInt(val->val)
    .average();
Run Code Online (Sandbox Code Playgroud)

java nullpointerexception java-8 java-stream

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

jOOQ with java 15:org.jooq 中的接口 org.jooq.Record 和 java.lang 中的类 java.lang.Record 匹配

我刚刚尝试将我的项目升级到 Java 15,现在出现以下错误:

  both interface org.jooq.Record in org.jooq and class java.lang.Record in java.lang match
Run Code Online (Sandbox Code Playgroud)

有没有人有解决这个问题的经验?

java jooq java-record java-15

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

为什么不使用自定义构造函数推断可变参数记录组件?

尝试使用record和记录组件的一些代码。我正在使用变量 rarity 组件,并且在自定义构造函数上遇到了编译时错误。

public record Break<R extends Record>(R record, String... notifications) {

    public Break(R record, String... notifications) {
        System.out.println("record: " + record + " and notifications: " + Arrays.toString(notifications));
        this.record = record;
        this.notifications = notifications;
    }

    // compile error: non canonical record constructor must delegate to another costructor
    public Break(R record) { 
        System.out.println("record: " + record);
        this.record = record;
    }

    public Break() {
        this(null); // this works 
        // actually intelliJ suggests it uses the constructor that is not …
Run Code Online (Sandbox Code Playgroud)

java arity java-record java-16

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

为什么 v != null ?v++ : 1 与 (v != null ? v : 0) + 1 在 HashMap.compute 上递增键不同?

我想应用一个计算方法,如果键存在则增加值,否则放 1。

Map<Integer, Integer> map = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

我不明白为什么

for (int i = 0; i < 10; i++) map.compute(1, (k, v) -> v != null ? v++ : 1);
Run Code Online (Sandbox Code Playgroud)

结果{1=1}

for (int i = 0; i < 10; i++) map.compute(1, (k, v) -> (v != null ? v : 0) + 1);
Run Code Online (Sandbox Code Playgroud)

结果{1=10}?

我对第一种情况的理解是:

  • 如果有一个带有 key 的值k,则从中获取结果v++并将其放回原处,否则用 1 填充

而秒的情况是:

  • 如果有一个带有 k ksave的值v+1,否则 save 0+1,它也是 1

为什么在这种情况下运行v++ …

java hashmap java-8

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