Announcement Announcement Module
Collapse
No announcement yet.
Should Contexts themselves be configured by DI? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Should Contexts themselves be configured by DI?

    Was writing a util and, of course, now everything will be a pojo (which lead me to some OOP ponderings...) and then had another thought.

    When one creates contexts one configures them in external places, such as web xml or java code. Specifically the hierarchy. Thus, I'm pondering, shouldn't there we a way to declaritively define this hierarchy within the context definitions?

    For example, the beans element could have a 'contexts' attribute that contains a list of contexts that partake in a relationship. Better yet, beans could have a child element, 'contexts', that can contain a richer element content.

    Why?
    - One value add is that it constrains the look-up of beans. Given context xml files, unless one resorts to code inspections of other configuration data (web.xml, xxx-servlet.xml, ..) one does not know what bean is resolved. Or one can use the IDE tool. And if a Spring module is componentized how do we know bean names really are unique or what the deployer or user configures the application? This may be out of scope of course.

    - To support standalone 'components' (there is another thread in this forum about this).

    - Another use, is to allow DI of contexts. Thus, no need to use xml includes for example.

    - Still another is easier tools to support Spring use (I bet the Spring Eclipse plug-in would be easier to write).

    I don't like the use of 'parent' for this since this precludes the use of siblings or other richer relations.


    Example:

    <beans blah blah......>

    <contexts>
    <list>
    <context refID="root" access="public" ..... etc.>
    </list>
    </contexts>
    </beans>


    I hope after all this typing someone doesn't tell me this is already in Spring, just do a one line thing somewhere.


    --- Josef Betancourt

  • #2
    Re: Should Contexts themselves be configured by DI?

    Originally posted by jbetancourt

    I hope after all this typing someone doesn't tell me this is already in Spring, just do a one line thing somewhere.

    --- Josef Betancourt
    This is already in Spring, :-), although not necessarilly exactly how you describe it. But you can certainly define a hierarchy of contexts, using the keyed singleton variants of BeanFactoryLocator. Look here.

    Regards,

    Comment


    • #3
      I knew that would happen. LOL!

      The funny thing is that I use BeanRef stuff in this prototype I'm doing.

      But, you are right, a different approach. Seems the referenced approach is more 'programmatic', but it does leverage Spring better.

      Whereas, with what I proposed it seems more declarative, though the results would be the same.

      Cheers,

      Comment


      • #4
        Colin:

        To continue...

        1. If the beans element had an id attribute then it would allow some new capabilities.
        Thus, using the composite pattern, the beans collection itself becomes a bean, a "can of beans" . The present way Spring works, though great, is like combing one's head one hair at a time.
        A. Namespace like support. For example: ctx.getBean("utils:transformer");
        B. Easier beans graph configuration.
        C. Conditional configuration.
        E. and more

        Of course, Spring doesn't work this way or will. I'm just noting some thoughts.

        2. Here is an example of what I'm talking about. This is the same example shown
        in the api at http://www.springframework.org/docs/...ryLocator.html
        See the last example, where multiple config files are used in a hierarchy. The source element below hides what is actually used to load the beans config.

        Code:
        Definitions of beans as "cans" &#40;as in can of beans, grin&#41;.
        
         <?xml version="1.0" encoding="UTF-8"?>
         <!DOCTYPE beans ..............>
         <beans id="com.mycompany.myapp.util" lazy-init="true">
             <source><value>classpath&#58;com/mycompany/myapp/util/applicationContext.xml</value></source>
         </beans>
        
         <?xml version="1.0" encoding="UTF-8"?>
         <!DOCTYPE beans ..............>
         <beans id="com.mycompany.myapp.dataaccess" lazy-init="true" parent="com.mycompany.myapp.util">
             <source><list><value>classpath&#58;com/mycompany/myapp/dataaccess/applicationContext.xml</value></list></source>
         </beans>
        
         <?xml version="1.0" encoding="UTF-8"?>
         <!DOCTYPE beans ..............>
         <beans id="com.mycompany.myapp.services" lazy-init="true" parent="com.mycompany.myapp.dataaccess">
             <source><list><value>classpath&#58;com/mycompany/myapp/dataaccess/services.xml</value></list></source>
         </beans>
        
         <?xml version="1.0" encoding="UTF-8"?>
         <!DOCTYPE beans ..............>
         <beans> <!-- define an alias -->
           <bean id="com.mycompany.myapp.mypackage" class="java.lang.String">
             <constructor-arg><value>com.mycompany.myapp.services</value></constructor-arg>
           </bean>
         </beans>
        --- Josef Betancourt

        Comment


        • #5
          I use code

          <constructor-arg>
          <list>
          <value>classpath:datasource_beans_test.xml</value>
          <value>classpath:app_beans.xml</value>
          </list>
          </constructor-arg>

          and place these two files in the

          /WEB-INF/datasource_beans_test.xml
          /WEB-INF/app_beans.xml

          But I got the following error:

          org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'all_beans' defined in file [C:\devel\webapps\awards\WEB-INF\awards_beans_test.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class [org.springframework.context.support.ClassPathXmlAp plicationContext]; constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [datasource_beans_test.xml]; nested exception is java.io.FileNotFoundException: Could not open class path resource [datasource_beans_test.xml]
          org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'all_beans' defined in file [C:\devel\webapps\awards\WEB-INF\awards_beans_test.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class [org.springframework.context.support.ClassPathXmlAp plicationContext]; constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [datasource_beans_test.xml]; nested exception is java.io.FileNotFoundException: Could not open class path resource [datasource_beans_test.xml]

          Could you tell me how to specify the location of files?

          -Henry

          Comment


          • #6
            Henry,

            What does this have to do with this thread? I think I'm missing somethng... Please post your question in a new thread, and provide more details about where you're trying that definition...

            Comment

            Working...
            X