Results 1 to 6 of 6

Thread: Should Contexts themselves be configured by DI?

  1. #1
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default Re: Should Contexts themselves be configured by DI?

    Quote 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,

  3. #3
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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,

  4. #4
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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

  5. #5

    Default

    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

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Replies: 6
    Last Post: Mar 25th, 2009, 01:43 AM
  2. Replies: 11
    Last Post: Dec 3rd, 2007, 02:30 AM
  3. Beandoc crashing (on its samples!)
    By aaime in forum Container
    Replies: 17
    Last Post: Oct 7th, 2005, 07:21 AM
  4. Retrieving Spring configured Beans
    By chenrici in forum Container
    Replies: 5
    Last Post: Jul 15th, 2005, 02:27 PM
  5. Replies: 2
    Last Post: Mar 23rd, 2005, 06:59 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
  •