mercredi 9 mai 2018

Changing a static field using reflection inside ear

One EAR contains 2 WARs (WAR1, WAR2) Both these WARS have a dependency jar (JAR1).

JAR1 has a class (CLASS1) and in it, is a static field private String STATIC1 = "DEFAULT_VAL"; this works for WAR2 but not for WAR1. So I've added a service in WAR1 to modify it :

@Service
public class ModService {
    @PostConstruct
    public void modMyVal() {
        Field declaredField = CLASS1.class.getDeclaredField("STATIC1");
        declaredField.setAccessible(true);
        declaredField.set(this, "NEW_VAL_FOR_WAR1");
    }
}

To my surprise, everything is working fine in WAR2. I was expecting WAR2 to have NEW_VAL_FOR_WAR1. Both the WARs are loaded inside the same ear,jvm,server - they both depend on the same jar. How is that static field not changed for WAR2 ?

Few other pointers:

  • Jar1 is provided by 3rd party, so don't have control over the source.
  • Jar1 is residing inside WAR1/WEB-INF/lib & WAR2/WEB-INF/lib
  • Server is weblogic.
  • I'm happy that it worked :). But curious how is it working internally, and what's saving my day.




Aucun commentaire:

Enregistrer un commentaire