0

My program works fine when running it from IntelliJ IDE, but when I'm using it from a docker file it gives this error:

 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
 2020-03-26 13:48:35.598 ERROR 19 --- [           main] o.s.boot.SpringApplication               : Application run failed

Which will consequently throw a nullPointerException and can't get the beans.

I have defined the following class

    @Component
    public class ApplicationContextUtils implements ApplicationContextAware {

        private static ApplicationContext ctx;

        @Override
        public void setApplicationContext(ApplicationContext appContext)throws BeansException {
            ctx = appContext;

        }

        public static ApplicationContext getApplicationContext() {
                 return ctx;
        }
    }

And on my RabbitMQ I do:

    ApplicationContext appCtx = ApplicationContextUtils.getApplicationContext();
    FirebaseMessage firebaseMessage =  appCtx.getBean(FirebaseMessage.class);
    RabbitMQRequester rabbitMQRequester = appCtx.getBean(RabbitMQRequester.class);

I don't get why it works on the IDE and not on the docker-compose with a docker file build

jpdoliveira
  • 15
  • 10
  • You shouldn't use that util class. You should use dependency injection. – M. Deinum Mar 26 '20 at 14:16
  • @M.Deinum I had to use that because I ran into this problem https://stackoverflow.com/questions/60550899/nullpointerexception-autowired-mongodb-collection which I resolved with https://stackoverflow.com/questions/18347518/spring-autowiring-not-working-from-a-non-spring-managed-class/18486178#18486178 – jpdoliveira Mar 26 '20 at 14:40
  • The problem is you are trying to inject things into non-managed instances. And as there is no guarantee on the order that can be problematic. Just do proper injection and you don't need this ugly hack and things will work. – M. Deinum Mar 26 '20 at 15:17
  • How do you suggest that I do that injection? – jpdoliveira Mar 26 '20 at 15:52
  • By making that rabbitmq class a spring managed bean and not created outside the scope of Spring with `new`. More clarification was asked on your original question but instead of extending that question you lead to a path using this hack (which it really is). – M. Deinum Mar 26 '20 at 15:54
  • Well I'm not doing a new. I'm making it a @Component. Firebase and RabbitRequester are also a Component I did this because of this response: https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null#comment41516309_19896871 – jpdoliveira Mar 26 '20 at 15:56
  • 1
    The fact that it is a component doesn't mean you are nowhere not creating an instance. – M. Deinum Mar 26 '20 at 15:59
  • @M.Deinum Ok. This is going to sound stupid, but I used Autowired notation for that and it didn't work. But now I tried again and everything is fine. Thanks for your hints. – jpdoliveira Mar 26 '20 at 16:41

0 Answers0