Wednesday, January 16, 2008

Spring and JMX: BeanFactory Versus ApplicationContext

When using the Spring Framework, one can use the BeanFactory interface or the ApplicationContext interface to instantiate and configure/wire beans for use in a Spring-based application.

ApplicationContext extends BeanFactory and adds significant new functionality. The Spring Reference (PDF) recommends using ApplicationContext rather than BeanFactory in most cases, especially for support of enterprise functionality.

I have run into this difference when using Spring's JMX support. It is a trivial matter to wire Spring beans to an MBean server using an ApplicationContext such as FileSystemXmlApplicationContext or ClassPathXmlApplicationContext.

Another difference between BeanFactory and ApplicationContext is support for Aspect-Oriented Programming (AOP) as discussed in this blog entry related to an omission in the second edition of the book Spring in Action.

Other discussions regarding use of ApplicationContext versus BeanFactory can be found in Spring Best Practices, BeanNameAutoProxyCreator and BeanFactory Versus Application Context, Practical Spring (Page 8), Introduction to the Spring Framework, and Overview of the Spring Framework.

The article Spring: Creating Objects So You Don't Have To goes into more depth on BeanFactory and ApplicationContext.

No comments: