Spring调用bean之前关闭Spring ApplicationContext

时间:2014-10-30 17:23:06

标签: java eclipse spring maven

在我的Spring应用程序中,我注意到Spring(或Eclipse)的奇怪行为。它困惑了我。使用try / catch包围的ApplicationContext确保在finally块中完成后关闭。但是在Eclipse控制台中,我看到它在调用bean之前关闭了。

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();
    }
}

豆-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>

在Eclipse控制台输出中:

[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] ------------------------------------------------------------------------

正如你在Bean之前看到的那样初始化doClose方法,为什么?我认为它是Eclipse或maven插件错误......项目是用exec-maven-plugin构建的。

1 个答案:

答案 0 :(得分:2)

您的代码实际上正在运行:

System.out.println(launcher);行打印my.ioc.Launcher@2b7f535d

刚开始

INFO: Loading XML bean definitions from class path resource [beans-annot.xml]
my.ioc.Launcher@2b7f535d <--------
окт 30, 2014 8:52:56 PM …..

System.out.println("invoke bean");行打印invoke bean预期的方式..在哪里?

окт 30, 2014 8:52:56 PM ....ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support….root of context hierarchy
invoke bean <-------- 
[INFO] …

您的应用程序执行时间太短或太快,执行时,您会看到自己的应用程序的信息或输出一起与Spring启动/关闭信息。因此,两者都打印在一起,您的应用程序和弹簧(开始/关闭)过程,因此在这种情况下,终端/控制台给出了您所描述的印象。

尝试将System.out.println("invoke bean");括在Thread.sleep(5000)中,以某种方式延迟您的申请。

关于您的XML应用程序

  1. 对于每个.xsd删除版本,即:spring-beans-3.0.xsd to spring-beans.xsd。这是一个很好的做法包含版本
  2. 删除<context:annotation-config />是不必要的,因为您已宣布<context:component-scan base-package="my.ioc" />,请阅读以下内容:Difference between <context:annotation-config> vs <context:component-scan>了解详情。