Results 1 to 3 of 3

Thread: Beginner Question - Loading bean when xml is not on classpath

  1. #1
    Join Date
    May 2012
    Posts
    11

    Default Beginner Question - Loading bean when xml is not on classpath

    I have a complete beginner question, and it isnt tied into anything that I particularly need to do. I am just trying to create working examples so I can learn as I go.

    In an example application I am working with none of the xml files are declared on the classpath, they are in WEB-INF/spring directory (I dont think this is on the classpath). How would I load a bean from a file in this directory using the ApplicationContext? I have tried using the web application context like below:

    Code:
       ApplicationContext ctx = new XmlWebApplicationContext();
       Properties loadedFromCtx = (Properties)ctx.getBean("properties");
    This gives me an error: org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    Any help understanding would be welcomed as I am really new to Spring.

    Thanks

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello

    In an example application I am working with none of the xml files are declared on the classpath, they are in WEB-INF/spring directory (I dont think this is on the classpath)
    What is the problem if you move such files within WEB-INF/somedirectory? I think you should do that and with your web.xml do reference to such xml files

    Read the Spring Reference documentation
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    May 2012
    Posts
    11

    Default

    Quote Originally Posted by dr_pompeii View Post
    Hello


    What is the problem if you move such files within WEB-INF/somedirectory? I think you should do that and with your web.xml do reference to such xml files

    Read the Spring Reference documentation
    I am learning Spring only because my workplace has decided to we will start using it. So we are slowly getting used to it.

    I dont have the ability to change the project structure and the live projects I viewed under SVN also used this structure so I am assuming that is just the way things are going to be done.

    Manuel I have this in my web.xml which does reference the xml file. Is this what you were referring to?

    Code:
    <servlet>
    		<servlet-name>appServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    If I wanted to load a bean using java code just as a demonstration how would I do this?
    Last edited by paul_; Jul 10th, 2012 at 02:41 AM.

Posting Permissions

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