晚安。我在对象的注入中有一个难以理解的错误,我不明白它是什么以及如何避免,互联网搜索没有返回任何结果,没有一个解决方案没有帮助。如果你能解释一下原因,我将不胜感激。
ReminderService bean
@Service("ReminderService")
public class ReminderService implements dbService<Reminder>
{
@Autowired
private JpaReminder jpaReminder;
@Transactional
public List<Reminder> getAll()
{
return jpaReminder.findAll();
}
@Transactional
public Reminder get(Object id)
{
return jpaReminder.find(id);
}
}
JpaReminder bean,
@Repository("JpaReminder")
public class JpaReminder extends jpaDao<Reminder>
{
private EntityManager em;
/**
* @return Возвращает entityManager
*/
@Override
protected EntityManager getEntityManager()
{
return em;
}
public EntityManager getEm()
{
return em;
}
@PersistenceContext(unitName = "TaskTracker")
public void setEm(EntityManager em)
{
this.em = em;
}
}
堆栈跟踪错误
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ReminderService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.work.Spring.Dao.JpaReminder com.work.Spring.mainClass.ReminderService.jpaReminder; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JpaReminder' defined in file [/home/qwerty/Eclipse/Spring/target/classes/com/work/Spring/Dao/JpaReminder.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.work.Spring.Dao.JpaReminder]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.work.Spring.App.main(App.java:22)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.work.Spring.Dao.JpaReminder com.work.Spring.mainClass.ReminderService.jpaReminder; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JpaReminder' defined in file [/home/qwerty/Eclipse/Spring/target/classes/com/work/Spring/Dao/JpaReminder.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.work.Spring.Dao.JpaReminder]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 13 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JpaReminder' defined in file [/home/qwerty/Eclipse/Spring/target/classes/com/work/Spring/Dao/JpaReminder.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.work.Spring.Dao.JpaReminder]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 15 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.work.Spring.Dao.JpaReminder]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 26 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at com.work.Interfaces.jpaDao.<init>(jpaDao.java:24)
at com.work.Spring.Dao.JpaReminder.<init>(JpaReminder.java:19)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 28 more
答案 0 :(得分:3)
从异常的堆栈跟踪中,很明显JpaReminder
的构造函数代码正在抛出java.lang.ArrayIndexOutOfBoundsException: 1.
由于JpaReminder
正在延伸jpaDao
而JpaReminder
没有自己的构造函数,因此jpaDao
(或其父类&#39} ; s)构造函数是罪魁祸首
您可以调试代码并了解更多
答案 1 :(得分:0)
当我使用Java 8且Spring版本低于4时,我的应用程序中出现了类似的异常。我不得不升级到Spring 4或更改lambdas,所以我做了后者。
这很难知道,因为错误太模糊了。与数组无关,真的。