`
cywhoyi
  • 浏览: 421185 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring Bean Lifecycle Control

 
阅读更多

本文目的是通过示例告诉大家sprng bean生命周期的控制。

主要通过两个接口InitializingBean、DisposableBea,验证bean在容器中初始化以及销毁时候的触发事件。

当然也可以通过配置xml的文件,init-method、destroy-method等同于上述两个接口

1.InitializingBean and DisposableBean interfaces

 

package com.javacodegeeks.snippets.enterprise.services;

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

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

public class Employee implements InitializingBean, DisposableBean {

	private Long id;

	private String position;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	@Override
	public String toString() {
		return "id " + id + " and position " + position;
	}

	@PostConstruct
	public void initIt() throws Exception {
		System.out.println("Init method after properties are set : " + id + " "
				+ position);
	}

	@PreDestroy
	public void cleanUp() throws Exception {
		System.out.println("Spring Clean Up! Employee is cleaned up now.");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("I am in destroy... ");

	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("I am in afterPropertiesSet... ");

	}
}

 2.Use of init-method and destroy-method attributes in bean definition

 

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
	
		<bean id="employeeBean" class="com.javacodegeeks.snippets.enterprise.services.Employee" init-method="initIt" destroy-method="cleanUp">
		<property name="id" value="123"/>
		<property name="position" value="marketing"/>
	</bean>

</beans>

 3.@PostConstruct and @PreDestroy annotations

 

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
	
	<context:annotation-config />
		<bean id="employeeBean" class="com.javacodegeeks.snippets.enterprise.services.Employee">
		<property name="id" value="123"/>
		<property name="position" value="marketing"/>
	</bean>

</beans>

 4.OutPut

 



 

  • 大小: 30.2 KB
0
4
分享到:
评论

相关推荐

    bean_lifecycle.zip

    在Java开发中,Spring框架是应用最广泛的IoC(Inversion of Control)和DI(Dependency Injection)容器之一。Bean在Spring框架中扮演着核心角色,它们代表了应用程序中的对象。本篇文章将深入探讨Spring Bean的生命...

    Spring高级篇二.pdf

    &lt;bean id="lifecyclebean" class="cn.itcast.spring.d_lifecycle.lifecyclebean" init-method="setup" destroy-method="teardown"&gt; &lt;/bean&gt; ``` - 需要注意的是,`destroy-method`仅对单例`scope="singleton"`...

    模拟Spring的IOC

    在Java世界中,Spring框架以其强大的依赖注入(Dependency Injection,简称DI)和控制反转(Inversion of Control,简称IOC)能力,成为企业级应用开发的首选框架之一。理解并模拟Spring的IOC机制对于深入学习Spring...

    SSH笔记-IOC容器中 Bean 的生命周期

    在Spring框架中,IOC(Inversion of Control,控制反转)容器是核心组件,它负责管理对象的生命周期,也就是Bean的生命周期。这篇SSH笔记主要探讨了如何在Spring4中利用BeanPostProcessor接口定制Bean的初始化和销毁...

    Spring in Action 使用Maven 构建Spring 工程

    Spring的IoC(Inversion of Control)容器是实现DI的关键组件。在Maven构建的工程中,我们可以使用XML配置文件(通常命名为`beans.xml`)来定义bean及其依赖关系,或者使用Java配置类,这使得配置更加灵活且易于测试...

    spring的Ioc.zip

    Spring的IoC(Inversion of Control,控制反转)是它核心功能之一,它彻底改变了Java应用的组件管理和依赖关系的解决方式。IoC容器是Spring框架的基石,它负责管理对象的生命周期和对象间的依赖关系。下面将详细介绍...

    spring-reference.pdf

    - **Lifecycle Interfaces**:定义Bean的生命周期回调方法。 - **FactoryBean**:用于创建其他Bean的对象工厂。 - **Abstract and Child Bean Definitions**:允许创建抽象Bean和继承自抽象Bean的具体Bean。 ###...

    Spring IoC容器实现的结构分析

    通过对IoC(Inversion of Control,控制反转)的实现,Spring容器将对象的创建和组装工作从应用代码中分离出来,使得应用更易于测试和维护。本文将从实现的角度,通过对外部接口、内部实现、组成部分和执行过程四个...

    SPRING API 2.0.CHM

    Lifecycle ListableBeanFactory ListenerExecutionFailedException ListenerSessionManager ListFactoryBean LoadTimeWeaver LobCreator LobCreatorUtils LobHandler LobRetrievalFailureException ...

    SpringFramework常见知识点.md

    - **Lifecycle callbacks**:Bean 生命周期的方法回调。 - **Properties**:Bean 的属性值。 #### 十、Bean 的作用域 1. **singleton**:每个容器中只有一个实例。 2. **prototype**:每次请求都会创建一个新的...

    spring-framework-reference-4.1.2

    Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans .......................................................................................

    spring-framework-reference4.1.4

    Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans .......................................................................................

Global site tag (gtag.js) - Google Analytics