栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@PostConstruct注解和@PreDestroy注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@PostConstruct注解和@PreDestroy注解

@PostConstruct注解作用:

在bean创建完成并且属性复制完成后,执行初始化方法

@PreDestroy注解使用:

在bean移除之前使用

上面两个注解不属于Spring,在下面依赖中:


    javax.annotation
    javax.annotation-api
    1.3.2

【示例】:
@Component
public class Dog {
    public Dog() {
        System.out.println("Dog Construct....");
    }

    
    @PostConstruct
    public void init() {
        System.out.println("Dog @PostConstruct....");
    }

    
    @PreDestroy
    public void destroy() {
        System.out.println("Dog @PreDestroy");
    }
}

// 输出:
Dog Construct....
Dog @PostConstruct....
org.springframework.context.annotation.AnnotationConfigApplicationContext@52af6cff, started on Sun Mar 06 16:46:48 CST 2022
Dog @PreDestroy
相关链接

@Bean指定初始化和销毁方法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号