The Artima Developer Community
Sponsored Link

Java Buzz Forum
Autowiring in Spring

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
Autowiring in Spring Posted: May 7, 2004 9:32 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Chris Winters.
Original Post: Autowiring in Spring
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement
For some reason I never looked into Spring's autowiring capabilities. It may have been that I wasn't familiar enough with the framework to accept such magical doings on faith. But now I am more familiar with Spring and have a ton of respect for its design decisions and direction.

Then I read an email from James Cook on the springframework-user list where he showed his implementation of an autowire-by-interface. (I'd love to link directly to the message but the SF mailing list archiving blows.)

Basically he just created a bean that implements 'BeanPostProcessor'. Spring recognizes this at context initialization and slots it in the chain of "objects to call after I create a bean". When his bean is called during the process of creating a new bean he just checks to see if the new bean implements a particular interface and if so, sets a property in it. The whole thing is a dozen or two lines, with comment and logging.

Anyway, this got me to take a look at autowiring. At first I figured I'd try the 'byType' autowiring. This seems the most robust because you don't have to ensure the names in the bean declaration and the name of your setter method are exactly the same. But I ran into a problem relating to the transaction proxies discussed earlier.

To autowire by type you must eliminate ambiguity -- if you have two objects implementing 'CustomerDAO' then Spring won't know which one to set. But if you're using transactional proxies you have two beans implementing your DAO interface -- the actual bean and the dynamic proxy that takes care of all your transactional goop. Oops. Fortunately Spring is smart enough to recognize this at deployment time rather than bean instantation time and gives you excellent error messages to pin down the problem quickly.

So I went through and synced up all the setter names with the bean names -- they were mostly the same anyway, it was just a few places where the object name was log that I had to change it. So something like this:

<bean
    id="reportViewCtl"
    class="com.optiron.cd.web.ReportViewController">
  <property name="customerDao">
      <ref bean="customerDao" />
  </property>
  <property name="ledgerTransactionDao">
      <ref bean="ledgerTransactionDao" />
  </property>
</bean>

Becomes:

<bean
    id="reportViewCtl"
    class="com.optiron.cd.web.ReportViewController"
    autowire="byName" />

For now it just makes the XML file simpler. But for future development it means that all you have to do is declare a setter of a particular type and it will just be there. Excellent!

Another note: the Spring docs don't make clear that you can mix and match. Autowiring is not all or nothing -- you can still declare properties of a bean where you've got necessary ambiguity in the system and cannot change the property names, such as declaring a methodNameResolver for a MultiActionController. So it's fine to have something like:

<bean
    id="exportCtl"
    class="com.optiron.cd.web.ExportController"
    autowire="byName">
  <property name="methodNameResolver">
      <ref bean="actionResolverHomeDefault"/>
  </property>
</bean>

Read: Autowiring in Spring

Topic: [Apr 27, 2004 17:36 PDT] 12 Links Previous Topic   Next Topic Topic: On message

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use