Spring中Bean的初始化与销毁(基于Spring4.x)

  1. 通过在bean中设置init-method和destroy-method

    配置bean

    spring-lifecycle.xml

    1
    2
    3
    4
    5
    6
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanLifeCycle" class="com.sh.imcdemo.services.impl.BeanLifeCycle" init-method="start" destroy-method="stop"></bean>
    </beans>

    com.sh.imcdemo.services.impl 实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.sh.imcdemo.services.impl;
/**
* Created by Mr SJL on 2016/11/26.
*
* @Author Junlan Shuai
*/
public class BeanLifeCycle
{
public void start()
{
System.out.println("Bean start.");
}
public void stop()
{
System.out.println("Bean stop.");
}
}
  1. 通过实现InitializingBean和DisposableBean接口

    配置bean

    spring-lifecycle.xml

    1
    2
    3
    4
    5
    6
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanLifeCycle1" class="com.sh.imcdemo.services.impl.BeanLifeCycle"></bean>
    </beans>

    com.sh.imcdemo.services.impl实现类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.sh.imcdemo.services.impl;

    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;

    /**
    * Created by Mr SJL on 2016/11/26.
    *
    * @Author Junlan Shuai
    */
    public class BeanLifeCycle implements InitializingBean, DisposableBean
    {

    public void destroy() throws Exception
    {
    System.out.println("Bean destory.");
    }

    public void afterPropertiesSet() throws Exception
    {
    System.out.println("Bean afterPropertiesSet.");

    }
    }
  2. 通过设置default-destroy-method和default-init-method

    对于同一配置文件下的所有的Bean都会使用该默认的初始化和销毁方法(但有特殊情况,见本篇总结部分)

    1
    2
    3
    4
    5
    6
    7
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-destroy-method="defaultDestroy" default-init-method="defaultInit">
    <bean id="beanLifeCycle" class="com.sh.imcdemo.services.impl.BeanLifeCycle"></bean>
    </beans>

    com.sh.imcdemo.services.impl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package com.sh.imcdemo.services.impl;

    /**
    * Created by Mr SJL on 2016/11/26.
    *
    * @Author Junlan Shuai
    */
    public class BeanLifeCycle
    {
    public void defaultInit()
    {
    System.out.println("Bean defaultInit.");
    }
    public void defaultDestroy()
    {
    System.out.println("Bean defaultDestory");
    }
    }
  3. 总结

    • 当三种方式同时使用时,我们会发现,第三种方式被覆盖了,另外两种方式的输出先后顺序是:先是2再是1。
    • 当使用第3种方式时,实现类中不一定非要实现该默认方法,如果没有该方法,则没有处理。
    • 当第2种和第3中方式同时使用时,默认方法却没有被覆盖,两者都会输出,但是第1种和第3种同时使用时,默认方法却被覆盖了。(???)

附录

测试基类 com.sh.imcdemo.unitTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.sh.imcdemo.unitTest;
import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Mr SJL on 2016/11/26.
*
* @Author Junlan Shuai
*/
public class UnitTestBase
{
private ClassPathXmlApplicationContext context;

private String springXmlpath;
public UnitTestBase()
{

}
public UnitTestBase(String springXmlpath)
{
this.springXmlpath = springXmlpath;
}
@Before
public void before()
{
if (StringUtils.isEmpty(springXmlpath))
{
springXmlpath = "classpath*:spring-*.xml";
}
try
{
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
}
catch (BeansException e)
{
e.printStackTrace();
}

}
@After
public void after()
{
context.destroy();
}

protected <T extends Object> T getBean(String beanId)
{
return (T)context.getBean(beanId);
}
protected <T extends Object> T getBean(Class<T> clas)
{
return context.getBean(clas);
}

}

测试类com.sh.imcdemo.unitTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.sh.imcdemo.unitTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
* Created by Mr SJL on 2016/11/26.
*
* @Author Junlan Shuai
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class App3 extends UnitTestBase
{
public App3()
{
super("classpath:spring-lifecycle.xml");
}
@Test
public void test1()
{
super.getBean("beanLifeCycle");
}

@Test
public void test2()
{
super.getBean("beanLifeCycle1");
}

}

依赖包pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<spring.version>4.3.2.RELEASE</spring.version>
<junit.version>4.11</junit.version>


<!-- Spring依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- 单元测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×