Announcement Announcement Module
Collapse
No announcement yet.
Maintaining ApplicationContext in custom wrapper Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X