Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

简介: 关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作第二种是:通过 在xml中定义init-method 和  destory-method方法第三种是:...

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和  destory-method方法

第三种是: 通过bean实现InitializingBean和 DisposableBean接口


下面演示通过  @PostConstruct 和 @PreDestory

1:定义相关的实现类:

 

 1     package com.myapp.core.annotation.init;  
 2       
 3     import javax.annotation.PostConstruct;  
 4     import javax.annotation.PreDestroy;  
 5       
 6     public class PersonService {  
 7         
 8         private String  message;  
 9       
10         public String getMessage() {  
11             return message;  
12         }  
13       
14         public void setMessage(String message) {  
15             this.message = message;  
16         }  
17           
18         @PostConstruct  
19         public void  init(){  
20             System.out.println("I'm  init  method  using  @PostConstrut...."+message);  
21         }  
22           
23         @PreDestroy  
24         public void  dostory(){  
25             System.out.println("I'm  destory method  using  @PreDestroy....."+message);  
26         }  
27           
28     }  
View Code

 

2:定义相关的配置文件:

 1     <?xml version="1.0" encoding="UTF-8"?>  
 2     <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 6     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
 7     http://www.springframework.org/schema/context  
 8     http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
 9       
10     <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->  
11       
12     <context:annotation-config />  
13       
14     <bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
15       <property name="message" value="123"></property>  
16     </bean>  
17       
18     </beans>  
View Code

其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;

测试类:

 

 1     package com.myapp.core.annotation.init;  
 2       
 3     import org.springframework.context.ApplicationContext;  
 4     import org.springframework.context.support.ClassPathXmlApplicationContext;  
 5       
 6     public class MainTest {  
 7           
 8         public static void main(String[] args) {  
 9               
10             ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");  
11               
12             PersonService   personService  =  (PersonService)context.getBean("personService");  
13               
14             personService.dostory();  
15         }  
16       
17     }  
View Code

 

测试结果:

I'm  init  method  using  @PostConstrut....123
I'm  destory method  using  @PreDestroy.....123


其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

类来告诉Spring容器采用的 常用 注解配置的方式:

只需要修改配置文件为:

 

 1     <?xml version="1.0" encoding="UTF-8"?>  
 2     <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 6     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
 7     http://www.springframework.org/schema/context  
 8     http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
 9       
10     <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->  
11       
12     <!-- <context:annotation-config /> -->  
13       
14       
15     <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  
16     <bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
17               <property name="message" value="123"></property>  
18     </bean>  
19       
20       
21       
22     </beans>  
View Code

 

同样可以得到以上测试的输出结果。

 

相关文章
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
866 73
|
人工智能 自然语言处理 Java
Spring 集成 DeepSeek 的 3大方法(史上最全)
DeepSeek 的 API 接口和 OpenAI 是兼容的。我们可以自定义 http client,按照 OpenAI 的rest 接口格式,去访问 DeepSeek。自定义 Client 集成DeepSeek ,可以通过以下步骤实现。步骤 1:准备工作访问 DeepSeek 的开发者平台,注册并获取 API 密钥。DeepSeek 提供了与 OpenAI 兼容的 API 端点(例如),确保你已获取正确的 API 地址。
Spring 集成 DeepSeek 的 3大方法(史上最全)
|
缓存 安全 Java
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
1394 5
|
Java Spring 容器
两种Spring Boot 项目启动自动执行方法的实现方式
在Spring Boot项目启动后执行特定代码的实际应用场景中,可通过实现`ApplicationRunner`或`CommandLineRunner`接口完成初始化操作,如系统常量或配置加载。两者均支持通过`@Order`注解控制执行顺序,值越小优先级越高。区别在于参数接收方式:`CommandLineRunner`使用字符串数组,而`ApplicationRunner`采用`ApplicationArguments`对象。注意,`@Order`仅影响Bean执行顺序,不影响加载顺序。
1199 2
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
1535 4
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
前端开发 Java Spring
Spring MVC源码分析之DispatcherServlet#getHandlerAdapter方法
`DispatcherServlet`的 `getHandlerAdapter`方法是Spring MVC处理请求的核心部分之一。它通过遍历预定义的 `HandlerAdapter`列表,找到适用于当前处理器的适配器,并调用适配器执行具体的处理逻辑。理解这个方法有助于深入了解Spring MVC的工作机制和扩展点。
283 0
|
XML 缓存 Java
Spring - @PostConstruct & @PreDestroy
Spring - @PostConstruct & @PreDestroy
449 0
Spring - @PostConstruct & @PreDestroy
|
Java Spring 容器
【Spring注解驱动开发】@PostConstruct与@PreDestroy源码的执行过程
在前面的《【String注解驱动开发】你真的了解@PostConstruct注解和@PreDestroy注解吗?》一文中,我们简单的介绍了@PostConstruct注解与@PreDestroy注解的用法,有不少小伙伴纷纷留言说:在Spring中,@PostConstruct注解与@PreDestroy注解标注的方法是在哪里调用的呀?相信大家应该都挺好奇的吧,那今天我们就来一起分析下@PostConstruct注解与@PreDestroy注解的执行过程吧!
610 0
【Spring注解驱动开发】@PostConstruct与@PreDestroy源码的执行过程
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1674 0