mercredi 15 août 2018

IllegalAccessException in Spring with reflection and a WS Proxy Bean

After moving an application from Java8 to Java 9, I'm left with the following problem during the shutdown phase.

The application communicates to a number of Web Services, so during instantiation it creates com.sun.proxy versions of the interface. No problem there.

But when shutting down the Spring Framework is trying to see if the close() method exists on the Beans using reflection, and creates the an Exception for each proxy bean.

java.lang.IllegalAccessException: class org.springframework.beans.factory.support.DisposableBeanAdapter cannot access class com.sun.proxy.jdk.proxy11.$Proxy181 (in module jdk.proxy11) because module jdk.proxy11 does not export com.sun.proxy.jdk.proxy11 to unnamed module @4ec18a96
java.lang.IllegalAccessException: class org.springframework.beans.factory.support.DisposableBeanAdapter cannot access class com.sun.proxy.jdk.proxy12.$Proxy182 (in module jdk.proxy12) because module jdk.proxy12 does not export com.sun.proxy.jdk.proxy12 to unnamed module @4ec18a96

java.lang.IllegalAccessException: class org.springframework.beans.factory.support.DisposableBeanAdapter cannot access class com.sun.proxy.jdk.proxy10.$Proxy180 (in module jdk.proxy10) because module jdk.proxy10 does not export com.sun.proxy.jdk.proxy10 to unnamed module @4ec18a96

I understand what this error means, but I'm not sure how to resolve it due to the auto-generated nature of the beans.

2 Solutions that I have considered are

  1. Wrap the proxies in a bean from my own packages, where I can control who can access it reflectively
  2. Use a cmd line option of the form java --add-opens ****/****=ALL-UNNAMED

Option 1 requires a lot of extra code, wrapping it on creation and unwrapping when it is needed, so I want to avoid that.

Option 2 would certainly be easiest, but what do I replace the ****/**** with?

Or is there an Option 3 where I can indicate to Java that these proxy objects should allow reflection from the org.springframework.beans.factory.support package?





Aucun commentaire:

Enregistrer un commentaire