Announcement Announcement Module
Collapse
No announcement yet.
Integrating Spring JDBC into application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integrating Spring JDBC into application

    I've started looking into integrating Spring JDBC into our application and had a few questions/issues.

    Currently, I have Struts calling a Manager class and then the Manager class will call the DAO. When I rewrite the DAO to use Spring JDBC, I'm having a hard time understanding how to call it from the Manager. For example, I've defined the beans for the DAO, DataSource, and Manager class in the spring config file. I've run this in debug mode inside of my IDE and I can see that Spring is creating the Manager and setting the instance of the DAO on the Manager. What's confusing me is how should the action class now call the manager? Each time I do a Manager m = new Manager(), isn't that creating a new instance and bypassing what Spring has already done for me? I don't see the entry point in which I pick up the Spring Managed Bean instead of the new instance that is being created by the new keyword.

    I know I'm probably misunderstanding a very simple spring concept, but I've been struggling with this concept today. I've gotten it to work with using Spring from front to back, just not yet with plugging in the JDBC api.

    Thanks

  • #2
    Each time I do a Manager m = new Manager(), isn't that creating a new instance and bypassing what Spring has already done for me?
    Yes. If you've added a transactional proxy to thi class in Spring, it won't be applied either.

    You need to use IoC. Take a look at WebApplicationContextUtils usage in the Struts part of the JPetStore sample in the distribution.

    You may also be interested in the org.springframework.web.struts package for Struts support.

    Comment


    • #3
      I have a Struts BaseAction class from which my other Actions are extended. In the BaseAction I have code like:
      Code:
      private WebApplicationContext wac;
      
      public void setServlet(ActionServlet actionServlet) {
              super.setServlet(actionServlet);
              ServletContext servletContext = actionServlet.getServletContext();
              wac = WebApplicationContextUtils
                      .getRequiredWebApplicationContext(servletContext);
              myManager = (Manager) wac.getBean("myManager");
          }
      
      protected Manager getManager() {
              return myManager;
          }
      So in my Actions, I simply call getManager() to get the Spring configured manager.

      Spring has some helper classes for Struts integration, but I haven't tried them yet. The above works for me.

      Will

      Comment

      Working...
      X