mardi 31 mars 2015

Spring fails to autowire its beans - on my machine only. "Repository interface must not be null on initialization!"

I have a problem getting a Spring Data based application to run in my environment. I am running Debian, but my co-workers are either using Mac or Ubuntu. I have nothing special set up in my environment variables, and am using the exact same version of Java as others.


The application will not start with this error:



Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contentItemRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Repository interface must not be null on initialization!
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:127)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1517)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)


Has anyone see this exception at this location before and has some idea what could be causing it? It looks to me like the 'contentItemRepository' could not be created. Its annoying because Spring provides no clue as to why it could not be created, only that is was null when it tries to inject it into something else. IMO error reporting in Spring is really bad, its errors rarely describe the root of the problem; in this case its the consequence of an error that is reported, how do I find out why my bean could not be instantiated in the first place?


Why would this happen on my environment and no-one elses?


============================


The ContentItemRepository signature is:



@Repository
@Transactional
public interface ContentItemRepository extends JpaRepository<ContentItem, String>, JpaSpecificationExecutor<ContentItem> {


============================


This used to work for me, and I was able to identify the commit that broke the build, by iterating through all commits, doing a mvn clean install, and trying to start the server, until I found the delta that broke it.


The 'contentItemRepository' that cannot be null is this one:



@Component
+public class UrlAliasRequestConverter implements Mapper<UrlAliasRequest, UrlAlias> {
+
+ /**
+ * The content item contentItemType repository.
+ */
+ @Autowired
+ private ContentItemRepository contentItemRepository;


============================


Is it possible that Spring autowiring is confused somehow, and tries to do things in the wrong order, so that the repository is null when it tries to set it?


If the repository could not be created, would Spring report that as an earlier error?


I have seen this in the logs, suggesting that it is a circular reference problem that is leading to the instantiation failure:



nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'flyway.CONFIGURATION_PROPERTIES':
Initialization of bean failed;
...
nested exception is
org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'flyway': Requested bean is currently in
creation: Is there an unresolvable circular reference?


So the problem appears to be that flyway needs some dependencies and they need flyway.


The question is, why does this only happen on my environment not anyone elses? Even on the tests using H2 in memory, I see the problem, so its not my database that is at fault.


Does Spring have a badly implemented topological sort for ordering dependencies? Why would it misbehave on my environment? Could class ordering on the classpath influence its behaviour?


Aucun commentaire:

Enregistrer un commentaire