今天看到一个老代码, 是一个定时任务, 但是使用的是java的定时器,但是怎么也找不到是谁在加载它, 看见有个init方法,调用了,最后看见有个@PostConstruct 不知道干什么的, 于是就查了一下, 原来是,springmvc 中的注解功能, 具体他的作用, 如下:以作备忘
关于在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); } }
<?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>
通过申明加载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>
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
相关推荐
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
NULL 博文链接:https://nhy520.iteye.com/blog/889276
java @PostConstruct和@PreConstruct注解整理.........
获取springbean对象
主要介绍了Spring实战之使用@POSTConstruct和@PreDestroy定制生命周期行为操作,结合实例形式详细分析了Spring使用@POSTConstruct和@PreDestroy定制生命周期相关接口定义、配置与功能实现技巧,需要的朋友可以参考下
spring 注解初始化,初始化开始@postconstruct 对象销毁@predestroy javax.annotation.jar文件
@postConstruct 所标注的方法 内部是靠的spring提供的两个后置处理器(InitDestroyAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor)共同 协调分布处理完成的。 这2点也是网上绝大部人没讲明白的...
Spring Bean 的生命周期 > Spring Bean 的生命周期简单易懂。在一个 bean 实例被初始化时,需要执行一系列的初 始化操作以达到可用的状态。同样的,当一个 bean 不在被调用...> @PostConstruct 和@PreDestroy 注解方式
为了定义一个 bean 的安装和卸载,我们可以使用 init-method 和 destroy-method 参数简单的声明一下 ,其中 init-method 属性指定了一个方法,该方法在 bean 的实例化阶段会立即被调用;同样地,destro
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
Bean的初始化和销毁拓展: 18 Spring的IOC总结: 20 3.5 依赖注入(DI) 20 3.5.1 使用构造器注入 20 3.5.2 使用属性setting方法进行注入 21 3.5.3 装配list集合 22 3.5.4 装配set集合 22 3.5.5 装配map 22 3.5.6 装配...
spring4框架系列 [ 5 ] 基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy
主要给大家介绍了关于Spring定时任务中@PostConstruct被多次执行异常的分析与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
SpringBoot项目启动时实现调用一次初始化方法
主要介绍了SpringBoot @PostConstruct原理用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
4.6.1.3 默认的初始化和销毁方法 71 4.6.1.4 组合生命周期机制.. 72 4.6.1.5 启动和关闭回调 73 4.6.1.6 在非Web 应用中,优雅地关闭Spring IoC 容器 74 4.6.2 ApplicationContextAware 和...
spring MVC 初始启动concurrent blocking queue,通过@PostConstruct 注解实现,详情参考我的博客
Java中 @PostConstruct和@PreDestroy注解有什么作用?编写Webservice接口服务端(框架)有几种方式?常用的缓存框架有哪几个?简述TCP三次握手的理解 Linux下你常用的命令有哪些?Nginx有什么作用?
Spring需要的相关jar包。包括spring.jar 、commons-logging.jar 如果使用了且卖弄编程(AOP)还需要下列jar...如果使用了JSR-250中的注解,如@PostConstruct/@PreDestroy 还需要下列jar文件 common-annotations.jar