title: Spring Bean is not autowired
body: <p>I have a problem with Spring bean <code>DefaultConfigurationService</code> initialization that is extended from <code>abstract</code> class. I am totally stuck.</p>
  
  <p>Class hiearchy is as follows:</p>
  
  <pre><code>public interface ConfigurationService {
      TaxPayer getTaxPayer();
  } 
  </code></pre>
  
  <p>This class is mentioned to be useful for services that need to be initialized:</p>
  
  <pre><code>public abstract class BaseInitializationAwareService {
  
      private boolean initialized = false;
  
      public abstract void initialize();
  
      protected void checkInitialization() {
          if (!initialized) {
              initialize();
          }
      }
  
      protected void setInitialized() {
          this.initialized = true;
      }
  }
  </code></pre>
  
  <p>This class acts as base class for Configuration service.</p>
  
  <pre><code>public abstract class BaseConfigurationService extends BaseInitializationAwareService implements ConfigurationService {
  
  
  }
  </code></pre>
  
  <p>And with this bean, that acts as a configuration service, is a problem:</p>
  
  <pre><code>    public class DefaultConfigurationService extends BaseConfigurationService {
  
          private TaxPayerService taxPayerService;
  
          @Autowired
          public void setTaxPayerService(TaxPayerService taxPayerService) {
              Assert.notNull(taxPayerService);
              this.taxPayerService = taxPayerService;
          }
  
  public void initialize() {
          Optional&lt;TaxPayer&gt; dbtaxPayer = taxPayerService.getActiveTaxPayer();
          if (!dbtaxPayer.isPresent()) {
              throw new IllegalStateException("Tax payer setting not found!");
          }
          this.taxPayer = dbtaxPayer.get();
          setInitialized();
      }
  
      // the rest omitted...
      }
  </code></pre>
  
  <p>when I'm creating <code>DefaultConfigurationService</code> bean:</p>
  
  <pre><code>@Bean
      public BaseConfigurationService configurationService() {
          DefaultConfigurationService configurationService = new DefaultConfigurationService();
          configurationService.initialize();
          return configurationService;
      }
  </code></pre>
  
  <p>then <code>taxPayerService</code> in <code>DefaultConfigurationService</code> is null - it seems that is not autowired.</p>
  
  <p><strong>Can it be connected to the fact that <code>DefaultConfigurationService</code> is extended from abstract class?</strong></p>
  
  <p>TaxPayer service bean:</p>
  
  <pre><code>@Bean
      public TaxPayerService taxPayerService() {
          DatabaseTaxPayerService taxPayer = new DatabaseTaxPayerService();
          return taxPayer;
      }
  </code></pre>
  
  <p>This bean is probably <strong>never initialized</strong>...</p>
  
  <p>Thats is a exception:</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'webSecurityConfig.ApiSecurity':
    Unsatisfied dependency expressed through method
    'setContentNegotationStrategy' parameter 0; nested exception is
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name
    'org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration':
    Unsatisfied dependency expressed through method 'setConfigurers'
    parameter 0; nested exception is
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'passwordRecoverController': Unsatisfied
    dependency expressed through method 'setUserService' parameter 0;
    nested exception is
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'defaultUserService': Unsatisfied
    dependency expressed through method 'setNotificationService' parameter
    0; nested exception is
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'notificationService': Unsatisfied
    dependency expressed through method 'setConfigurationService'
    parameter 0; nested exception is
    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'configurationService' defined in class path
    resource [com.example.config/AppConfig.class]: Bean instantiation via
    factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [com.example.services.BaseConfigurationService]: Factory
    method 'configurationService' threw exception; nested exception is
    java.lang.NullPointerException</p>
  </blockquote>
  
  <hr>
  
  <p>For example, bean that needs <code>BaseConfigurationService</code>:</p>
  
  <pre><code>    public class EmailNotificationService extends BaseService implements NotificationService {
  
          private BaseConfigurationService configurationService;
  
  @Autowired
      public void setConfigurationService(BaseConfigurationService configurationService) {
          Assert.notNull(configurationService);
          this.configurationService = configurationService;
      }
  
      // the rest omitted... 
      }
  </code></pre>
  
  <hr>
  
  <p><strong>#update 1</strong></p>
  
  <p>Bean initialization example with internal dependencies to another beans:</p>
  
  <pre><code>@Bean
      public TransactionDataService transactionDataService() {
          return new DefaultTransactionDataService();
      }
  </code></pre>
  
  <p>and <code>DefaultTransactionDataService</code>:</p>
  
  <pre><code>public class DefaultTransactionDataService implements TransactionDataService {
      private PrivateKeyService privateKeyService;
  
      @Autowired
      public void setPrivateKeyService(PrivateKeyService privateKeyService) {
          Assert.notNull(privateKeyService);
          this.privateKeyService = privateKeyService;
      }
  }
  </code></pre>
  
  <p>and bean dependency</p>
  
  <pre><code>@Bean
      public PrivateKeyService privateKeyService() {
          return new DefaultPrivateKeyAwareService();
      }
  </code></pre>
  
  <p>and it <strong>works</strong>.</p>