我正在尝试将 @Autowired 注释与我的通用 Dao 接口(interface)一起使用,如下所示:
public interface DaoContainer<E extends DomainObject> {
public int numberOfItems();
// Other methods omitted for brevity
}
我在我的 Controller 中以下列方式使用这个接口(interface):
@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 只创建和注入(inject)了我的 DaoContainer 的一个实例,即 DaoContainer。换句话说,如果我问 userContainer.numberOfItems();我得到了 notificationContainer.numberOfItems() 的数量
我尝试使用强类型接口(interface)来标记正确的实现,如下所示:
public interface NotificationContainer extends DaoContainer<Notification> { }
public interface UserContainer extends DaoContainer<User> { }
然后像这样使用这些接口(interface):
@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。任何帮助将不胜感激 :)
最佳答案
好的,我想我已经为这个难题找到了一个相当合理的解决方案。处理这个问题的一种方法是为我的域模型中的每个实体创建接口(interface)和实现(正如 Espen 在他之前的回答中指出的那样)。现在,考虑拥有数百个实体和数百个实现。那感觉不对,不是吗?
我已经放弃了强类型子接口(interface),而是使用通用接口(interface):
@Service // Using @Service annotation instead @Configurable as Espen pointed out
public class HelloWorld {
@Autowired
private DaoContainer<Notification> notificationContainer;
@Autowired
private DaoContainer<User> userContainer;
// Implementation omitted
}
我的 DaoContainer 接口(interface)的实现如下所示:
@Repository
public class DaoContainerImpl<E extends DomainObject> implements DaoContainer<E> {
// This is something I need in my application logic
protected Class<E> type;
public int getNumberOfItems() {
// implementation omitted
}
// getters and setters for fields omitted
}
最后是应用程序上下文:
<context:spring-configured />
<context:component-scan base-package="com.organization.sample">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<bean class="com.organization.sample.dao.DaoContainerImpl" id="userContainer">
<property name="type" value="com.organization.sample.domain.DiaryUser" />
</bean>
<bean class="com.organization.sample.dao.DaoContainerImpl" id="notificationContainer">
<property name="type" value="com.organization.sample.domain.DiaryNotification" />
</bean>
所以基本上我无法让纯通用 Autowiring 工作,但这个解决方案对我有用(至少现在):)
关于Spring 3 DI 使用通用 DAO 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2847133/