Announcement Announcement Module
No announcement yet.
How best to set parent context bean into child context bean in this situation? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How best to set parent context bean into child context bean in this situation?

    Does anyone know how I can ensure that (1) a bean defined in the parent webapp context is set on the controller bean in the child portlet context and that (2) the controller bean has been fully configured such that I could call a method on the bean defined in the parent webapp context from the controller in afterPropertiesSet()?

    I have a simple task scheduling and in-memory result caching library ( ) that allows the user to call methods on an instance of the class to schedule new tasks, then later I call methods to get the cached result of that class.

    I define the caching library config as a resource that lives in its jar as nerot/nerot.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <context:annotation-config />
        <bean name="nerotScheduleFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="waitForJobsToCompleteOnShutdown" value="false"/>
        <bean name="nerotStore" class=""/>
        <bean name="nerot" class="nerot.Nerot">
            <property name="scheduler" ref="nerotScheduleFactory"/>
            <property name="store" ref="nerotStore"/>
    Then in a portlet, I load that context into the root webapp context in web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        <display-name>My Portlet</display-name>
        <description>This is my portlet</description>
            <!-- the root context loaded when the webapp loads -->
    (Note: this is config before Pluto modification.)

    The caching library beans get loaded into the root webapp context when the webapp is instantiated.

    However, I want to call a method on the caching library bean that I need access to within the portlet's context. Preferably, I'd like to do this in such a way that the portlet's context has been loaded first (in order to get parameters set on the controller), and then call the instance of the bean in the root webapp context with those parameters.

    I was hoping to use:

    private Nerot nerot;
    in the controller class.

    My previous implementation used:
    // expecting feedUrl and intervalInMillis to have been set on the controller bean via Spring portlet context config
    public void afterPropertiesSet() throws Exception {
        nerot.scheduleRss(feedUrl, intervalInMillis);
    in the controller (in the child context) to call the method I wanted to call in the "nerot" bean in the parent context.

    However, I'm having to rely on these config parameters to be set and the "nerot" bean instance to be set on the child context controller bean, before calling the schedule method.

    Maybe it is somehow as easy as referencing the root context's bean "nerot" from the child portlet context's bean config. What is the best way of doing that?

    (Please note that I'm calling schedule via a method like this instead of just configuring Quartz in Spring, because in other places I need schedule to be called at runtime. I realize care would need to be taken with that, since it is spawning new threads, and I don't want it to spawn too many. The reason I'm not using traditional caching is to avoid issues that we had with the application in requests getting delayed when an RSS feed was down. A more scalable solution that would persist the feed would be better, but I wanted something simple to use. In at least a few ways, I'm replicating and hiding some of the functionality already in Quartz.)
    Last edited by garysweaver; Oct 21st, 2010, 03:04 PM.

  • #2
    Problem was in the tests not loading parent context configurations

    I was thinking that it wasn't able to access the parent context bean, but the issue was that I wasn't loading the parent context config files as part of the tests. Here is an example of a working test after the fix:

    package com.acme;
    import com.acme.MyController;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    @ContextConfiguration(locations = {
    public class MyPortletControllerTest extends AbstractDependencyInjectionSpringContextTests {
        public MyPortletController myPortletController;
        public void testContextLoadsProperly() {
    (Note: I'm now realizing that the goal was to call the method when the root context initialized, and I shouldn't be configuring that in the controller, because then it delays the controller load while it does the first schedule call, so I'll need to do the schedule call via another bean that I'll configure in the root context. Should work, but I'll bet there is a better solution. Edit: I implemented classes that call schedule methods as soon as the bean properties are all set in v3.4 of Nerot.)
    Last edited by garysweaver; Oct 22nd, 2010, 01:48 PM.


    • #3
      Thanks sharing. It's geat

      watch movies online


      • #4


        Glad to know this could be of use!