public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); }
}
观察控制台输出
正确输出:
view plaincopy to clipboardprint? 2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]; startup date [Wed Dec 30 09:03:28 CST 2009]; root of context hierarchy 2009-12-30 9:03:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128 2009-12-30 9:03:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128: defining beans []; root of factory hierarchy 2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]; startup date [Wed Dec 30 09:03:28 CST 2009]; root of context hierarchy 2009-12-30 9:03:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128 2009-12-30 9:03:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128: defining beans []; root of factory hierarchy
如果你没有引用 commons-logging.jar ,会输出如下错误提示
view plaincopy to clipboardprint? Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:145) at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:84) at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59) at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:58) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at my.sh.spring.springtest.main(springtest.java:9)