Announcement Announcement Module
Collapse
No announcement yet.
Static "current" context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Static "current" context

    One of the things I do now is something like, having

    /slayer/request/requestXPTO
    /slayer/response/somekey

    and after I get the "path" info for those urls, invoke something like on a "resource" object
    Code:
    		if(channel.equals("request")){
    			responseBean = doRequest(rb, synchronous);
    		}
    		
    		if(channel.equals("response")){
    			responseBean = doResponse(rb, synchronous);
    		}
    However I don't know how to have access to the channels currently defined on the context. Now I'm "injecting" then on my "resource" object when I instanciate it, like

    Code:
    context.start();
    		
    resource = (Resource) context.getBean("resource");
    		
    resource.setAvailableChannels(context.getBeanNamesForType(MessageChannel.class));
    However, I should be able to access the "current context" in a static way to avoid this.

    Is this possible?

    Cheers.

  • #2
    Do you mean something like
    Code:
            ChannelRegistry channelRegistry = (ChannelRegistry) AppContext.getContext().getBean(
                MessageBusParser.MESSAGE_BUS_BEAN_NAME);
            MessageChannel statusRequestChannel = channelRegistry.lookupChannel("RequestChannel");
    ?

    Comment


    • #3
      Something like that, yes, but where do you get that AppContext? I don't find anything in Spring core like that, and ApplicationContext doesen't have any static methods.

      Comment


      • #4
        Oh sorry, we make one for most of our projects, for example:

        Code:
        public class AppContext {
            private static AbstractApplicationContext context = null;
        
            public static AbstractApplicationContext getContext() {
                if (context == null) {
                    context = new ClassPathXmlApplicationContext(
                        "/main/webapp/WEB-INF/applicationContext-integration.xml");
                    context.start();
                }
                return context;
            }
        
        }
        Note that this one just loads a NEW context from the particular context file, if you want something like the context of the webapp you need to do something different...

        Comment


        • #5
          Originally posted by whyBish View Post
          if you want something like the context of the webapp you need to do something different...
          E.g. something like this.

          Creating a new context each time might prove to be quite expensive.

          Regards,
          Andreas

          Comment

          Working...
          X