Hi,
I've got a class that looks something like this:
AbstractTransaction class looks 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 } }
Configuration (admin-servlet.xml):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); } }
I am working with a legacy servlet and get a changeQuantityTransaction in the servlet's init method: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>
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


Reply With Quote