14 Replies Latest reply on Feb 7, 2006 5:55 AM by adrian.brock

    How to access beans created by the bootstrap

    rpelisse

      Hi,
      I come from the Spring world ( please don't throw tomatoes to my face : ) ! ). I read the microcontainer getting started. I can see that most features of the spring core are reproduced here and i read the xml configurations quite easily. However i have hard time understand just one thing.
      Once I call the StandaloneBootstrap().bootstrap(), wich basicly parses the XML file and creates the proper beans, how can I get a reference to those beans.

      In the Spring framework, i add a static method such as :

      ApplicationContext cxt = new FileSystemXmlApplicationContext("config.xml");
      cxt.getBean("myBean");

      But i didn"t see anything like this in the 'A Guide For POJO developpers' , neitheir in the example... How can get back my instanciated bean ?

      I hope that my question isn't too silly...

        • 1. Re: How to access beans created by the bootstrap

          I strongly discourage this usage. It defeats the whole point of IOC.

          To me, these examples from Spring represent a failure. i.e. a fallback to the old
          service locator pattern because of a lack of thought.

          There are other service locators (such as jndi) that make your code much more
          portable across frameworks.

          The xml/deployment is just a facade/convenience over the kernel controller.

          Sometimes the direct access cannot be avoided, usually laziness or no requirement
          to not depend on the framework, e.g. unit tests
          http://anoncvs.forge.jboss.com/viewrep/~raw,r=1.4/JBoss/microkernel/src/main/org/jboss/test/kernel/junit/MicrocontainerTestDelegate.java

          • 2. Re: How to access beans created by the bootstrap

             

            "belaran" wrote:
            I read the microcontainer getting started. I can see that most features of the spring core are reproduced here


            Actually it is an independent invention.

            The microcontainer is based on JBoss's JMX kernel, which predates Spring
            by a number of years.

            I was persuaded to use some of Spring's xml names,
            e.g. property instead of attribute (although some of the class names still contain the old names)
            and I did steal
            <null/>
            when Scott pointed out this was missing. :-)

            I think we also have some features that are not in Spring?
            I don't know for sure I've never directly used Spring. :-)

            • 3. Re: How to access beans created by the bootstrap

               

              "adrian@jboss.org" wrote:

              The xml/deployment is just a facade/convenience over the kernel controller.


              If you are interested, there is an open feature request to create a Spring facade.
              i.e. Spring xml -> JBossXB parser->JBoss MC metadata
              and not just for Spring:
              http://jira.jboss.com/jira/browse/JBMICROCONT-26

              • 4. Re: How to access beans created by the bootstrap
                rpelisse

                First of all, thanks you Adrian for all your response. Sorry I didn't reply sooner.

                About doing a Spring facade, i believe that will be a great idea but I don"t know if I got what it's take to do it. I'm still pretty new to all this and the person(s) doing this job should be very good with both IoC Framework. However, I'll consider the challenge...

                I strongly discourage this usage. It defeats the whole point of IOC.


                I agree completly with you. The method "getBean' is, the way I see it, just a way to make test while trying the Spring framework...

                However, while doing my research an Spring and Hivemind, i found a lot of critics against the use of JNDI locator, which tends to tie the webApp to the locator, making portability and unit test not simple [ that is what i read,not an opinion ].

                So let's say for the purpose of the exercice that we agree that Locator is not an option, how can we get back the Bean from the Microcontainer ?


                • 5. Re: How to access beans created by the bootstrap

                   

                  "belaran" wrote:

                  However, while doing my research an Spring and Hivemind, i found a lot of critics against the use of JNDI locator, which tends to tie the webApp to the locator, making portability and unit test not simple [ that is what i read,not an opinion ].


                  So let's rephrase it - although I doubt they were talking about getBean()
                  and more advocating IOC.

                  "Don't tie yourself to a spec defined locator, use our proprietary api instead?" :-)


                  So let's say for the purpose of the exercice that we agree that Locator is not an option, how can we get back the Bean from the Microcontainer ?


                  See the link to the code I posted or read this:
                  http://www.jboss.com/index.html?module=bb&op=viewtopic&t=70357

                  • 6. Re: How to access beans created by the bootstrap

                    That unit test helper class with examples will be included in the next
                    (1.0.2 - still unscheduled) release of the microcontainer.

                    • 7. Re: How to access beans created by the bootstrap
                      rpelisse

                       

                      "adrian@jboss.org" wrote:

                      "Don't tie yourself to a spec defined locator, use our proprietary api instead?" :-)


                      : D

                      Thanks ! I'll watch the code, the two link your provided me should be enough if i'm not too dumb...

                      • 8. Re: How to access beans created by the bootstrap
                        rpelisse

                        I know this thread is now old and that I haven't post anything but my work took me away from JBoss Microcontainer for a while.... But finally, I understand what Adrian meant in his examples, and why there is no "getBean()". The whole idea is quite simple, but given the fact that I had hard time with it, maybe other will, so i'll explain here what i finally understood.

                        Basicly my question was "how can i get my bean from the bootstrap in a simple "main' program ?" . In a small spring app, i would have done this :

                        public class MyApp
                        {
                         public static void main(String[] args)
                         {
                         ...
                         Component comp = context.getBean("component');
                         }
                        }



                        When i look at JBoss Container documentation and examples, i was looking for this kind of behavior. A method to call, something where my program could actually use the 'created bean'. And there I was wrong. As Adrian stated, the use of a '.getBean' method ruins the all concept of IoC as it tie the program to a specific API ( in this case Spring API).

                        I agreed ( and i still agree) but i didn't see how to do without it, even when i look into the example. Then, it finally struck me ( i know, i know i'm quite slow) : There is actually no need for my program to get beans from the microcontainer as my program is another bean managed by the container. At the bootstrap , the microcontainer will create my program and execute its initialisation method ( or simply its constructor) and there the program actually start. So there is no 'main', no need to actually get the bean for the program. The progam is a POJO and its dependancies are resolved by the container !
                        We simply launch the bootstrap instead of the program.

                        Now this all seems so clear, i almost feel stupid... : /
                        I hope that this post will help other, stuck in the same misconception... ( otherwise this post will simply be another public humiliation).

                        Thanks again Adrian !








                        • 9. Re: How to access beans created by the bootstrap

                          Some time the direct call is more convenience and comfortable.

                          wounld you like to provide a solution?

                          thanks in advance.

                          • 10. Re: How to access beans created by the bootstrap

                            Direct access gives developer more freedom to controll his code and architecture,more room to design ,especaily for R&d.

                            • 11. Re: How to access beans created by the bootstrap

                              for example,I will use MC with jboss portal.

                              Can portlet be a bean and defined in the jboss-beans.xml?or a servlet?

                              if can,that's will be easy.
                              else,
                              Beans defiend in jboss-beans.xml is hard to access in portlet in a simple way.

                              I think that is not the MC's aim.

                              • 12. Re: How to access beans created by the bootstrap

                                maybe jboss seam will provide a way?

                                • 13. Re: How to access beans created by the bootstrap

                                  jboss seam use bean as a datasoure

                                  • 14. Re: How to access beans created by the bootstrap