depents-on是指指定Bean初始化及銷燬時的順序,使用depends-on屬性指定的是Bean要先初始化完畢後才初始化當前Bean,因爲只有Singleton Bean能被Spring管理銷燬,因此當指定的Bean都是singleton時,使用depends-on屬性指定的Bean要在指定的Bean以後銷燬java
一、須要實體類以及配置文件測試實例 日誌信息
1 package com.slp.spring.learn.helloworld.di; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 /** 8 * ResourceBean從配置文件中配置文件位置,而後定義初始化方法init中打開指定的文件,而後獲取文件流;最後定義銷燬方法destroy用於在應用程序關閉時調用該方法關閉掉文件流 9 * @author sangliping 10 * 11 */ 12 public class ResourceBean { 13 14 private FileOutputStream fos; 15 private File file; 16 public void init(){ 17 System.out.println("ResourceBean:=============初始化"); 18 //加載資源,在此只是演示 19 System.out.println("ResourceBean:==============加載資源,執行一些與操做"); 20 try { 21 fos=new FileOutputStream(file); 22 } catch (FileNotFoundException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27 28 public void destory(){ 29 System.out.println("ResourceBean:============銷燬"); 30 //釋放資源 31 System.out.println("ResourceBean:===========釋放資源,執行一些清理操做"); 32 try { 33 fos.close(); 34 } catch (IOException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 public FileOutputStream getFos() { 41 return fos; 42 } 43 44 public void setFos(FileOutputStream fos) { 45 this.fos = fos; 46 } 47 48 public File getFile() { 49 return file; 50 } 51 52 public void setFile(File file) { 53 this.file = file; 54 } 55 56 }package com.slp.spring.learn.helloworld.di; import java.io.IOException; /** * DependentBean中會注入ResourceBean,並從ResourceBean中獲取文件流寫入內容;定義初始化方法init用來定義一些初始化操做並向文件中輸出文件頭信息;最後定義銷燬方法用於在關閉應用程序時想文件中輸出文件尾信息。 * @author sangliping * */ public class DependentBean { ResourceBean resourceBean; public void write(String ss) throws IOException { System.out.println("DependentBean:=======寫資源"); resourceBean.getFos().write(ss.getBytes()); } // 初始化方法 public void init() throws IOException { System.out.println("DependentBean:=======初始化"); resourceBean.getFos().write("DependentBean:=======初始化=====".getBytes()); } // 銷燬方法 public void destroy() throws IOException { System.out.println("DependentBean:=======銷燬"); // 在銷燬以前須要往文件中寫銷燬內容 resourceBean.getFos().write("DependentBean:=======銷燬=====".getBytes()); } public void setResourceBean(ResourceBean resourceBean) { this.resourceBean = resourceBean; } }<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="resourceBean" class="com.slp.spring.learn.helloworld.di.ResourceBean" init-method="init" destroy-method="destory"> <property name="file" value="D://test.txt"></property><!-- Spring容器能夠自動把字符串轉換爲java.io.File --> </bean> <bean id="dependentBean" class="com.slp.spring.learn.helloworld.di.DependentBean" init-method="init" destroy-method="destroy" depends-on="resourceBean"> <property name="resourceBean" ref="resourceBean"></property> </bean> </beans>package com.slp.spring.learn.helloworld.di; import java.io.IOException; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; /*2017-07-13 10:28:39,793 DEBUG main org.springframework.core.env.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemProperties] PropertySource with lowest search precedence 2017-07-13 10:28:39,884 DEBUG main org.springframework.core.env.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemEnvironment] PropertySource with lowest search precedence 2017-07-13 10:28:39,885 DEBUG main org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:126) Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment] 2017-07-13 10:28:39,918 INFO main org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:510) Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c30a502: startup date [Thu Jul 13 10:28:39 CST 2017]; root of context hierarchy 2017-07-13 10:28:40,080 DEBUG main org.springframework.core.env.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemProperties] PropertySource with lowest search precedence 2017-07-13 10:28:40,081 DEBUG main org.springframework.core.env.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemEnvironment] PropertySource with lowest search precedence 2017-07-13 10:28:40,086 DEBUG main org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:126) Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment] 2017-07-13 10:28:40,196 INFO main org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315) Loading XML bean definitions from class path resource [di/depent-on.xml] 2017-07-13 10:28:40,201 DEBUG main org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:72) Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl] 2017-07-13 10:28:40,382 DEBUG main org.springframework.beans.factory.xml.PluggableSchemaResolver.getSchemaMappings(PluggableSchemaResolver.java:140) Loading schema mappings from [META-INF/spring.schemas] 2017-07-13 10:28:40,394 DEBUG main org.springframework.beans.factory.xml.PluggableSchemaResolver.getSchemaMappings(PluggableSchemaResolver.java:146) Loaded schema mappings: {http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/jms/spring-jms-3.0.xsd=org/springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm.xsd=org/springframework/oxm/config/spring-oxm-3.2.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd=org/springframework/jdbc/config/spring-jdbc-3.1.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/tx/spring-tx-3.2.xsd=org/springframework/transaction/config/spring-tx-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd=org/springframework/oxm/config/spring-oxm-3.2.xsd, http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/jms/spring-jms-3.2.xsd=org/springframework/jms/config/spring-jms-3.2.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx-3.1.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org/springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd=org/springframework/oxm/config/spring-oxm-3.1.xsd, http://www.springframework.org/schema/jms/spring-jms.xsd=org/springframework/jms/config/spring-jms-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd, http://www.springframework.org/schema/jms/spring-jms-3.1.xsd=org/springframework/jms/config/spring-jms-3.1.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc.xsd=org/springframework/jdbc/config/spring-jdbc-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd=org/springframework/jdbc/config/spring-jdbc-3.2.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd} 2017-07-13 10:28:40,398 DEBUG main org.springframework.beans.factory.xml.PluggableSchemaResolver.resolveEntity(PluggableSchemaResolver.java:118) Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd 2017-07-13 10:28:40,633 DEBUG main org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:106) Loading bean definitions 2017-07-13 10:28:40,698 DEBUG main org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216) Loaded 2 bean definitions from location pattern [di/depent-on.xml] 2017-07-13 10:28:40,700 DEBUG main org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540) Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@7c30a502: org.springframework.beans.factory.support.DefaultListableBeanFactory@1e6d1014: defining beans [resourceBean,dependentBean]; root of factory hierarchy 2017-07-13 10:28:40,763 DEBUG main org.springframework.context.support.AbstractApplicationContext.initMessageSource(AbstractApplicationContext.java:807) Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@7a187f14] 2017-07-13 10:28:40,817 DEBUG main org.springframework.context.support.AbstractApplicationContext.initApplicationEventMulticaster(AbstractApplicationContext.java:831) Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@27808f31] 2017-07-13 10:28:40,818 INFO main org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:603) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e6d1014: defining beans [resourceBean,dependentBean]; root of factory hierarchy 2017-07-13 10:28:40,820 DEBUG main org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:215) Creating shared instance of singleton bean 'resourceBean' 2017-07-13 10:28:40,821 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:432) Creating instance of bean 'resourceBean' 2017-07-13 10:28:40,844 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:506) Eagerly caching bean 'resourceBean' to allow for resolving potential circular references 2017-07-13 10:28:40,936 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1612) Invoking init method 'init' on bean with name 'resourceBean' ResourceBean:=============初始化 ResourceBean:==============加載資源,執行一些與操做 2017-07-13 10:28:40,966 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:460) Finished creating instance of bean 'resourceBean' 2017-07-13 10:28:40,967 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance of singleton bean 'resourceBean' 2017-07-13 10:28:40,972 DEBUG main org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:215) Creating shared instance of singleton bean 'dependentBean' 2017-07-13 10:28:40,972 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:432) Creating instance of bean 'dependentBean' 2017-07-13 10:28:40,974 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:506) Eagerly caching bean 'dependentBean' to allow for resolving potential circular references 2017-07-13 10:28:40,977 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance of singleton bean 'resourceBean' 2017-07-13 10:28:40,980 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1612) Invoking init method 'init' on bean with name 'dependentBean' DependentBean:=======初始化 2017-07-13 10:28:40,982 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:460) Finished creating instance of bean 'dependentBean' 2017-07-13 10:28:40,984 DEBUG main org.springframework.context.support.AbstractApplicationContext.initLifecycleProcessor(AbstractApplicationContext.java:858) Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@b3d7190] 2017-07-13 10:28:40,985 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance of singleton bean 'lifecycleProcessor' 2017-07-13 10:28:40,995 DEBUG main org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:81) Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties] 2017-07-13 10:28:41,023 DEBUG main org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:81) Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment] 2017-07-13 10:28:41,028 DEBUG main org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:103) Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null] 2017-07-13 10:28:41,066 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance of singleton bean 'dependentBean' DependentBean:=======寫資源 2017-07-13 10:28:41,114 INFO Thread-0 org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1042) Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c30a502: startup date [Thu Jul 13 10:28:39 CST 2017]; root of context hierarchy 2017-07-13 10:28:41,116 DEBUG Thread-0 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance of singleton bean 'lifecycleProcessor' 2017-07-13 10:28:41,116 INFO Thread-0 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:444) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e6d1014: defining beans [resourceBean,dependentBean]; root of factory hierarchy 2017-07-13 10:28:41,116 DEBUG Thread-0 org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:303) Invoking destroy method 'destroy' on bean with name 'dependentBean' DependentBean:=======銷燬 2017-07-13 10:28:41,117 DEBUG Thread-0 org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:303) Invoking destroy method 'destory' on bean with name 'resourceBean' ResourceBean:============銷燬 ResourceBean:===========釋放資源,執行一些清理操做 */ public class MoreDependencyInjectTest { @Test public void testDependOn() throws IOException{ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("di/depent-on.xml"); //註冊銷燬回調 不然定義的銷燬方法不執行 context.registerShutdownHook(); DependentBean dependent = context.getBean("dependentBean", DependentBean.class); dependent.write("測試寫入數據"); } }