Results 1 to 2 of 2

Thread: Maintaining ApplicationContext in custom wrapper

  1. #1
    Join Date
    Oct 2004
    Posts
    9

    Default Maintaining ApplicationContext in custom wrapper

    Hi all.

    Using Spring across an API layer that is meant for use in both the Web and EJB layers. I have a collection of DAO's managed by the Spring container, and I want to correctly manage the instance of the ApplicationContext so that it is created and the config parsed only once. I have seen in the documentation (and Rod's book) that falling back to the singleton in this case may make sense. I am wondering if I can have a POJO factory class which wrapps a static BeanFactory:

    Code:
    public class DAOFactory
    {
        private static ApplicationContext context;
    
        public DAOFactory()
        {
        }
        
        static
        {
            try
            {
                context = new ClassPathXmlApplicationContext("app-config.xml");
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
    
        }
    
        protected BeanFactory getBeanFactory()
        {
            return context;
        }
        
        public Object getBean(String name)
        {
            return getBeanFactory().getBean(name);
        }
    
        public UserDAO getUserDAO()
        {
            return (UserDAO).getBean("UserDAO");
        }
    Any issues with this approach? Do I need to wrap the DAOFactory as a singleton as well?

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You can also make your DAOFactory implements ApplicationContextAware and configure it as a POJO. This way, DAOFactory will be aware of other beans configured in other contexts.
    For eaxmple, if your are using OpenSessionInViewFilter, which is a web compenent, you need to configure the same SessionFactory for both the filter and the DAOs.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Replies: 2
    Last Post: Sep 1st, 2009, 09:24 AM
  2. Replies: 2
    Last Post: Aug 2nd, 2006, 10:18 PM
  3. Replies: 3
    Last Post: Nov 15th, 2005, 03:24 PM
  4. Replies: 4
    Last Post: Jun 8th, 2005, 06:22 AM
  5. Obtaining ApplicationContext for web and batch apps
    By Thomas Vaught in forum Container
    Replies: 10
    Last Post: May 23rd, 2005, 07:09 AM

Posting Permissions

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