2.21@PostConstruct 注解

戴着假发的程序员出品  抖音ID:戴着假发的程序员  欢迎关注

[查看视频教程]

源码:

1 @Documented
2 @Retention (RUNTIME)
3 @Target(METHOD)
4 public @interface PostConstruct {
5 }

@PostConstruct和bean标签的属性init-method有同样的作用,可以标记bean生命周期回调方法的初始化方法。

案例:

 1 /**
 2  * @author 戴着假发的程序员
 3  *  
 4  * @description
 5  */
 6 @Component
 7 public class AuthorDAO implements IAutorDAO  {
 8     public AuthorDAO(){
 9         System.out.println("实例化AuthorDAO");
10     }
11     @PostConstruct
12     public void init(){
13         System.out.println("执行AuthorDAO的初始化方法-init");
14     }
15     @Override
16     public String get() {
17         return "戴着假发的程序员";
18     }
19 }

当我们初始化容器时控制台输出:

1 ApplicationContext ac =
2         new AnnotationConfigApplicationContext("com. st.dk.demo4");
实例化AuthorDAO
执行AuthorDAO的初始化方法-init

 

posted @ 2020-10-12 08:21  戴着假发的程序员0-1  阅读(134)  评论(0编辑  收藏  举报