Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Avoid loading of Spring Configuration files multiple times

  1. #1
    Join Date
    Dec 2007
    Location
    India
    Posts
    9

    Default Avoid loading of Spring Configuration files multiple times

    Hi,
    I am using spring jms for reading message from IBM MQ series.
    That is working fine, the problem is, it load spring configuration file each time.
    I am using : ClassPathXmlApplicationContext to load xml file.
    But now I wanted that spring xml file should be loaded autoamtically as applcation loaded.How this is possbile? And if possible then how can I get bean.
    Thanks.

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

    Default

    Post some code, what is it you do? Your JMS Listener creates a new ApplicationContext each time? What type of application do you have?
    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
    Join Date
    Dec 2007
    Location
    India
    Posts
    9

    Default Code

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
    new String[] { "ewfspringapp.xml" });
    LogManager
    .logGeneralInfo("Class path loaded and readingspringapreal.xml for Sender");
    JMSSender jmsSender = (JMSSender) appContext.getBean("jmsSender");

    QueueConnectionFactory factory = (QueueConnectionFactory) jmsSender
    .getJmsTemplate102().getConnectionFactory();
    javax.jms.QueueConnection qConnection = (javax.jms.QueueConnection) factory
    .createQueueConnection();

    javax.jms.Session sess = (QueueSession) qConnection
    .createQueueSession(false, Session.AUTO_ACKNOWLEDGE);


    This is the part of code that I am using. But the problem is that each time it loads ewfspringapp.xml.
    I want to load this only once, when application loads. For this I am trying to make servlet that will call :
    Resource res = new FileSystemResource("beans.xml");
    XmlBeanFactory factory = new XmlBeanFactory(res);


    And for the further use i will use factory object.
    Let me know is approach possbile. What should I have to make changes for this?

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

    Default

    Please use [ code][/code ] tags when posting code...

    Why so complicated why don't you simply use the DispatcherServlet or ContextLoaderListener to load your context? AI'm also not sure what you want with JMS but why not use the Spring abstraction for that also?!
    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

  5. #5
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Code:
    static ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("ewfspringapp.xml");
    would suffice.

    Regards,
    Oleksandr

  6. #6
    Join Date
    May 2007
    Location
    Netherlands
    Posts
    614

    Default

    static would work, but I think as marten suggested that you should not have a reference to the context. If you're using dependency injection correctly you should virtually never have to call getBean().

  7. #7
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by iwein View Post
    static would work, but I think as marten suggested that you should not have a reference to the context. If you're using dependency injection correctly you should virtually never have to call getBean().
    That's true. For web-application using context loader listener is superior choice.
    And pulling bean from context is typically not the best solution (there are few exception though).

  8. #8
    Join Date
    Dec 2007
    Location
    India
    Posts
    9

    Default

    Hi, I made following changes:
    Creating class SpringApplicationContext

    Code:
    public class SpringApplicationContext implements ApplicationContextAware {
    
    	  private static ApplicationContext CONTEXT;
    
    	   public void setApplicationContext(ApplicationContext context)   throws BeansException {
    	    CONTEXT = context;
    	  }
    
    	   public static Object getBean(String beanName) {
    	    return CONTEXT.getBean(beanName);
    	  }
    	}
    And use SpringApplicationContext to create bean:

    Code:
    JMSSender jmsSender = (JMSSender) SpringApplicationContext.getBean("jmsSender");
    Then put ewfspringappl-servlet.xml in WEB-INF and add the following in that:

    Code:
    <bean id="springApplicationContext" class="com.springexample.client.SpringApplicationContext"/>
    And made DispatcherServlet named ewfspringappl in web.xml.

    When running the application I got followings errors::
    Context initialization failed
    The following exception was logged org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [com.springexample.client.SpringApplicationContext] for bean with name 'springApplicationContext' defined in ServletContext resource [/WEB-INF/ewfspringappl-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.springexample.client.SpringApplicationContext
    Caused by: java.lang.ClassNotFoundException: com.springexample.client.SpringApplicationContext



    How can I remove these errors.

    Thanks.

  9. #9
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Seems that class is not on your classpath (likely, you have mistyped class name in the bean definition (or in class itself ).
    Quote Originally Posted by piscean17 View Post
    Hi, I made following changes:
    Creating class SpringApplicationContext

    Code:
    public class SpringApplicationContext implements ApplicationContextAware {
    
    	  private static ApplicationContext CONTEXT;
    
    	   public void setApplicationContext(ApplicationContext context)   throws BeansException {
    	    CONTEXT = context;
    	  }
    
    	   public static Object getBean(String beanName) {
    	    return CONTEXT.getBean(beanName);
    	  }
    	}
    And use SpringApplicationContext to create bean:

    Code:
    JMSSender jmsSender = (JMSSender) SpringApplicationContext.getBean("jmsSender");
    Then put ewfspringappl-servlet.xml in WEB-INF and add the following in that:

    Code:
    <bean id="springApplicationContext" class="com.springexample.client.SpringApplicationContext"/>
    And made DispatcherServlet named ewfspringappl in web.xml.

    When running the application I got followings errors::
    Context initialization failed
    The following exception was logged org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [com.springexample.client.SpringApplicationContext] for bean with name 'springApplicationContext' defined in ServletContext resource [/WEB-INF/ewfspringappl-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.springexample.client.SpringApplicationContext
    Caused by: java.lang.ClassNotFoundException: com.springexample.client.SpringApplicationContext



    How can I remove these errors.

    Thanks.

  10. #10
    Join Date
    Dec 2007
    Location
    India
    Posts
    9

    Default

    Hi,

    The class name is right.But I have confusion.

    Code:
    <bean id="springApplicationContext" class="com.springexample.client.SpringApplicationContext"/>
    Where should I put this code?

    Right now I am putting this in spring.xml.Should I have to put it in some other xml and load it as context - param?

    Thanks.

Posting Permissions

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