4

I have an ApplicationContextInitializer that initializes certain application properties. I am trying to autowire spring's ResourceLoader and a restclient into it, but couldn't. Is it possible to autowire inside an ApplicationContextInitializer implementation?

 @Component public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Autowired private ResourceLoader resourceLoader; @Autowired private MyRestClient restClient; // some init methods } 

My autowired.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-lazy-init="true"> <context:property-placeholder/> <context:annotation-config /> <context:component-scan base-package="com.package.containing.classes" /> </beans> 
1
  • 1
    please add more details, like what is the error you're getting for start and your context class Commented Feb 15, 2016 at 17:30

1 Answer 1

1

Recently I came across the same pitfall. I was trying to autowire a property inside a ApplicationContextInitializer that didn't work. The symptom was that my application at startup only printed the banner to the console and then silently quit. The reason was that a NullPointerException was raised inside the initialize method since my property was null. While the NPE was a code specific issue in my case it showed that the @Autowire annotation was not processed by Spring because if so Spring should have failed in the initialization due to a missing bean definition matching my property instead of injecting null.

In short: Spring does not perform autowiring to ApplicationContextInitializer.

Sign up to request clarification or add additional context in comments.

2 Comments

ApplicationContextInitializer is too early in the lifecycle, before autowire is processed. What did you end up doing?
@krishnaT Post a new question with a minimal, reproducible example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.