Monday, July 20, 2009

XmlBeanFactory vs ClassPathXmlApplicationContext

In my previous blog, I explained how to use ClassPathXmlApplicationContext. Today when I debug a application, I found below issues regarding XmlBeanFactory.

When we use XmlBeanFactory, TX annotation is not working. and it does not even load when you say in XML file.

So if you want to use annotation, please use ClassPathXmlApplicationContext. I believe in WEB application, tomcat system actually uses ClassPathXmlApplicationContext. So in order to make your code testing separately, please do not use XmlBeanFactory.

Below it is the example of XmlBeanFactory and ClassPathXmlApplicationContext

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"applicationContext2.xml"));
InvoiceProcessDao process = (InvoiceProcessDao)factory.getBean("invoiceProcess");


ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext3.xml");
InvoiceProcessDao process = (InvoiceProcessDao)ctx.getBean("invoiceProcess");
process.checkPayment();
System.out.println(" Over") ;

2 comments:

  1. This is correct. I too had faced problem with @@Transactional when using XmlBeanFactory to load the beans. Thanks for posting this. I wish i would have found this a few days back...

    ReplyDelete
  2. Thanks for sharing...

    But rather than saying TX annotation is not working. and it does not even load when you say in XML file.

    You could have said more clearly like the person has commented above @@Transactional...

    Because many ppl like me do not know what is TX annotation...

    ReplyDelete