开发者

Spring error when trying to manage several classes that share a common base class?

Im currently using Spring 3.0.x .. Im wondering what's wrong with these structures, where i would like to manage the subclasses but not the parent classes.

I have 2 child DAOs extending the BaseDAO :

public abstract class BaseDAO<K, E> {
....
}

@Repository
public class UserDAO extends BaseDAO<String, User> {
....
}

@Repository
public class ApprovalDAO extends BaseDAO<String, Approval> {
....
}

And i have the services like this, with hierarchies like this :

public abstract class BaseService<K, E extends BaseEntity, D extends BaseDAO<K, E>> {
    @Autowired
    protected D dao;
....
}

public abstract class BaseCommonService<K, E extends BaseCommonEntity, D extends BaseDAO<K, E>> extends BaseService<K,E,D> {
....
}

@Service
public class UserService extends BaseCommonService<String, User, UserDAO> {
....
}

When trying to inject the userservice object to my application, it throws error like this :

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testEntities': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.primetech.module.common.service.UserService com.primetech.module.purchase.app.TestEntities.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.primetech.core.parent.BaseDAO com.primetech.core.parent.BaseService.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.primetech.core.parent.BaseDAO] is defined: expected single matching bean but found 2: [userDAO, approvalDAO] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064) 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:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.Def开发者_StackOverflowaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) at com.primetech.module.purchase.app.TestEntities.main(TestEntities.java:81) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.primetech.module.common.service.UserService com.primetech.module.purchase.app.TestEntities.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.primetech.core.parent.BaseDAO com.primetech.core.parent.BaseService.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.primetech.core.parent.BaseDAO] is defined: expected single matching bean but found 2: [userDAO, approvalDAO] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283) ... 13 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.primetech.core.parent.BaseDAO com.primetech.core.parent.BaseService.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.primetech.core.parent.BaseDAO] is defined: expected single matching bean but found 2: [userDAO, approvalDAO] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064) 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:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:838) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:780) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) ... 15 more Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.primetech.core.parent.BaseDAO com.primetech.core.parent.BaseService.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.primetech.core.parent.BaseDAO] is defined: expected single matching bean but found 2: [userDAO, approvalDAO] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283) ... 26 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.primetech.core.parent.BaseDAO] is defined: expected single matching bean but found 2: [userDAO, approvalDAO] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) ... 28 more

I tried changing this section, removing the @Repository annotation :

@Repository
public class ApprovalDAO extends BaseDAO<String, Approval> {
....
}

into this :

public class ApprovalDAO extends BaseDAO<String, Approval> {
....
}

And things run without error, but then, the approvalDAO isnt managed by the spring anymore, and cannot be injected later by @Autowired

Any suggestions on how i can solve this problem ?


Autowiring works only if there is exactly one implementation bean of the specific type present in the Spring Context. I'd assume that using the generic D extends BaseDao leads to a situation where Spring is trying to autowire instances of BaseDao instead of UserDao and ApprovalDao. Because you both UserDao and ApprovalDao implement BaseDao, Spring context contains multiple implementations of BaseDao and cannot decide which one should be used.

Spring is trying to tell this to you in the stack trace

org.springframework.beans.factory.NoSuchBeanDefinitionException: 
__No unique bean of type__ [com.primetech.core.parent.BaseDAO] is defined:
expected single matching bean but found 2: [userDAO, approvalDAO]

You could try to test this by defining the dao in the concrete service using the actual dao type e.g.

public abstract class BaseService<K, E extends BaseEntity, D extends BaseDAO<K, E>> {
   private final D dao;
   protected BaseService(D dao) {
      this.dao = dao;
   } 
}

public class UserService extends BaseService<K, User, UserDao<K, User>> {
   @Autowired
   public UserService(UserDao dao) {
     super(dao);
   }
}

I'd continue by defining interfaces for UserDao and ApprovalDao so that dependencies are on interfaces and not implementations. The daos may still have a common super interface and their implementations may be based on the BaseDao to avoid unnecessary duplication.

In the example I've defined the injected Dao in the constructor because I assume that the same dao instance should be used throughout the lifetime of the service and the service cannot exist without the dao set. In my opinion, a constructor argument communicates this contract better. Furthermore it might make the class a bit more testable and maintainable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜