Repository navigation
Description
Chris Beams opened SPR-3668 and commented
I have put together and attached a simplified version of this problem with a test case that demonstrates the NullPointerException.
In the example, you'll see a generic 'Repository' interface, which extends Set<T> (and thus, "is a collection", which is apparently important in relationship to how spring wires things up in the application context)
Repository has a non-generic subinterface 'EntityRepository', declared as:
public interface EntityRepository extends Repository<Entity> { ... }
EntityRepository has two concrete implementations: JdoEntityRepository and InMemoryEntityRepository. Both extend and abstract Repository implementation (AbstractRepository<T>) as follows:
final class JdoEntityRepository extends AbstractRepository<Entity> { ... }
final class InMemoryEntityRepository extends AbstractRepository<Entity> { ... }
I have a simple beans.xml that wires up an EntityRepository, injecting it into an 'EntityUtility' bean.
When this injection is performed, I get the following stack trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityUtility' defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is java.lang.NullPointerExceptionCaused by: java.lang.NullPointerException at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:395) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:204) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:356) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:438) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:153) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:799) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:717) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:384) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65) at example.EntityRepositoryTest.demonstrateNullPointerException(EntityRepositoryTest.java:22)
IMPORTANT: If I cease to have Repository<T> extend Set<T> (and for example, have it extend Iterable<T>), the entire problem goes away. This clearly has something to do with Repository being a Collection (evidenced by the last call in the stack trace 'convertToTypedCollection()'.
Steps to reproduce the failure:
- Download the attached example.zip file and unpack
- cd spring-collections-example
- Using Java 5.0 and Maven 2.0.5 or better, run
mvn test - You'll see the 'EntityRepositoryTest.demonstrateNullPointerException()' unit test fail with the stack trace above.
Once you've seen the failure, you can make this test succeed by editing Repository.java and removing the 'extends Set<T>' from the class declaration.
Affects: 2.0.6
Attachments:
- example.zip (8.58 kB)
Activity
spring-projects-issues commentedon Jul 8, 2007
Chris Beams commented
uploading zip containing test case mentioned in the description
spring-projects-issues commentedon Jul 9, 2007
Juergen Hoeller commented
Thanks for the report! Spring's collection conversion algorithm is indeed a bit too eager there. I've generally refined this for Spring 2.1, only trying to convert well-known collection types in the first place (skipping custom extended collection types right away).
That said, the actual problem here is that your Set implementations returns null from its "iterator()" method - which is unexpected (invalid according to the Set contract) and makes TypeConverterDelegate choke. The value wouldn't have been replaced anyway, of course, since no actual conversion is necessary... but we don't even get to that point due to the Iterator unexpectedly being null.
In any case, TypeConverterDelegate protects its collection conversion process against such misbehaving Collections with a null Iterator now - in 2.1 M3 as well as in 2.0.7. In addition, Spring 2.1 doesn't even try to convert unknown collections now, as indicated above.
Juergen
spring-projects-issues commentedon Jul 9, 2007
Chris Beams commented
Juergen,
Thanks for the prompt response!
I'm glad to report that fixing the null-returning iterator does eliminate the NPE, both in my example code as well as in my application.
I'm especially glad to hear that custom collection types will not be specially processed going forward. I've run into other issues with this in the past and I think it's a great decision to let them be treated as 'just another bean'.
Chris Beams