我试图使用@Autowired注解与我的通用DAO接口是这样的:春3 DI通用DAO接口
public interface DaoContainer<E extends DomainObject> {
public int numberOfItems();
// Other methods omitted for brevity
}
我在下列方式使用该接口在我的控制器:
@Configurable
public class HelloWorld {
@Autowired
private DaoContainer<Notification> notificationContainer;
@Autowired
private DaoContainer<User> userContainer;
// Implementation omitted for brevity
}
我已经配置了我的应用程序上下文中使用以下配置
<context:spring-configured />
<context:component-scan base-package="com.organization.sample">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<tx:annotation-driven />
这只有partiall y,因为Spring创建并注入了我的DaoContainer的一个实例,即DaoContainer。换句话说,如果我问了userContainer.numberOfItems();我得到
我试图使用强类型接口来标记的正确实施这样notificationContainer.numberOfItems()的数量:
public interface NotificationContainer extends DaoContainer<Notification> { }
public interface UserContainer extends DaoContainer<User> { }
然后使用这些接口是这样的:
@Configurable
public class HelloWorld {
@Autowired
private NotificationContainer notificationContainer;
@Autowired
private UserContainer userContainer;
// Implementation omitted...
}
可悲的是这未能BeanCreationException:
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.organization.sample.dao.NotificationContainer com.organization.sample.HelloWorld.notificationContainer; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.organization.sample.NotificationContainer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
现在,我有点困惑,我应该如何继续或甚至可能使用多个Dao。任何帮助将不胜感激:)
我没有看到您的接口的任何实现类。有多少人,他们看起来像什么? – skaffman 2010-05-17 07:38:01
我没有明确的接口实现,因为我希望我可以使用通用的dao类(即DaoContainer)。我可以创建显式的实现(正如Espen在他的回答中指出的那样)。这只是不合理,因为我试图尽可能多地利用Java泛型。但是,我确实有DaoContainerImpl 。 –
Peders
2010-05-19 05:30:17
也许http://stackoverflow.com/questions/502994/spring-ioc-and-generic-interface-type/511417#511417是一个解决方案 – 2010-08-18 09:38:45