关于在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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2021-08-19
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-07
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案