1.@PostConstruct与@PreDestroy

@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
@PreDestroy:在容器销毁bean之前通知我们进行清理工作

2.实例

@Service
public class PersonService {@PostConstructpublic void post(){System.out.println("PersonService ... PostConstruct");}@PreDestroypublic void pre(){System.out.println("PersonService ... PreDestroy");}
}@Configuration
@ComponentScan(value = {"com.java.service"})
public class PostConstructConfig {
}//测试类
public class PostConstructTest {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PostConstructConfig.class);System.out.println("applicationContext ..... 初始化结束");//只有bean从容器中移除时,才会调用preDestroy来通知System.out.println("applicationContext ..... 准备关闭");applicationContext.close();System.out.println("applicationContext ..... 已关闭");}
}运行结果:PersonService ... PostConstruct
applicationContext ..... 初始化结束
applicationContext ..... 准备关闭
PersonService ... PreDestroy
applicationContext ..... 已关闭

【Spring注解系列08】@PostConstruct与@PreDestroy相关推荐

  1. 【Spring注解系列10】SpringBean的生命周期

    1.SpringBean生命周期定义 指bean创建---初始化----销毁的过程. 构造(对象创建): 单实例:在容器启动的时候创建对象 多实例:在每次获取的时候创建对象 初始化和销毁方式: 指定初 ...

  2. 【Spring注解系列13】Spring自动装配总结:@Autowired、@Resource、@Qualifier、@Inject

    目录 1.@Autowired.@Resource.@Qualifier.@Inject 1).@Autowired 2).@Resource与@Inject 3). @Autowired参数取值 2 ...

  3. 【Spring注解系列11】Spring后置处理器BeanPostProcessor用法与原理

    1.BeanPostProcessor原理 先说,bean的后置处理器BeanPostProcessor接口中两个方法: postProcessBeforeInitialization:在初始化之前工 ...

  4. 【Spring注解系列07】Spring注入Bean有哪些方式总结

    给Spring容器中注册组件方式: 1).包扫描+组件标注注解(@Controller/@Service/@Repository/@Component) 2).@Bean 导入的第三方包里面的组件 3 ...

  5. 【Spring注解系列06】FactoryBean注入对象用法

    使用Spring提供的 FactoryBean(工厂Bean); 1).默认获取到的是工厂bean调用getObject创建的对象 2).要获取工厂Bean本身,我们需要给id前面加一个&   ...

  6. 【Spring注解系列01】@Configuration与@Bean

    1. @Configuration与@Bean @Configuration: 告诉Spring这是一个配置类,配置类==配置文件. @Configuration==beans.xml @Bean: ...

  7. 【Spring注解系列14】@Profile进行环境切换

    1.@Profile Profile:      Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能: 开发环境.测试环境.生产环境: 数据源:(/A)(/B)(/C): @P ...

  8. 【Spring注解系列12】@Value与@PropertySource注解

    1.@Value与@PropertySource注解 @Value注解:主要用于赋值,该值可以是取值配置文件中的,也可以直接赋值,也可以使用SpEl表达式进行计算的结果,抑或直接从环境变量中获取. 该 ...

  9. 【Spring注解系列05】@Import注入原理

    1.@Import注解 用于注入指定的类,导入组件id默认是组件的全类名. 只能作用于类上. 属性:value = {xx.class,xx.class}        说明:xx.class为要导入 ...

最新文章

  1. 不可错过的2019秋招CV岗心得!原来拿offer也是有套路的
  2. MYSQL千万级数据量的优化方法积累
  3. php拍照从手机相册中选择,Android获取图片:拍照和从相册中选择
  4. php 检查图片重复度,php – 检测图片的“整体平均”颜色
  5. 北大教授最短毕业致辞。#人性的可爱 #饶毅#自我尊重
  6. 双11个性化推荐背后,阿里云“舜天”如何应对百亿次挑战? 1
  7. C/C++队列与循环队列
  8. 光棍节脱单,单身狗该你上了
  9. python actor_Python定义一个Actor任务
  10. 大数据平台系统设计包括哪些
  11. java 线程协作 wait(等待)与 notiy(通知)
  12. openSUSE Tumbleweed 支持 Linux Kernel 4.20
  13. 局部内部类使用局部变量应注意什么?
  14. OLS和GWR模型部分参数解释
  15. 继承ActionSupport实现Action
  16. 利用jieba对已爬取好的中国地名信息进行分词
  17. c++ 中 setw()函数
  18. 我的关于 **Mermaid** 语法
  19. 19清明假前一周安排
  20. 【暑假阅读推荐书目】经济篇

热门文章

  1. BLE 包结构及传输速率
  2. linux设备驱动——andriod平台wlan驱动
  3. UNICODE与UTF-8的转换
  4. 特征工程(2):特征构建
  5. stdthread(6)并发mutex
  6. EOS 智能合约源代码解读 (14)system合约“exchange_state.hpp”
  7. 定时器Timer和Ticker
  8. 数据结构--稀疏矩阵的一种实现
  9. [ARM异常]-图解armv7/armv8的异常向量表和基地址
  10. VC6导入和使用二进制资源