In my Spring app I noticed strange behavior of Spring (or Eclipse). It confused me. ApplicationContext surrounded with try/catch to be sure is closed after done in finally block. But in Eclipse console I saw that it closed before bean invoked.
public class Main {
public static void main(String[] args) {
ApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext(new String[] { "beans-annot.xml" });
Launcher launcher = (Launcher) context.getBean("launcher");
System.out.println(launcher);
launcher.invokeBean();
} catch (BeansException e) {
e.printStackTrace();
} finally {
if(context != null)
((AbstractApplicationContext) context).close();
}
}
}
@Component
public class Bean {
public void invoke(){
System.out.println("invoke bean");
}
}
@Component
public class Launcher {
@Autowired
public Bean bean;
//setter
public void invokeBean(){
bean.invoke();
}
}
beans-annot.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="my.ioc" />
<context:annotation-config />
</beans>
In Eclipse console output:
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ IoC ---
окт 30, 2014 8:52:56 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6e4e4adb: startup date [Thu Oct 30 20:52:56 FET 2014]; root of context hierarchy
окт 30, 2014 8:52:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans-annot.xml]
my.ioc.Launcher@2b7f535d
окт 30, 2014 8:52:56 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6e4e4adb: startup date [Thu Oct 30 20:52:56 FET 2014]; root of context hierarchy
invoke bean
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
As you can see doClose method init before Bean, why? I think that it's Eclipse or maven plugin error... Project was build with exec-maven-plugin.