Announcement Announcement Module
Collapse
No announcement yet.
@AutoWired by type and by name if necessary Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @AutoWired by type and by name if necessary

    Hi,

    I have a question.

    I have a generic DAO template class and I use it for all my tables in my database.

    The declaration is like that.

    @Repository
    public final class GenericHibernateDao<T, ID extends Serializable>
    implements GenericDao<T, ID>

    I have something like that in applicationContext.xml

    <!-- Generic Dao -->
    <bean id="counterpartSampleDao"
    class="sgcib.ged.testxfire.core.persistence.hibern ate.dao.GenericHibernateDao">
    <property name="persistentClass">
    <bean id="counterpartSampleClass" class="java.lang.Class"
    factory-method="forName">
    <constructor-arg>
    <value>sgcib.ged.testxfire.core.persistence.hibern ate.domain.CounterpartSample</value>
    </constructor-arg>
    </bean>
    </property>
    </bean>


    <bean id="dealSampleDao"
    class="sgcib.ged.testxfire.core.persistence.hibern ate.dao.GenericHibernateDao">
    <property name="persistentClass">
    <bean id="dealSampleClass" class="java.lang.Class"
    factory-method="forName">
    <constructor-arg>
    <value>sgcib.ged.testxfire.core.persistence.hibern ate.domain.DealSample</value>
    </constructor-arg>
    </bean>
    </property>
    </bean>

    <bean id="productSampleDao"
    class="sgcib.ged.testxfire.core.persistence.hibern ate.dao.GenericHibernateDao">
    <property name="persistentClass">
    <bean id="productSampleClass" class="java.lang.Class"
    factory-method="forName">
    <constructor-arg>
    <value>sgcib.ged.testxfire.core.persistence.hibern ate.domain.ProductSample</value>
    </constructor-arg>
    </bean>
    </property>
    </bean-->



    I want to wire them with @Autowire in my Business Components but it not possible because I have more than one bean for this generic Dao class (it is normal because @Autowired wire by type).

    I want to know is it possible to wire by type and if there is more than one bean, to wire by name. I think it coud be a very cool feature.

    It is like @Configurable annotation
    (autowire=Autowire.BY_NAME)
    (autowire=Autowire.BY_TYPE)
    and add something like
    (autowire=Autowire.BY_TYPE_AND_BY_NAME_IF_NECESSAR Y)
    (the last constant is ugly, but I'm very bad to find good short name)

    The last feature can also be add fto the @Configurable annotation.

  • #2
    Isn't that what @Resource(name="myBeanName") is supposed to do?

    I'm still trying to get my appContext file to work right, so I can't be sure, but that was the way I interpreted the reference manual (p.86 - Section 3.10.2)

    Comment


    • #3
      I think you are right but I can find the annotation.

      What is the package?

      Comment


      • #4
        I just found it myself. It's not a spring annotation, but a java 5 annotation. You need to add the Java 5 EE libraries to your build path.

        I still don't have it working yet, but at lease @Resource isn't causing a compile error for me anymore.

        Comment


        • #5
          @Resource is actually bundled with Java 6 SE. Otherwise you need to add the common-annotations.jar to your classpath (it can be found in 'lib/j2ee' of the 2.1 distribution).

          Comment

          Working...
          X