Results 1 to 4 of 4

Thread: Loading Beans issues

  1. #1

    Default Loading Beans issues

    Hi All,
    I am using a custom class to load all my context files. Something like this
    Code:
    public class TestContainer {
        private static final ClassPathXmlApplicationContext context;
        private static final Logger logger = Logger.getLogger(TestContainer.class);
        private static String[] contextFiles = new String[]{
                "/spring/a-context.xml",
                "/spring/b-context.xml",
               
        };
       
    
        static {
            logger.info("Initializing TestContainer ");
    
          //some other code here to check whether file exist or not
            String[] configLocations = // assigned valid file values here.
    
            context = new ClassPathXmlApplicationContext(configLocations, false);
            context.refresh();
        }
    
        /**
         * Retrieve the Spring Context.
         *
         * @return A Spring Context
         */
        public static ApplicationContext getContext() {
            return context;
        }
        
        public static Object getJobService() {
            // job service is needed in many places, 
           		return TestContainer.getContext().getBean("jobService");
    	}
    }
    the getJobService() is used in many places in different applications. jobService bean takes time in initialization as it is populated from some external resource. The problem is when application A is trying to access getJobService() we are getting proper values but when accessed by application B it is giving new fresh object but actually we require it should retain the values what are already set by application A since it is singleton bean.

    Am I right or I am missing some thing or is there any better way of achieving same thing what I want to achieve.
    Last edited by Rajput.kamal; Jan 22nd, 2013 at 02:23 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Please use [ code][/code ] tags when posting code, that way it remains readable...

    The problem is when application A is trying to access getJobService() we are getting proper values but when accessed by application B it is giving new fresh object
    Ehrm... Why should App A. be capable of accessing beans of App B. ?! That is never going to work as a singleton is a singleton per ApplicationContext although in this case it is per classloader and App A. and App B are probably running in seperate JVM and as such don't share anything.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    I am doing this in spring based portlets which are in different war files each. I am having custom services which I have written in a single project along with TestContainer.java . When portlet A access
    Code:
    TestContainer.getContext().getBean("bean1");
    and bean1 internally calls TestContainer.getJobService();

    sample code for Bean1 is
    Code:
    public class Bean1 {
    
    public AccountDeatils getAccountDetails(int accountId) throws TimeoutException, JMSException,
                {
            
           JobService jobService = getJobService();
            return jobService.getAccountDetails(accountId);
        }
    
      private JobService getJobService() {
    		return (JobService) TestContainer.getJobService();
    	}
    }
    AccountDetails get populated through external JMSService.

    My use case is that both Portlet A and Portlet B are trying to access same service that is bean1.getAccountDetails(1) but both are showing different data. My question is whether both portlet are accessing same object or different object of AccountDetails. If they are accessing different Object then how to make them access same object.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    As mentioned both are different applications and as such share nothing...

    Next to that you shouldn't be constructing applicationcontexts yourself, spring should do that for you with the ContextLoaderListener. When using portlets you could try to make a bean globalSession scoped however not sure if this works with your current setup as the bean should be defined in a global context and not in some singleton thing you construct/load yourself. You should use dependency injection to inject the service instead of doing a lookup.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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