Announcement Announcement Module
Collapse
No announcement yet.
ApplicationContext vs BeanFactory with Transactions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ApplicationContext vs BeanFactory with Transactions

    Dear community,

    I've read many posts in this forum and on the web in general pointing out the difference between an ApplicationContext and a BeanFactory but was not able to find a clear answer to the question I'm going to ask.
    Here are some links I checked (among many others):
    • ht*p://forum.springframework.org/showthread.php?t=30593&highlight=applicationcontex t+beanfactory
    • ht*p://forum.springframework.org/showthread.php?t=45694
    • ht*p://static.springframework.org/spring/docs/2.0.x/reference/beans.html#context-introduction

    We're using Spring 2.0 transactions in our current project. As I was using an XmlBeanFactory at first, transactions would not work. When I switched to ApplicationContext, everything worked fine (after some changes).
    Now, I would really like to fully understand the destinct difference between the ApplicationContext and BeanFactory when it comes to transactions, as this is not fully clear to me.

    I do understand that ApplicationContext adds functionality as described in Springs reference guide, chapter 3.8. I've read that, in order to use Spring AOP (which is required to use transactions), one needs to use ApplicationContext because BeanFactory "...simply doesn't do it". Well, this doesn't give me a lot more insight, does it?

    For example, how does ApplicationContext know to look out for a bean named transactionManager? Can I see this somewhere in the code? I've been debugging Spring/Hibernate code but didn't find the correct spot I guess...

    Can somebody share their knowledge with me on this matter? Especially, I'd like to know more about the difference between the ApplicationContext and BeanFactory when it comes to transactions (as mentioned earlier). Nevertheless, any thought and insight on this topic is appreciated!

    Cheers

  • #2
    Spring Transactions are implemented on top of Spring AOP.

    Spring AOP is implemented on top of BeanPostProcessor's.

    3.7.1. Customizing beans using BeanPostProcessors:
    It is important to know that a BeanFactory treats bean post-processors slightly differently than an ApplicationContext. An ApplicationContext will automatically detect any beans which are defined in the configuration metadata which is supplied to it that implement the BeanPostProcessor interface, and register them as post-processors, to be then called appropriately by the container on bean creation. Nothing else needs to be done other than deploying the post-processors in a similar fashion to any other bean. On the other hand, when using a BeanFactory implementation, bean post-processors explicitly have to be registered, with code like this:
    Code:
    ConfigurableBeanFactory factory = new XmlBeanFactory(...);
                
    // now register any needed BeanPostProcessor instances
    MyBeanPostProcessor postProcessor = new MyBeanPostProcessor();
    factory.addBeanPostProcessor(postProcessor);
    
    // now start using the factory
    About the spring internals - feel free to check implementations of the NamespaceHandler interface that are used for backing various spring config elements.

    Regarding default transaction manager name - check org.springframework.transaction.config.AnnotationD rivenBeanDefinitionParser.DEFAULT_TRANSACTION_MANA GER_BEAN_NAME constant for @Transactional-driven stuff and org.springframework.transaction.config.TxAdviceBea nDefinitionParser.doParse() method for tx-namespace-driven transactions:

    Code:
    	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    		// Set the transaction manager property.
    		String transactionManagerName = (element.hasAttribute(TxNamespaceUtils.TRANSACTION_MANAGER_ATTRIBUTE) ?
    				element.getAttribute(TxNamespaceUtils.TRANSACTION_MANAGER_ATTRIBUTE) : "transactionManager");
    		builder.addPropertyReference(TxNamespaceUtils.TRANSACTION_MANAGER_PROPERTY, transactionManagerName);
    
    		List txAttributes = DomUtils.getChildElementsByTagName(element, ATTRIBUTES);
    		if (txAttributes.size() > 1) {
    			parserContext.getReaderContext().error(
    					"Element <attributes> is allowed at most once inside element <advice>", element);
    		}
    		else if (txAttributes.size() == 1) {
    			// Using attributes source.
    			Element attributeSourceElement = (Element) txAttributes.get(0);
    			RootBeanDefinition attributeSourceDefinition = parseAttributeSource(attributeSourceElement, parserContext);
    			builder.addPropertyValue(TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE, attributeSourceDefinition);
    		}
    		else {
    			// Assume annotations source.
    			Class sourceClass = TxNamespaceUtils.getAnnotationTransactionAttributeSourceClass();
    			builder.addPropertyValue(TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE, new RootBeanDefinition(sourceClass));
    		}
    	}

    Comment


    • #3
      Hey Denis

      Thanks for you immediate response and my apologies for the delayed response.

      Thanks for the hints...checked the code and do understand a little bit better how Spring handles it.

      Regards

      Comment

      Working...
      X