Announcement Announcement Module
No announcement yet.
programmatic bean and factory creation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • programmatic bean and factory creation

    Of course, declarative bean definitions are optimal. But, there may be use cases where one requires programmatic definitions, for example, a desire to use the container as just a prebuilt Abstract Factory pattern component.

    By looking at some of the unit tests and some of the javadocs I can figure out how to create a bean factory and a bean with some properties. So, one can do something like:

            DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
            factory.registerBeanDefinition("beanname", new RootBeanDefinition(com.example.Foo.class, new MutablePropertyValues()));
    But, how to do wiring and other things that are easy using the XML definition format?

    It is not possible to figure out by just using the reference guide (unless I missed it or I just need to put more effort into it).

    Why are there no examples of programmatic bean construction and wiring in the reference? Or let me know if I'm missing more documentation on this.

  • #2
    If you're going to do it programmatically, why use Spring at all?


    • #3
      Originally posted by rhasselbaum
      If you're going to do it programmatically, why use Spring at all?
      Good point. As in JUnit tests, one can just manipulate POJOs outside of the Spring container.

      The way I read it, XML is just one bean definition format, and I think its the best too. So, if you still want to take advantage of the bean container for various reasons, like removing the 'evil' singletons, and don't want to get involved in external XML loading, Programmatic or even scripted definitions are options. This is the scenario I am referring to.


      • #4
        Since the XML configured bean factory and application context are built on programmatic apis all the functionality is available programmaticdally, but unfortunately ithere's not much documentation for this other than JavaDocs....


        • #5
          I have experimented with a more convenient programmatic approach to bean definitions than the present BeanDefinition approach (the raw metadata Spring uses itself), and it will probably make 1.3.

          Nevertheless, programmatic config is possible now, and autowiring can be very useful in adding objects to an existing config.