0

I have this component that looks basically like this: -- Please note, I am trying to have a Spring boot application with 2 data sources. I do not think this is the problem here, but I thought I would mention it.

@Configuration
@Component
@ConfigurationProperties("connection.jpa")
@EnableTransactionManagement
public class HibernateConnectionsConfiguration {

@Bean(name = "jpaDataSource")
@Primary
public DataSource jpaDataSource() {
    return DataSourceBuilder.create().password(password).driverClassName(driverClassName).url(databaseUrl).username(username).build();
}

@Bean
public DaoFactory getDaoFactory() {
    EntityManagerFactory emf = entityManagerFactory();
    DaoFactory f = new DaoFactory(emf);
    System.out.println("DAO FACTORY IN Bean CONSRUCTOR:"+f);
    return f;
}

Now the logs clearly say that the DaoFactory is NOT null and seems to be instantiated correctly.

I have this other class where I try and autowire that bean. Please note that I am trying to import the DaoFactory bean and none of the DataSources. There is only one DaoFactory @Bean

public class ECServiceFactory implements IServiceFactory {

private Map<String,String> props = null;
private final User rootUser;
private final WorkflowExecutionManager workflowExecutionManager;
private ElasticSearchService elasticSearchService;
private final Class<? extends Exception> securityException;
private final Configuration cmsConfig;
private VersionedDocumentService documentService;

@Autowired
DaoFactory daoFactory;

public ECServiceFactory(Map<String,String> props,
        VersionedDocumentService documentServiceClass,
        User rootUser,
        Class<? extends Exception> securityException,
        Configuration configuration) {
    this.documentService = documentServiceClass;
    this.props = props;
    this.rootUser = rootUser;
    this.workflowExecutionManager  = new WorkflowExecutionManager(60, 100);
    System.out.println("DAO Factory from SERVICE Facory:"+daoFactory);
    this.elasticSearchService = new ElasticSearchService(daoFactory);
    this.securityException = securityException;
    this.cmsConfig = configuration;
}

Now the logs clearly state the the DAO Facotry from Service Factory: is NULL

Now the class that instantiates my ECServiceFactory is in my Application.java class, --- However, I have that class implement ApplicationListener and the method looks like this:

@SuppressWarnings("unchecked")
@Override
public void onApplicationEvent(ApplicationReadyEvent arg0) {
  ECServiceFactory svcFactory = new ECServiceFactory(application, documentService,new SuperUser(),AccessDeniedException.class,config);
 }

So it seems to me that all the beans have been loaded at this point and I should not have any problems with my Autowire.

What am I doing wrong?

mmaceachran
  • 3,178
  • 7
  • 53
  • 102
  • 2
    because your `ECServiceFactory` is not a spring managed bean. Marked this question as a duplicate as that is also answered in the other question. Next to that `@Autowired` is only available AFTER the object has been constructed not WHILE constructing the object. – M. Deinum Jan 30 '18 at 08:18
  • Someday, many years from now, I am going to remember the spring managed bean bit. This has got to be the 6th time this has gotten me. – mmaceachran Jan 30 '18 at 08:32

0 Answers0