@PostConstruct 和 @PreDestory
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是:通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
1:定义相关的实现类:
- package com.myapp.core.annotation.init;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- public class PersonService {
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- @PostConstruct
- public void init(){
- System.out.println("I'm init method using @PostConstrut...."+message);
- }
- @PreDestroy
- public void dostory(){
- System.out.println("I'm destory method using @PreDestroy....."+message);
- }
- }
2:定义相关的配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <context:annotation-config />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
- package com.myapp.core.annotation.init;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
- PersonService personService = (PersonService)context.getBean("personService");
- personService.dostory();
- }
- }
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
类来告诉Spring容器采用的 常用 注解配置的方式:
只需要修改配置文件为:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <!-- <context:annotation-config /> -->
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
同样可以得到以上测试的输出结果。
@PostConstruct 和 @PreDestory的更多相关文章
- Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Spring注解@PostConstruct与@PreDestroy
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- spring注解:@PostConstruct和@PreDestroy
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Spring实现初始化和销毁bean之前进行的操作,三种方式
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Spring学习笔记之三----基于Annotation的Spring IOC配置
使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...
- spring常用注解使用解析
spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件.spring将会把合适的java类全部注册成spring Bean. 问题:spring怎么知道把哪些 ...
- Spring详细总结
Spring的特性之一:IOC(控制反转Inverse Of Control),又称依赖注入,是一种重要的面向对象编程的法则来削减计算机程序的耦合问题 也是轻量级spring框架的核心: 依赖注入: ...
随机推荐
- Linux登录ssh携带密码
最近在使用ssh,但是每次都需要输入密码,想简单一步搞定,Google了一下,发现了一个比较笨的但实用的方法 1.安装 sshpass sudo apt-get install sshpass 2.携 ...
- Spring中AOP的模拟实现
什么是AOP? 面向切面编程(AOP)完善spring的依赖注入(DI),面向切面编程在spring中主要表现为两个方面 1.面向切面编程提供声明式事务管理 2.spring支持用户自定义的切面 面向 ...
- 利用GeneratedKeyHolder获得新增数据主键值
Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录所对应的主键值的方法: int update(PreparedStatementCreator psc, KeyHold ...
- 微信小程序的动画效果
前言 由于公司计划有变,所以从H5页面改成去小程序写.所以在着手开发小程序.本人也不是什么前端高手,只是一名写后端偶尔写写前端的渣渣.请前端大神们勿喷. 一.什么是微信小程序? 小程序在我的理解中只是 ...
- [avalon]data-repeat-rendered循环渲染完毕后的回调函数
有些时候需要在渲染完毕后,引用其他类似JQ插件的初始化工作.这时候需要使用data-repeat-rendered的回调函数. 应用场景: 在metro-start界面下,需要给每个tile引用cli ...
- Js-Html 前端系列--Ajax
最近在做Web项目,用到很多Ajax,也遇到很多问题.下面与大家分享. Ajax传参 Ajax同步与异步 Ajax与谷歌浏览器 Ajax传参 我们一般传参有两种形式: $.ajax({ ... dat ...
- Flexible 弹性盒子模型之CSS justify-content 属性
实例 在弹性盒对象的 <div> 元素中的各项周围留有空白: div { display: flex; justify-content: space-around; } 复制 效果预览 浏 ...
- ACM博弈问题小试
题目: 取石子(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作 ...
- 递归思路分解(C#)
例子一:求1!+2!+......+X! 思路分解:因为是用递归思想解决问题,也就是方法调用方法.那么肯定的方法是重复利用的.在这道题里,我们要重复利用的也就是求X!和求和 所以我们先把求X!的代码写 ...
- codeforces 192A Funky Numbers
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...