转自:http://blog.csdn.net/topwqp/article/details/8681573
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
1:定义相应类实现InitializingBean ,DisposableBean 接口
- package com.myapp.core.annotation.init;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- public class PersonService implements InitializingBean,DisposableBean{
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- @Override
- public void destroy() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("I'm init method using implements InitializingBean interface...."+message);
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("I'm init method using implements DisposableBean interface...."+message);
- }
- }
2:定义相应的配置文件:
- <?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.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <!-- <context:annotation-config /> -->
- <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- -->
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
3:测试类:
- package com.myapp.core.annotation.init;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- AbstractApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
- PersonService personService = (PersonService)context.getBean("personService");
- context.registerShutdownHook();
- }
- }
4:输出测试结果:
- I'm init method using implements DisposableBean interface....123
- 三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose
- INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy
- I'm init method using implements InitializingBean interface....123
相关推荐
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
(6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法postProcessBeforeInitialzation()对Bean进行加工操作,这个非常重要,Spring的AOP就是用它实现的。 (7)如果Bean实现了...
spring配置对象实例化后执行的方法,两种实现比对,通过InitializingBean接口,并实现arterPropertiesSet()方法;或者通过applicationContext.xml配置init-method属性
Spring Bean 的生命周期 ...> InitializingBean 和 DisposableBean 回调接口 > 针对特殊行为的其他 Aware 接口 > Bean 配置文件中的 Custom init()方法和 destroy()方法 > @PostConstruct 和@PreDestroy 注解方式
主要介绍了Spring中的InitializingBean接口的使用,需要的朋友可以参考下
同时,Spring还提供了扩展点(如BeanPostProcessor和InitializingBean接口),允许开发人员在Bean生命周期的不同阶段进行自定义操作。 AOP(面向切面编程)功能:Spring框架提供了AOP功能,通过切面(Aspect)来...
1. Bean的定义,给出配置元数据,可通过xml、java类、注解三种方式 2. Bean的初始化,调用InitializingBean 接口定义的after
NULL 博文链接:https://latex.iteye.com/blog/900664
NULL 博文链接:https://yuui89828923aaa.iteye.com/blog/812569
将会逐步扩大完善Spring Boot版本:2.1.7,兼容的Spring Cloud版本为Greenwich,版本对照参考概要如何配置拦截器:拦截器,过滤器,@ RestControllerAdvice bean的初始化:InitializingBean接口,@ conditionXXX注解...
All Classes ...InitializingBean InputStreamEditor InputStreamResource InputStreamSource InputTag InstantiationAwareBeanPostProcessor InstantiationAwareBeanPostProcessorAdapter ...
春天学习笔记 Bean初始化 BeanDefinitionBuilder 新的GenericBeanDefinition Bean的命名(非强制性) ...延迟初始化和非延迟对象的差异:应用之上启动前后BeanInitialDemo Bean的销毁@PreDestroy> Dispo
spring+InitializingBean+quartz+定时器-附件资源
bean),即使用普通的“new”运算符创建 bean,然后 spring 进一步控制初始化 bean,替换 @Value 占位符 bean 属性上的值,调用 afterPropertiesSet InitializingBean 类型,注入@Autowired 或@Inject 属性等。...
EWA讲义 Hacettepe大学企业Web体系结构讲座的幻灯片和示例应用程序 应用清单: AjaxApp:基于Eclipse的Web项目,使用jQuery演示...spring-bean-lifecycle:使用init-method,destroy-method,InitializingBean,Disp
.1. netty-sekiro 原文 感谢作者。以下是对原作者内容的学习提取 ...服务器端是用Springboot管理各种组件,在Springboot bean(InitializingBean)初始化 afterPropertiesSet完成会启动两个nio服务,分别监听5600
【资源说明】 1、基于SpringBoot的高并发选课系统源码+项目说明(毕设).zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计...通过上述代码所示,Spring