我正在使用新的 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)
如何为记录添加默认构造函数或有其他方法可以解决此问题?
我的多 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)
我怎样才能摆脱这个警告?
想知道 completablefuture 是否可以在创建它的线程中运行。您可能会问为什么我需要这样做,因为 completablefuture 是用于异步编程的。原因是我有一些异步任务和一些我想在生成线程中运行的任务,以便我可以使用 allOf 等并保持代码的一致性
我正在尝试在 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) 当使用下面的匿名类时,我们调用的变量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表达式只是实现匿名类的缩写。
我有一个代码场景,我将每个转换为流,但我不确定这是否是我遵循的正确方法
这是每个循环:
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) 此代码将给出 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 15,现在出现以下错误:
both interface org.jooq.Record in org.jooq and class java.lang.Record in java.lang match
Run Code Online (Sandbox Code Playgroud)
有没有人有解决这个问题的经验?
尝试使用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) 我想应用一个计算方法,如果键存在则增加值,否则放 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}?
我对第一种情况的理解是:
k,则从中获取结果v++并将其放回原处,否则用 1 填充而秒的情况是:
ksave的值v+1,否则 save 0+1,它也是 1为什么在这种情况下运行v++ …
java ×10
java-8 ×3
java-record ×3
java-11 ×2
java-stream ×2
arity ×1
hashmap ×1
java-14 ×1
java-15 ×1
java-16 ×1
jooq ×1
lambda ×1
log4j2 ×1
maven ×1
spring-boot ×1