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.