spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

spring-modulith-observability causes Kotlin bean properties to be initialized as null

Open itanxiao opened this issue 1 year ago • 11 comments
trafficstars

When enabling spring-modulith-observability, Kotlin bean properties are unexpectedly initialized as null. For example, after enabling this module, properties such as hisHttpClient and medicalInstitutionCode are null. Disabling the module resolves the problem.

Expected behavior:

  • Kotlin bean properties should be correctly initialized when observability is enabled.

Actual behavior:

  • When spring-modulith-observability is enabled, the properties in the Kotlin bean are null as seen in the following screenshots.

Screenshots:

  1. Bean initialization with null values: image

  2. CGLIB proxy details showing null for constructor-injected properties: image

itanxiao avatar Sep 05 '24 13:09 itanxiao

The version used is 1.2.3 image

itanxiao avatar Sep 05 '24 13:09 itanxiao

That's not unexpected regarding the class that you see being a proxy whose actual target will still have the proper dependencies wired. This should only be a problem if you try to access the fields directly. Method invocations, however, will pass the interceptor chain and ultimately trigger the method invocation on the target.

Can you clarify how a problem manifests from that, beyond you seeing the null fields in the debugger?

odrotbohm avatar Sep 12 '24 08:09 odrotbohm

Snipaste_2024-09-13_09-07-42

java.lang.NullPointerException: Parameter specified as non-null is null: method reqeust.MedicalInfoRequest.<init>, parameter medicalInstitutionCode

That's not unexpected regarding the class that you see being a proxy whose actual target will still have the proper dependencies wired. This should only be a problem if you try to access the fields directly. Method invocations, however, will pass the interceptor chain and ultimately trigger the method invocation on the target.

Can you clarify how a problem manifests from that, beyond you seeing the null fields in the debugger?

itanxiao avatar Sep 13 '24 01:09 itanxiao

Unfortunately few line screenshots are not that helpful in diagnosing the problem. Is there any chance you provide a minimal reproducer? Is anyone trying to access the field directly instead of calling a method passing the proxy?

odrotbohm avatar Sep 13 '24 07:09 odrotbohm

Unfortunately few line screenshots are not that helpful in diagnosing the problem. Is there any chance you provide a minimal reproducer? Is anyone trying to access the field directly instead of calling a method passing the proxy?

test project

spring-modulith-test-main.zip

itanxiao avatar Sep 16 '24 08:09 itanxiao

$ git clone https://github.com/chenzhenjia/spring-modulith-test
$ cd spring-modulith-test
$ chmod +x ./gradlew
$ ./gradlew
BUILD SUCCESSFUL in 1m 30s
1 actionable task: 1 executed

How do I reproduce the problem?

odrotbohm avatar Sep 16 '24 09:09 odrotbohm


$ git clone https://github.com/chenzhenjia/spring-modulith-test

$ cd spring-modulith-test

$ chmod +x ./gradlew

$ ./gradlew

BUILD SUCCESSFUL in 1m 30s

1 actionable task: 1 executed

How do I reproduce the problem?

Start the application and visit http://127.0.0.1:8080 to see the exception in the console.

itanxiao avatar Sep 16 '24 09:09 itanxiao

I am sorry, but I won't follow up unless you can properly prepare the project, including instructions what to do with the code to see the problem (what does "start the application" mean?). I ran the project (./gradlew). It leads to a successful build.

odrotbohm avatar Sep 16 '24 09:09 odrotbohm

I am sorry, but I won't follow up unless you can properly prepare the project, including instructions what to do with the code to see the problem (what does "start the application" mean?). I ran the project (./gradlew). It leads to a successful build.

Try ./gradlew bootRun or ./gradlew :app bootRun to start the spring boot application

itanxiao avatar Sep 16 '24 10:09 itanxiao

For the former command, I get:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':core:bootRun'.
> Failed to calculate the value of task ':core:bootRun' property 'mainClass'.
   > Main class name has not been configured and it could not be resolved from classpath /Users/odrotbohm/temp/spring-modulith-test/core/build/classes/kotlin/main

For the latter:

FAILURE: Build failed with an exception.

* What went wrong:
Cannot locate tasks that match ':app' as task 'app' not found in root project 'spring-modulith-test'.

odrotbohm avatar Dec 17 '24 07:12 odrotbohm

For the former command, I get:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':core:bootRun'.
> Failed to calculate the value of task ':core:bootRun' property 'mainClass'.
   > Main class name has not been configured and it could not be resolved from classpath /Users/odrotbohm/temp/spring-modulith-test/core/build/classes/kotlin/main

For the latter:

FAILURE: Build failed with an exception.

* What went wrong:
Cannot locate tasks that match ':app' as task 'app' not found in root project 'spring-modulith-test'.

My mistake, I should have used ./gradlew :app:bootRun

itanxiao avatar Dec 18 '24 06:12 itanxiao