Announcement Announcement Module
Collapse
No announcement yet.
Not sure how to Autowire datasource for (JDBC) support classes? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not sure how to Autowire datasource for (JDBC) support classes?

    I'm migrating our DAOs from our own custom JDBC implentation to use some of the support classes instead, such as JdbcSimpleDaoSupport. In the process, we are trying to perform some Autowiring and component scanning...
    The problem is, with the JdbcSimpleDaoSupport class, the method setDataSource is set as final, so I cannot override it and apply the @Autowired annotation to it. Until I can figure this out, I can't annotate this DAO class as a component (@Component or @Repository) to have it auto-scanned.
    Am I approaching this wrong, or is there an easy workaround to this?

  • #2
    You can alway provide a different method name that is autowired and then just call super.setDataSource(...) in that method.

    Comment


    • #3
      Thanks chudak, that's a good suggestion. I was hoping for something a bit more elegant though, something as if they had thought of this beforehand. If I have to name my setter something different in this class, then I've lost a bit of consistency throughout other places where I might need/have access to the same setter.

      Comment


      • #4
        You can always use composition instead of inheritance. Have your dao's have a reference to the dao support class rather than extending it. Then set the datasource in your support class in your spring beans file and have the support class autowired into your dao.

        Comment


        • #5
          Originally posted by chudak View Post
          You can always use composition instead of inheritance. Have your dao's have a reference to the dao support class rather than extending it. Then set the datasource in your support class in your spring beans file and have the support class autowired into your dao.
          I think this idea might just work... I'll give it a shot when I can, but for now to conceptualize it, here's my bean config I'm envisioning for this:

          <bean id="dataSource" class="...BasicDataSource">
          ...omitted for brevity...
          </bean>

          <bean id="jdbcSimpleDaoSupport" class="...springframework...JdbcSimpleDaoSupport">
          <property name="dataSource" ref="dataSource" />
          </bean>

          <bean id="myDao" class="...MyDao">
          <property name="jdbcSimpleDaoSupport" ref="jbcSimpleDaoSupport"/>
          </bean>

          From here it's a little foggy without having access to try it; in my DAO class I need a jdbcSimpleTemplate to use in my methods. How do I go from injecting the actual support class as a reference to getting the jdbcSimpleTemplate from it?
          Once the details are ironed out, I can then Autowire the property in the DAO class, and component scan the DAO class itself using @Repository, then remove the myDao bean altogether. Sound about right?

          Comment


          • #6
            Originally posted by xaeryan View Post
            I think this idea might just work... I'll give it a shot when I can, but for now to conceptualize it, here's my bean config I'm envisioning for this:

            <bean id="dataSource" class="...BasicDataSource">
            ...omitted for brevity...
            </bean>

            <bean id="jdbcSimpleDaoSupport" class="...springframework...JdbcSimpleDaoSupport">
            <property name="dataSource" ref="dataSource" />
            </bean>

            <bean id="myDao" class="...MyDao">
            <property name="jdbcSimpleDaoSupport" ref="jbcSimpleDaoSupport"/>
            </bean>

            From here it's a little foggy without having access to try it; in my DAO class I need a jdbcSimpleTemplate to use in my methods. How do I go from injecting the actual support class as a reference to getting the jdbcSimpleTemplate from it?
            Once the details are ironed out, I can then Autowire the property in the DAO class, and component scan the DAO class itself using @Repository, then remove the myDao bean altogether. Sound about right?
            How are you getting the simple template now? You are probably calling:

            Code:
            getJdbcTemplate().doSomething();
            because you inherited that method from your parent class (JdbcDaoSupport).

            Now, you just call the same method on the collaborator:

            Code:
            public class MyDao
            {
                private JdbcDaoSupport support;
                public void setJdbcDaoSupport(JdbcDaoSupport support)
                {
                    this.support = support;
                }
            
                public void doSomething()
                {
                    support.getJdbcTemplate().doSomething();
                }
            }

            Comment


            • #7
              OK good, I didn't want to post it in case it was horribly in the wrong direction, but that's on the lines of what I was thinking. Thanks for your help!

              Comment


              • #8
                Worked like a charm, but led me to discover another issue regarding Controller annotations. Whole different problem altogether though: http://forum.springsource.org/showthread.php?p=273457
                Thanks again for your help!

                Comment

                Working...
                X