3

Our application somehow gets into an unusable state very intermittently and in a hard to reproduce fashion. We found it follows a path that uses this code that we think is likely the cause of the issue but aren't sure why or how to fix it. We don't see this around specific if statements so it doesn't seem like it's going down a different path of code but rather more likely a timing issue.

`import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

((AnnotationConfigWebApplicationContext) applicationContext).refresh();`

It seems like the refresh triggers the following error so the next time we try and go through normal code paths spring is in a bad state.

`Unsatisfied dependency expressed through constructor parameter 0; nested exception is     org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'MyCodeClass' defined in URL [jar:file:/path/repo-version.jar!/com/mypathtoMyCodeClassImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available`

MyCodeClass semi looks like

`import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component("MyCodeClass")
public class MyCodeClassImpl implements MyCodeClass, AnotherInterface {

    @Autowired
    @Lazy
    private ApplicationContext applicationContext;

... Other code
}`
  • Why are you even refreshing the context, that is pretty dangerous actually... – M. Deinum Jan 06 '23 at 07:55
  • When our service starts up right after being installed we configure it at run time with things like database configuration. Our beans rely on that configuration and so we start up with dummy beans and swap them out once configuration input is added. – FireBreather Jan 10 '23 at 18:39
  • Which seems wrong as you now have doubled the amount of beans. Why isn't this part of the startup process or regular configuration phase. What is so special about it? – M. Deinum Jan 10 '23 at 18:45

1 Answers1

0

I am not sure about Injecting ApplicationContext in that way that you have done,i think it could be error prone; if you want to Inject ApplicationContaxt in your beans, its better to implements ApplicationContextAware with target bean and let to spring container do that.

sina zarei
  • 87
  • 1
  • 5