Results 1 to 5 of 5

Thread: Static "current" context

  1. #1
    Join Date
    Feb 2008
    Location
    Dublin - Ireland
    Posts
    102

    Default 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. #2
    Join Date
    Jan 2008
    Posts
    182

    Default

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

  3. #3
    Join Date
    Feb 2008
    Location
    Dublin - Ireland
    Posts
    102

    Default

    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.

  4. #4
    Join Date
    Jan 2008
    Posts
    182

    Default

    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...

  5. #5
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •