BeanFactory not initialized or already closed, crtnotinitialized

Source: Internet
Author: User

BeanFactory not initialized or already closed, crtnotinitialized

Today, I encountered a common error when writing a Spring program, but I have never encountered it before. Today I saw this error. After research, I solved it, shouldn't I make this mistake.

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please 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 'refresh' before accessing beans via the ApplicationContext    at 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 disabled. The cause of the error is very simple:

ApplicationContext ctx = new ClassPathXmlApplicationContext ();

No configuration file is specified. When Spring instantiates BeanFactory, the file named applicationContext. xml is found under classPath by default. However, you did not specify the file, so this error occurs.

This is the cause of the Error. Just write the configuration file name in brackets.

ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml ");

In this way, this error will not occur.

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.