Results 1 to 2 of 2

Thread: Totally confused - @Component vs @Configurable

  1. #1
    Join Date
    Jan 2011
    Posts
    18

    Question Totally confused - @Component vs @Configurable

    Hi,

    I've got a class that looks something like this:

    Code:
    @Configurable
    public class ChangeQuantityTransaction extends AbstractTransaction
    {
      @Override
      @Transactional
      public Object doTransaction(MapSqlParameterSource params) throws Exception
      {
         a few sql updates are performed in here
      }
    }
    AbstractTransaction class looks like this:

    Code:
    public abstract class AbstractTransaction
    {
      protected JdbcTemplate _jt;
    
      public Object doTransaction(MapSqlParameterSource params) throws Exception;
    
      @Autowired
      protected void setDataSource(DataSource dataSource)
      {
        _jt = new JdbcTemplate(dataSource);     
      }
    }
    Configuration (admin-servlet.xml):

    Code:
    <bean id="changeQuantityTransaction" class="mypackage.ChangeQuantityTransaction" />
    
    <bean class="org.springframework.web.context.support.ServletContextAttributeExporter"
      <property name="attributes">
        <map>
          <entry key="changeQuantityTrans">
            <ref bean="changeQuantityTrans" />
          </entry>
        </map>
      </property>
    </bean>
    I am working with a legacy servlet and get a changeQuantityTransaction in the servlet's init method:

    Code:
    private ChangeQuantityTransaction _trans;
    public void init(ServletConfig config)
    {
      Object obj = config.getServletContext().getAttribute("changeQuantityTrans");
      setTransaction((ChangeQuantityTransaction)obj);
    }
    
    private void setTransaction(ChangeQuantityTransaction trans)
    {
      _trans = trans;
    }

    Here is the confusing part. When I annotate ChangeQuantityTransaction with @Configurable, the method setDataSource (in the abstract parent) is called only once. If I annotate the class with @Component, setDataSource is called twice.

    Why do the different annotations cause setDataSource to be called a different number of times? Looking at the Spring debug output I can see the bean is created as a singleton, but I don't see why it is being initialized twice, and I can't find anything in the reference material that tells me why @Configurable and @Component would behave in a different manner.

    Another question - which annotation is better here, @Configurable or @Component?

    Thanks,

    Paul

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Is it called on the same instance or is it simply called twice. I suspect that you have component-scanning enabled, which would lead to double instantiation of your bean. Also why would you want to use @Configurable or @Component, as you already specified the bean in xml...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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