Results 1 to 5 of 5

Thread: @AutoWired by type and by name if necessary

  1. #1
    Join Date
    Sep 2005
    Posts
    15

    Default @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. #2
    Join Date
    Mar 2007
    Posts
    128

    Default

    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)

  3. #3
    Join Date
    Sep 2005
    Posts
    15

    Default

    I think you are right but I can find the annotation.

    What is the package?

  4. #4
    Join Date
    Mar 2007
    Posts
    128

    Default

    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.

  5. #5
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    @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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •