bean生命周期基于JSR250

siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springioc
│   │               └── domain
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springioc
    │               └── domain
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

15 directories, 4 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springioc.domain;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;

public class User {

	private final Logger logger = Logger.getLogger(this.getClass());

	@PostConstruct
	public void init() {
		logger.info("initialized");
	}

	@PreDestroy
	public void destroy() {
		logger.info("destruct");
	}

}
<?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">
	<!--
		关于spring容器中bean的生命周期的管理,个人喜欢使用JSR250的规范实现来管理。

		使用容器管理bean的生命周期
		主要是bean的初始化和销毁时的管理工作,有4种途径来管理.

		1)在配置文件的全局中进行管理
		在<beans>标签内设置属性default-init-method和default-destroy-method;
		<beans default-init-method="" default-destroy-method=""></beans>
		设置的属性值,主要取决于该容器中注册的bean是否具有同名的方法签名;

		2)单独设置bean的生命周期;
		在<bean>标签内设置属性值init-method和destroy-method;
		<bean init-method="" destroy-method=""></bean>
		属性值取决于bean类是否存在匹配的方法签名;


		3)使用JSR250的注解@PostConstructor和@PreDestroy
		1.8的jdk内嵌的有JSR 250的jar包,不需要额外导入;
		使用方式:在bean类的初始化和销毁方法上标记对应的注解即可;

		4)实现约定的接口
		org.springframework.beans.factory.InitializingBean
		org.springframework.beans.factory.DisposableBean
		对需要管理的bean实现上述两个接口,重写继承的方法即可.
		耦合度较高,一般不建议使用;

		优先级说明:
		当同一bean定义了多种生命周期管理的设置,其先后的执行顺序如下:
		1)JSR 250注解的方式优先执行;
		2)实现约定接口的方式
		3)xmlConfig风格中的局部配置或全局配置(其中局部的配置会覆盖全局的配置)
	-->
	<bean class="ocn.site.springioc.domain.User"></bean>

</beans>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired User user;

	@Test
	public void run() throws Exception {
		logger.info(user);
	}

}
19-09-08 16:20:10 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-08 16:20:10 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4e04a765, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@783e6358, org.springframework.test.context.support.DirtiesContextTestExecutionListener@17550481]
19-09-08 16:20:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 16:20:10 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 16:20:10 CST 2019]; root of context hierarchy
19-09-08 16:20:10 ocn.site.springioc.domain.User  =====>>> initialized
19-09-08 16:20:10 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@2145433b
19-09-08 16:20:10 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 16:20:10 CST 2019]; root of context hierarchy
19-09-08 16:20:10 ocn.site.springioc.domain.User  =====>>> destruct
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值