次のような一般的なDaoインターフェイスで@Autowiredアノテーションを使用しようとしています。
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 />
Springは私のDaoContainerの1つのインスタンス、つまり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を使用することさえ可能であるかについて少し混乱しています。どんな助けでも大歓迎です:)