Results 1 to 3 of 3

Thread: Integrating Spring JDBC into application

  1. #1
    Join Date
    Jul 2005
    Posts
    1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

  3. #3
    Join Date
    Aug 2004
    Posts
    19

    Default

    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

Similar Threads

  1. Replies: 5
    Last Post: Aug 9th, 2008, 05:30 AM
  2. Replies: 3
    Last Post: Aug 12th, 2005, 12:36 PM
  3. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  4. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM
  5. Integrating Spring JDBC and WebWork2
    By gaffonso in forum Data
    Replies: 1
    Last Post: Oct 15th, 2004, 03:28 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
  •