Announcement Announcement Module
Collapse
No announcement yet.
Writing a Util class with all static methods using Bean ... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing a Util class with all static methods using Bean ...

    [Subject: Writing a Util class with all static methods using Bean Factory]

    Hi all,

    I was instructed to write a util class that queries the database at startup, cache the results and mark all the methods in this class as static so that everyone at different tiers can use the methods provided by this class easily.

    Bg info: Using Spring Final Released 1.1.1 with IBatis 2.0.6

    This is my intended approach but wasn't sure if this is the correct way.

    1. Create a Dao interface and its implementation class to query the database and make use of IBatis caching model to cache the results.

    2. Create a Bean interface and its implementation class to call this Dao's methods to propagate them to the Util class.

    3. In my Util class, declare this Bean interface as final static global variable, declare all methods as static and then use this static Bean to implement the various methods.

    i.e:

    Util.java:

    Code:
    public class Util {
        static final Bean myBean;
    
    
         static {
             ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
              myBean = ctx.getBean("Bean");
         }
    
    
          public static List getResults() {
             return myBean.getResults();
           }
    
           public static List convertFormat(List paramList) {
              return myBean.convertFormat();
           } 
    }

    Bean.java

    Code:
    public class Bean {
            private Dao myDao;
    
            public void setMyDao(Dao myDao) {
                this.myDao = myDao;
            }
    
            public List getResults() {
                 myDao.getResults();
            }
    
            public List convertFormat(List paramList) {
                List newConvertedList = new ArrayList();
    
                for (Iterator iter = paramList.iterator(); iter.hasNext();) {
                   VO currVO = (VO) iter.next();
                    /*** some conversion codes ***/
                    newConvertedList.add(currVO);
                 }
    
                 return newConvertedList;
            }
    
    }

    From what I know, the number of records in this result list can be as large as 1 million.

    My questions:
    1. Would the static "convertFormat" in my Util class become a performance bottleneck because everytime someone does a Util.convertFormat(paramList), Spring will need to get the context, bean, dao, ... and finally convert the list to the correct format?


    2. Do you think it is a better idea to take away the Util class and expose the bean to the rest of the developers? i.e: To let someone at a different tier, i.e: DAO level, to access this bean?


    i.e: AnotherDao.java

    Code:
    public class AnotherDaoSqlMapDao extends .... {
        private Bean myBean;
    
        {
             ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
              myBean = ctx.getBean("Bean");
         }
    
         ....
    }

    TIA.

    Mingshun

  • #2
    I don't know the larger issues that your team may be dealing with; but it seems to me the proposed solution really defeats a lot of the usefulness of Spring. Typically you'd let Spring do the lookup of any DAO and/or other wrapper objects rather than explicitly looking them up in your code. Also, note that typically Spring manages beans as if they were singletons without the need for making them actual singletons, or using static methods - when Spring sets the object it is the same (unless you override this in the config file).

    I don't know is the proposed convertFormat would be a bottle-neck or not; I'd say probably not, but you'd need to do some tests to be sure.

    I think, if it is possible a better way is to take away (or at least heavily refactor the Utils) and use Spring to set the DAO object into the other classes that need it - making this completely transparent to your code, and potentially enabling switching to a different IOC implementation (as all the set / lookup logic would be in the config files). But again, this depends on some larger questions about how the pieces fit together - if your team is working with some weird archetecture then explictily initializing the Spring context might be required.

    Comment


    • #3
      What I really mean in my first post is to let Spring do the lookup of any DAO and use the Utils class to wrap the Spring Bean and expose its methods as static so that everyone can use it at any tier without doing any lookup.

      NB:
      The reason why my design is so weird is because this big chunk of codes was initially resting on a different project with a completely different architecture with no JUnit Testing. Hence I do not want to refactor too much but rather write a wrapper on it.

      Comment


      • #4
        Ok, I think I see what you're getting at. Unless you want to do more refactoring (from the code you give you could either make Bean.convertFormat static, or pull it out to the Util class. With a little bit of work you should be able to get everything to work pretty well - I think the static initializer call you're making will only be done the first time the class is referenced, so it shouldn't be an issue.

        Comment


        • #5
          I have the same type of situation in that I'm refactoring an architecture that does DL of collaborators via a static plugin factory. I modified it sort of like your util class where 'myBean' would work from the context.

          My plan is to treat this as a placeholder, and as I refactor the clients of my static class, switch them to accept DI and wire them in the conext.

          BTW, I am using a WebApplicationContext with Struts, so the static initializer just creates an instance of the bean and I have a static setter that will set the context in the bean externally, which is called from a ContextLoaderPlugin.

          Comment


          • #6
            MethodInvokingFactoryBean

            I realize this is rather old discussion, but had been looking for something to do this and thought I would post this link in case others are looking for something that comes with Springframework now http://static.springframework.org/sp...ctoryBean.html

            Comment

            Working...
            X