Compiler Environment:
graalvm-community-openjdk-17.0.9+9.1
springboot3.2.2
win11
Problem Description:
Use @Configuration to create a TestDao bean, but the @Autowired attribute in the created TestDao bean is not loaded. I must add @Repository or @Component to the TestDaoImpl class and shield @Configuration to load the @Autowired attribute correctly.
TestConfig
package com.efuture.business.config;
import com.efuture.business.dao.Impl.TestDaoImpl;
import com.efuture.business.dao.TestDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
@Bean
public TestDao testDao(){
return new TestDaoImpl();
}
}
TestDao
package com.efuture.business.config;
import com.efuture.business.dao.Impl.TestDaoImpl;
import com.efuture.business.dao.TestDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
@Bean
public TestDao testDao(){
return new TestDaoImpl();
}
}
TestDaoImpl
package com.efuture.business.dao.Impl;
import com.efuture.business.bean.TestDto;
import com.efuture.business.dao.TestDao;
import com.efuture.business.javaPos.global.GlobalInfo;
import com.efuture.business.mapper.base.TestMapper;
import com.efuture.business.service.impl.MpBaseServiceImpl;
import com.efuture.business.util.db.DbTools;
import com.efuture.business.util.db.MultipleDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Slf4j
public class TestDaoImpl implements TestDao {
@Autowired
private DbTools dbTools;
@Autowired
private TestMapper testMapper;
String tableName ="testtable";
public TestDaoImpl() {
}
// public TestDaoImpl(){
// super.setTableName(tableName);
// }
@Override
public List<Map> getName2(String mkt) {
// DbTools dbTools = new DbTools();
String a = dbTools.FilterDatesrc("getname");
log.info("dbTools=====>"+a);
if ("Y".equals(GlobalInfo.centrally)) {
MultipleDataSource.setDataSourceKey(dbTools.FilterDatesrc("getname", mkt));
} else {
MultipleDataSource.setDataSourceKey(dbTools.FilterDatesrc("getname"));
}
List<Map> list = testMapper.getName2();
return list;
}
@Override
public List<TestDto> getName(String mkt) {
return null;
}
}
graalvm native build
[1/8] Initializing... (13.0s @ 0.26GB)
Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
Graal compiler: optimization level: 2, target machine: x86-64-v3
C compiler: cl.exe (microsoft, x64, 19.38.33134)
Garbage collector: Serial GC (max heap size: 80% of RAM)
6 user-specific feature(s)
- com.efuture.business.config.ReflectFeature
- com.oracle.svm.thirdparty.gson.GsonFeature
- oracle.nativeimage.CharacterSetFeature
- oracle.nativeimage.NativeImageFeature
- org.springframework.aot.nativex.feature.PreComputeFieldFeature
- org.sqlite.nativeimage.SqliteJdbcFeature
The bundle named: oracle.net.mesg.Message, has not been found. If the bundle is part of a module, verify the bundle name is a fully qualified class name. Otherwise verify the bundle path is accessible in the classpath.
11111111
clazzname:com.efuture.business.service.impl.HelloHessianServiceImpl
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
Warning: RecomputeFieldValue.ArrayBaseOffset automatic substitution failed. The automatic substitution registration was attempted because a call to jdk.internal.misc.Unsafe.arrayBaseOffset(Class) was detected in the static initializer of com.alibaba.fastjson2.util.JDKUtils. Detailed failure reason(s): Could not determine the field where the value produced by the call to jdk.internal.misc.Unsafe.arrayBaseOffset(Class) for the array base offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store.
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of com.alibaba.fastjson2.util.JDKUtils. Detailed failure reason(s): Could not determine the field where the value produced by the call to sun.misc.Unsafe.objectFieldOffset(Field) for the field offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store.
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of com.alibaba.fastjson2.util.JDKUtils. Detailed failure reason(s): Could not determine the field where the value produced by the call to sun.misc.Unsafe.objectFieldOffset(Field) for the field offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store.
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of com.alibaba.fastjson2.util.JDKUtils. Detailed failure reason(s): Could not determine the field where the value produced by the call to sun.misc.Unsafe.objectFieldOffset(Field) for the field offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store.
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of com.alibaba.fastjson2.util.JDKUtils. Detailed failure reason(s): The argument of sun.misc.Unsafe.objectFieldOffset(java.lang.reflect.Field) is not a constant value or a field load that can be constant-folded., Could not determine the field where the value produced by the call to sun.misc.Unsafe.objectFieldOffset(Field) for the field offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store.
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of com.alibaba.fastjson2.util.JDKUtils. Detailed failure reason(s): Could not determine the field where the value produced by the call to sun.misc.Unsafe.objectFieldOffset(Field) for the field offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store.
# Printing 5 class initialization trace(s) of class(es) traced by TraceClassInitialization to: F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\reports\traced_class_initialization_20240808_181323.txt
[2/8] Performing analysis... [*******] (80.5s @ 2.64GB)
28,667 (91.86%) of 31,207 types reachable
50,458 (65.23%) of 77,357 fields reachable
142,117 (62.49%) of 227,426 methods reachable
8,066 types, 1,643 fields, and 14,892 methods registered for reflection
103 types, 109 fields, and 88 methods registered for JNI access
5 native libraries: crypt32, ncrypt, psapi, version, winhttp
[3/8] Building universe... (10.4s @ 3.20GB)
[4/8] Parsing methods... [***] (8.3s @ 1.68GB)
[5/8] Inlining methods... [***] (3.7s @ 2.31GB)
[6/8] Compiling methods... [*******] (54.3s @ 3.09GB)
[7/8] Layouting methods... [***] (11.0s @ 2.63GB)
[8/8] Creating image... [***] (10.2s @ 2.98GB)
76.41MB (53.07%) for code area: 98,581 compilation units
66.82MB (46.41%) for image heap: 613,901 objects and 225 resources
768.20kB ( 0.52%) for other data
143.98MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area: Top 10 object types in image heap:
15.06MB java.base 16.40MB byte[] for code metadata
8.00MB ojdbc11-23.2.0.0.jar 7.84MB java.lang.Class
4.60MB tomcat-embed-core-10.1.18.jar 6.09MB byte[] for java.lang.String
4.26MB fastjson2-2.0.52.jar 6.03MB java.lang.String
4.22MB java.xml 5.28MB byte[] for embedded resources
3.33MB jdk.proxy4 4.80MB byte[] for general heap data
3.26MB mysql-connector-j-8.3.0.jar 4.43MB char[]
2.15MB spring-data-redis-3.2.2.jar 2.41MB com.oracle.svm.core.hub.DynamicHubCompanion
2.06MB jackson-databind-2.15.3.jar 2.11MB byte[] for reflection metadata
2.04MB svm.jar (Native Image) 1.26MB java.lang.String[]
26.72MB for 128 more packages 9.65MB for 6009 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
AWT: Use the tracing agent to collect metadata for AWT.
HEAP: Set max heap for improved and more predictable memory usage.
CPU: Enable more CPU features with '-march=native' for improved performance.
------------------------------------------------------------------------------------------------------------------------
22.9s (11.8% of total time) in 174 GCs | Peak RSS: 5.35GB | CPU load: 7.23
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\awt.dll (jdk_library)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\boot-test.exe (executable)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\jaas.dll (jdk_library)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\java.dll (jdk_library_shim)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\javaaccessbridge.dll (jdk_library)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\jawt.dll (jdk_library)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\jsound.dll (jdk_library)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\jvm.dll (jdk_library_shim)
F:\ideaworksapcesbygit\springboot3.2.2-graalvm\springboot3.2.2-graalvm-learn-0.0.1\target\w2k_lsa_auth.dll (jdk_library)
========================================================================================================================
Finished generating 'boot-test' in 3m 13s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:21 min
[INFO] Finished at: 2024-08-08T18:15:03+08:00
[INFO] ------------------------------------------------------------------------
graalvm runing error is dbTools is null
java.lang.ClassNotFoundException: org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer
at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:123)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:87)
at java.base@17.0.9/java.lang.Class.forName(DynamicHub.java:1322)
at java.base@17.0.9/java.lang.Class.forName(DynamicHub.java:1311)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:304)
at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:224)
at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206)
at org.springframework.boot.diagnostics.FailureAnalyzers.loadFailureAnalyzers(FailureAnalyzers.java:69)
at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:64)
at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:58)
at java.base@17.0.9/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base@17.0.9/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.instantiate(SpringFactoriesLoader.java:382)
at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:228)
at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206)
at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:483)
at org.springframework.boot.SpringApplication.getExceptionReporters(SpringApplication.java:831)
at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:814)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:344)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
at com.efuture.business.SpringbootApplication.main(SpringbootApplication.java:52)
2024-08-08T10:15:16.877Z {main} [TRACE] DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
2024-08-08T10:15:16.877Z {main} [TRACE] SpringFactoriesLoader - Loaded [org.springframework.boot.diagnostics.FailureAnalysisReporter] names: [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter]
2024-08-08T10:15:16.877Z {main} [ERROR] SpringApplication - Application run failed
java.lang.NullPointerException: null
at com.efuture.business.dao.Impl.TestDaoImpl.getName2(TestDaoImpl.java:41)
at com.efuture.business.test.Sqlitetest.getTestName(Sqlitetest.java:25)
at com.efuture.business.SpringbootApplication.run(SpringbootApplication.java:43)
at org.springframework.boot.SpringApplication.lambda$callRunner$5(SpringApplication.java:790)
at org.springframework.util.function.ThrowingConsumer$1.acceptWithException(ThrowingConsumer.java:83)
at org.springframework.util.function.ThrowingConsumer.accept(ThrowingConsumer.java:60)
at org.springframework.util.function.ThrowingConsumer$1.accept(ThrowingConsumer.java:88)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:789)
at org.springframework.boot.SpringApplication.lambda$callRunners$3(SpringApplication.java:774)
at java.base@17.0.9/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base@17.0.9/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357)
at java.base@17.0.9/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510)
at java.base@17.0.9/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base@17.0.9/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base@17.0.9/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base@17.0.9/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base@17.0.9/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:341)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
at com.efuture.business.SpringbootApplication.main(SpringbootApplication.java:52)
2024-08-08T10:15:16.877Z {main} [TRACE] DefaultListableBeanFactory - Returning cached instance of singleton bean 'applicationAvailability'
2024-08-08T10:15:16.877Z {main} [DEBUG] ApplicationAvailabilityBean - Application availability state ReadinessState changed to REFUSING_TRAFFIC
2024-08-08T10:15:16.877Z {main} [DEBUG] ServletWebServerApplicationContext - Closing org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext@56b18d34, started on Thu Aug 08 18:15:14 CST 2024
Comment From: snicoll
@liufangzhou53 please take the time to format the report correctly. Right now it's a dump of various elements that are very hard to read. You can edit your original description. If you need help with formatting, there is https://docs.github.com/github/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax.
Comment From: 1haann
Hello. First of all, I am not good at English, so please understand. I saw the code and error message of your writing.
@slf4j
public class TestDaoImpl implements TestDao {
@Autowired
private DbTools dbTools;
@Autowired
private TestMapper testMapper;
String tableName ="testtable";
public TestDaoImpl() {
}
// public TestDaoImpl(){
// super.setTableName(tableName);
// }
@Override
public List<Map> getName2(String mkt) {
// DbTools dbTools = new DbTools();
String a = dbTools.FilterDatesrc("getname");
log.info("dbTools=====>"+a);
if ("Y".equals(GlobalInfo.centrally)) {
MultipleDataSource.setDataSourceKey(dbTools.FilterDatesrc("getname", mkt));
} else {
MultipleDataSource.setDataSourceKey(dbTools.FilterDatesrc("getname"));
}
List list = testMapper.getName2();
return list;
}
@Override
public List<TestDto> getName(String mkt) {
return null;
}
}
"java.lang.NullPointerException: null at com.efuture.business.dao.Impl.TestDaoImpl.getName2(TestDaoImpl.java:41)"
This error message seems to be a problem caused by the testDaoImpl class not being registered as bean. TestDaoImpl class is not registered as bean DI doesn't seem to work successfully for dbTools and testMapper. I think you should register as Bean through components like @Component, @Service in TestDaoImpl class. This is my first comment on someone else's code and I hope the issue is fixed.
Comment From: snicoll
@1haann much better, thanks.
@Configuration
public class TestConfig {
@Bean
public TestDao testDao(){
return new TestDaoImpl();
}
}
From the original description, I've extracted this. Please review the best practices. Native processing takes decision at build-time. You are exposing a bean of type TestDao so no handling of autowired fields for TestDaoImpl will happen as the type is not exposed. Try rewriting it as follows:
@Configuration
public class TestConfig {
@Bean
public TestDaoImpl testDao(){
return new TestDaoImpl();
}
}
For further support, please ask on StackOverflow as this issue tracker is meant for bug reports and enhancements only.
Comment From: liufangzhou53
thanks