Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring 3 DI using generic DAO interface
    primarykey
    data
    text
    <p>I'm trying to use @Autowired annotation with my generic Dao interface like this:</p> <pre><code>public interface DaoContainer&lt;E extends DomainObject&gt; { public int numberOfItems(); // Other methods omitted for brevity } </code></pre> <p>I use this interface in my Controller in following fashion:</p> <pre><code>@Configurable public class HelloWorld { @Autowired private DaoContainer&lt;Notification&gt; notificationContainer; @Autowired private DaoContainer&lt;User&gt; userContainer; // Implementation omitted for brevity } </code></pre> <p>I've configured my application context with following configuration</p> <pre><code>&lt;context:spring-configured /&gt; &lt;context:component-scan base-package="com.organization.sample"&gt; &lt;context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /&gt; &lt;/context:component-scan&gt; &lt;tx:annotation-driven /&gt; </code></pre> <p>This works only partially, since Spring creates and injects only one instance of my DaoContainer, namely DaoContainer. In other words, if I ask userContainer.numberOfItems(); I get the number of notificationContainer.numberOfItems()</p> <p>I've tried to use strongly typed interfaces to mark the correct implementation like this:</p> <pre><code>public interface NotificationContainer extends DaoContainer&lt;Notification&gt; { } public interface UserContainer extends DaoContainer&lt;User&gt; { } </code></pre> <p>And then used these interfaces like this:</p> <pre><code>@Configurable public class HelloWorld { @Autowired private NotificationContainer notificationContainer; @Autowired private UserContainer userContainer; // Implementation omitted... } </code></pre> <p>Sadly this fails to BeanCreationException:</p> <pre><code>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)} </code></pre> <p>Now, I'm a little confused how should I proceed or is using multiple Dao's even possible. Any help would be greatly appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload