Beanfactory not initialized or already closed-call ' refresh ' before accessing beans solution

Source: Internet
Author: User
Tags log4j

Today in writing spring program encountered a very common error, and I did not seem to have encountered before, today only to see this error, after the study to solve the error, it is really wrong to make this mistake.

Log4j:warn No Appenders could is found for logger (org.springframework.core.env.StandardEnvironment). Log4j:warn Initialize the log4j system Properly.log4j:WARN see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info . Exception in thread "main" java.lang.IllegalStateException: beanfactory not initialized or already closed-call ' REFR Esh ' before accessing beans via the ApplicationContextat    Org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory ( abstractrefreshableapplicationcontext.java:172) at    Org.springframework.context.support.AbstractApplicationContext.getBean (abstractapplicationcontext.java:1097) At    Com.pb.demo.Test.main (test.java:12)

Beanfactory is not instantiated or has been closed, in fact, the cause of the error is very simple, in writing:

ApplicationContext CTX = new Classpathxmlapplicationcontext ();

, when the configuration file is not specified, spring instantiates beanfactory by default to Classpath to find the file named Applicationcontext.xml, but you do not specify it, so this error occurs.

This is the reason for the error, the name of the file in parentheses to write the line.

ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");

In that case, there would be no such mistake.

Beanfactory not initialized or already closed-call ' refresh ' before accessing beans solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.