Announcement Announcement Module
Collapse
No announcement yet.
Spring IoC Question ... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring IoC Question ...

    Hi,
    I want to use Spring IoC into my web application to have more abstraction between the layers.

    So I defined some Singletons in the xml that Spring uses (I called it springBeans.xml).

    When I want to access a bean defined in springBeans.xml,
    I noticed Spring instanciate a new bean each time.

    But in some classes, I do some things in the constructor that I want to do only once. But since Spring is creating a new instance each time someone needs the class, the constructor gets called more than once.

    For example I am loading a jdbc driver in a constructor's class defined in spring's xml.

    It will load it each time I have a request for the bean.

  • #2
    It sounds like you may be creating multiple ApplicationContexts. Make sure that you are just creating one ApplicationContext and getting all of your beans from there. The typical way to do this is to have a ContextLoaderListener in your web.xml which will create a single WebApplicationContext. Check the sample applications for an example.

    Comment


    • #3
      Hi,
      I'm not always accessing with application context but with the getBean method too.
      I have classes that aren't servlets that need to access the beans so I can't use the application context.
      It's like u are supposing that I need to put all my Spring beans in the application context.

      I have layers : Presentation - Front Controller+Controllers - Business - DAOs
      For example, for the Business objects I can't access DAOs with the application context,
      I use getBean but it creates everytime a new bean.

      Comment


      • #4
        Right. I don't mean the servlet container's "application context" but Spring's ApplicationContext object. An ApplicationContext object is a BeanFactory and calling getBean on different BeanFactory instances will return new beans. So you need to make sure you only have one ApplicationContext/BeanFactory instance that is shared by all of your layers that call getBean.

        The WebApplicationContext is one way to have a single, shared ApplicationContext. Another way to do that is to use the SingletonBeanFactoryLocator. Just make sure you aren't doing something like creating new FileSystemXmlApplicationContext instances every time you call getBean

        Ideally you don't want to call getBean anywhere. What you'd like to do is have Spring wire your objects together for you. So let Spring wire your DAOs to your Business layer instead of having your business layer call getBean to find the DAO.

        Comment


        • #5
          dgynn, thanks for your response!

          I don't know if I have several Srping-ApplicationContexts.
          How can I see ?
          I have one xml => springBeans.xml
          When I want to access a bean I do this for example :
          IGestionUtilisateurs gestionUtilisateurs =
          (IGestionUtilisateurs)
          factoryBeans.getBean("gestionUtilisateurs");

          U tell me I shouldn't call getBean anywhere,
          >>So let Spring wire your DAOs to your Business layer
          >>instead of having your business layer call getBean to find the DAO.
          How can I do this ?

          Comment


          • #6
            You have one xml file, but every time you load that file you are creating a new BeanFactory object. The code you have there is correct, but you need to make sure that "factoryBeans" is being instantiated once.

            Take a look at the samples that come with Spring. The PetStore sample shows DAOs being wired to business objects.

            Comment


            • #7
              Hello,
              sorry I had 3 weeks exams,
              I see the petstore sample,
              is there any tutorial on it ?
              I looked through this website but can't find one.

              Comment


              • #8
                Originally posted by matthew016
                Hello,
                sorry I had 3 weeks exams,
                I see the petstore sample,
                is there any tutorial on it ?
                I looked through this website but can't find one.
                Just focus on the IoC-part of the example application. You'll find it out without tutorials and documentations. Just follow the idea of IoC and start "injecting" beans in your application aswell. It's quite simple, but really efficient

                Comment


                • #9
                  Thank u!
                  Actually I went into jpetstore before posting my reply, to check the IoC,
                  But I can't find the Spring factory !

                  Comment


                  • #10
                    Originally posted by matthew016
                    Thank u!
                    Actually I went into jpetstore before posting my reply, to check the IoC,
                    But I can't find the Spring factory !
                    Take a look into the petclinic example application. The "\war\WEB-INF\"-folder contains 'applicationContext-hibernate.xml'-file + some other xml-files aswell. There you can find the factory-classes or beans what they are in Spring application.

                    I hope you are already familiar with the IoC concept. If not, study it first (injecting beans etc).

                    The basic idea in a MVC application is that you don't pull any beans from applicationContext. Instead of pulling, you inject them. On application start (e.g. when Tomcat starts the application) the application is "glued" together. This is done by the bean configurations.

                    I hope this helps... I think there are some other more easy tutorials and sample applications on the net. I'll see if I can find one.

                    Comment


                    • #11
                      I think I understand what u mean,
                      correct if I'm wrong :
                      instead of using "getBean("...")" in my application (what u call "pull" a bean),
                      I should have the bean as a variable in the class when the application starts
                      (thanks to the xml bean factory)

                      But I have several problems/questions when I want to inject them :

                      1. I have a controler, a servlet, and I don't know if I can pull a bean in my servlet ? (since I it uses method init and I have no constructor ?)

                      2. I noticed that Tomcat loads several times the Bean factory ...
                      and by loading the bean factory again, it instanciates again all the beans.
                      This is annoying because in my beans I have arrays, that I want to keep those arrays.
                      (Also I don't really understand what a "applicationContext" is)
                      Last edited by matthew016; Sep 11th, 2006, 04:57 PM.

                      Comment


                      • #12
                        Hi Matthew,

                        Originally posted by matthew016
                        1. I have a controler, a servlet, and I don't know if I can pull a bean in my servlet ? (since I it uses method init and I have no constructor ?)
                        In reference to this question, again check the sample applications. Notice the following in the PetClinic sample application:

                        1) There is a ClinicController (src\org\springframework\samples\petclinic\web) that has a dependency on the Clinic interface. The controller provides a setter method to allow for dependency injection.

                        2) In the petclinic-servlet.xml file (war\WEB-INF), the "clinic" bean is injected into the "clinicController" bean.

                        3) The "clinic" bean is defined in the applicationContext-jdbc.xml file (war\WEB-INF).

                        So the service layer "clinic" bean is injected into the web controller, even though it is defined in a different (parent) BeanFactory.

                        This is possible because of the following code in the web.xml (war\WEB-INF) file:
                        Code:
                        <context-param>
                           <param-name>contextConfigLocation</param-name>
                              <param-value>/WEB-INF/applicationContext-jdbc.xml</param-value>
                        </context-param>
                        This ensures that the applicationContext file listed above is the parent application context of the web application context. If you follow these guidelines, you can benefit from 100% dependency injection for your web layer beans as well!

                        I don't completely understand what you were asking with #1, but I just wanted to show that it is easy to inject the required beans into your controller without ever having to resort to "getBean" calls.

                        Were you asking something different?

                        -Arthur Loder

                        Comment


                        • #13
                          Hi,
                          thank u, I have seen how they do it in the petclinic example.

                          But I still can't injecft a bean into my controller,

                          I asked it to someone else and he says me I can't inject the bean into my controller because it is a servlet, and instanciated by tomcat, so it cannot be related to Spring IOC.

                          I need to put the beans in the application scope of my web application and get them from the context in my servlet.
                          Last edited by matthew016; Sep 21st, 2006, 11:46 AM.

                          Comment


                          • #14
                            Hi matthew016,

                            Originally posted by matthew016 View Post
                            But I still can't injecft a bean into my controller
                            Can you post some of your configuration files (such as your web.xml, your [servlet-name]-servlet.xml, and any applicationContext files)?

                            -Arthur Loder

                            Comment


                            • #15
                              I did a type error last post :

                              I asked it to someone else and he says me I can't inject the bean into my controller because it is a servlet, and instanciated by tomcat, so it canNOT be related to Spring IOC.

                              Here is what he told me :

                              The servlet gets created by tomcat, it knows nothing about spring.
                              And to make matters worse tomcat may create more than one instance of your servlet



                              SO if the servlets cannot be related to Spring IoC, no use I give my code.


                              I need to put the beans I want to share with my servlet controller into the application scope,
                              but when I want to access the spring factory with : new XmlBeanFactory(new ClassPathResource("springBeans.xml"));
                              it loads every bean again, I need a way to access the factory without creating a new one to get the beans I want to share with my servlet and put them in the application scope.
                              Last edited by matthew016; Sep 21st, 2006, 11:27 AM.

                              Comment

                              Working...
                              X