【Spring】依赖注⼊加载顺序
⼀、Spring依赖注⼊depents-on参数
depents-on是指指定Bean初始化及销毁时的顺序,使⽤depends-on属性指定的是Bean要先初始化完毕后才初始化当前Bean,由于只有Singleton Bean能被Spring
管理销毁,所以当指定的Bean都是singleton时,使⽤depends-on属性指定的Bean要在指定的Bean之后销毁
1、需要实体类以及配置⽂件测试实例⽇志信息
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();
spring framework37        }
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:=======写资源");
}
// 初始化⽅法
public void init() throws IOException {
System.out.println("DependentBean:=======初始化");
}
// 销毁⽅法
public void destroy() throws IOException {
System.out.println("DependentBean:=======销毁");
// 在销毁之前需要往⽂件中写销毁内容
}
public void setResourceBean(ResourceBean resourceBean) {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:context="/schema/context"
xsi:schemaLocation="
/schema/beans        /schema/beans/spring-beans-3.0.xsd
/schema/context                /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://"></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 t.support.ClassPathXmlApplicationContext;
/*2017-07-13 10:28:39,793 DEBUG main nv.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemProperties] Proper 2017-07-13 10:28:39,884 DEBUG main nv.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemEnvironment] Prope 2017-07-13 10:28:39,885 DEBUG main nv.AbstractEnvironment.<init>(AbstractEnvironment.java:126) Initialized StandardEnvironment with Property 2017-07-13 10:28:39,918 INFO  main t.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:510) Refreshing org.sp 2017-07-13 10:28:40,080 DEBUG main nv.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemProperties] Property 2017-07-13 10:28:40,081 DEBUG main nv.MutablePropertySources.addLast(MutablePropertySources.java:107) Adding [systemEnvironment] Prope 2017-07-13 10:28:40,086 DEBUG main nv.AbstractEnvironment.<init>(AbstractEnvironment.java:126) Initialized StandardEnvironment with Property 2017-07-13 10:28:40,196 INFO  main org.springframework.l.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315) Loading XML 2017-07-13 10:28:40,201 DEBUG main org.springframework.l.DefaultD
ocumentLoader.loadDocument(DefaultDocumentLoader.java:72) Using JAXP provider 2017-07-13 10:28:40,382 DEBUG main org.springframework.SchemaMappings(PluggableSchemaResolver.java:140) Loading 2017-07-13 10:28:40,394 DEBUG main org.springframework.SchemaMappings(PluggableSchemaResolver.java:146) Loaded 2017-07-13 10:28:40,398 DEBUG main org.springframework.solveEntity(PluggableSchemaResolver.java:118) Found XML sch 2017-07-13 10:28:40,633 DEBUG main org.springframework.isterBeanDefinitions(DefaultBeanDefinitionDocumen 2017-07-13 10:28:40,698 DEBUG main org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216 2017-07-13 10:28:40,700 DEBUG main t.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540) Bean f 2017-07-13 10:28:40,763 DEBUG main t.support.AbstractApplicationContext.initMessageSource(AbstractApplicationContext.java:807) Unable to lo 2017-07-13 10:28:40,817 DEBUG main t.support.AbstractApplicationContext.initApplicationEventMulticaster(AbstractApplicationContext.java:831)
2017-07-13 10:28:40,818 INFO  main org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:603) P 2017-07-13 10:28:40,820 DEBUG main org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:215) Creati 2017-07-13 10:28:40,821 DEBUG main org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.ja 2017-07-13 10:28:40,844 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactor 2017-07-13 10:28:40,936 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableB ResourceBean:=============初始化
ResourceBean:==============加载资源,执⾏⼀些与操作
2017-07-13 10:28:40,966 DEBUG main org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.ja 2017-07-13 10:28:40,967 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
Returning cached instance 2017-07-13 10:28:40,972 DEBUG main org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:215) Creati 2017-07-13 10:28:40,972 DEBUG main org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.ja 2017-07-13 10:28:40,974 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactor 2017-07-13 10:28:40,977 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance 2017-07-13 10:28:40,980 DEBUG main org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableB DependentBean:=======初始化
2017-07-13 10:28:40,982 DEBUG main org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.ja 2017-07-13 10:28:40,984 DEBUG main t.support.AbstractApplicationContext.initLifecycleProcessor(AbstractApplicationContext.java:858) Unable to 2017-07-13 10:28:40,985 DEBUG main org.springframework.beans.factory.support.Abstract
BeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance 2017-07-13 10:28:40,995 DEBUG main Property(PropertySourcesPropertyResolver.java:81) Searching for 2017-07-13 10:28:41,023 DEBUG main Property(PropertySourcesPropertyResolver.java:81) Searching for 2017-07-13 10:28:41,028 DEBUG main Property(PropertySourcesPropertyResolver.java:103) Could not fin 2017-07-13 10:28:41,066 DEBUG main org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached instance DependentBean:=======写资源
2017-07-13 10:28:41,114 INFO  Thread-0 t.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1042) Closing org.springf 2017-07-13 10:28:41,116 DEBUG Thread-0 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) Returning cached inst 2017-07-13 10:28:41,116 INFO  Thread-0 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:444
2017-07-13 10:28:41,116 DEBUG Thread-0 org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:30 DependentBean:=======销毁
2017-07-13 10:28:41,117 DEBUG Thread-0 org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:30 ResourceBean:============销毁
ResourceBean:===========释放资源,执⾏⼀些清理操作
*/
public class MoreDependencyInjectTest {
@Test
public void testDependOn() throws IOException{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("l");
//注册销毁回调否则定义的销毁⽅法不执⾏
DependentBean dependent = Bean("dependentBean", DependentBean.class);
dependent.write("测试写⼊数据");
} }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。