spring

how to deal with erroneous (@Autowired+@Bean) method annotation in 3rd-party library for spring >=6.2.0


I have a 3rd party-library over which I do not have control and which has some java code around the lines:

@Configuration
public class DemoConfig {
    @Autowired
    @Bean
    public DemoService demoService() {
        return new DemoService(demoService2());
    }
    @Bean
    public DemoService2 demoService2() {
        return new DemoService2();
    }
}

The above code was working happily until Spring 6.2.0 at which point Spring started to reject the @Autowired as invalid (fair enough) and crashes the application (please refer BeanMethod class for detail).

My main question is: what are my otions here ?

  1. I would prefer to avoid a manual hacking of the library byte-code (with a bytecode editor like recaf) as this 3rd-party library is still evolving and I don't want to have to manually re-hack every single new version when it becomes available.
  2. I can't afford to wait until the library maintainers finally fix the problem by themselves because it could be a long while before that happens.
  3. I tried to write a java-agent to fix the problem on-the-fly (by modifying the bytecode in memory with javassist) but I discovered that Spring's BeanMethod class relies on asm to validate the bean metadata/annocations which is to say, it will read the annotation metadata directly from the underlying class file (even though my class is already loaded in memory because of the agent). Hence Spring crashes before even trying to use the bytecode I engineered, stripped from the extra Autowired annotation.

Is my best option to programatically modify that 3rd-party library bytecode as part of my build-tool (gradle in this case) ?

Thanks a lot for your expertise and your time.


Solution

  • In the end, I programmatically edited the faulty jar-dependency bytecode (and stripped the impacted methods/classes from the superfluous annotation) as part of my gradle build, through a gradle "TransformAction" class (and a bytecode editor library; javassist in my case).