spring中afterPropertiesSet方法与init-method配置描述
1. InitializingBean.afterPropertiesSet()
Spring中InitializingBean接口类为bean提供了定义初始化方法的方式,它仅仅包含一个方法:afterPropertiesSet()。
Bean实现这个接口,在afterPropertiesSet()中编写初始化代码:
public class EventEngineImpl implements EventEngine, InitializingBean {
private static final Map<String, List<SubscribeRule>> EVENT_RULE_MAP = new HashMap<String, List<SubscribeRule>>();
/**
* 规则列表
*/
protected List<SubscribeRule> listEventRule;
public void publishEvent(Event event) {
...
}
public void afterPropertiesSet() throws Exception {
...
}
public void setListEventRule(List<SubscribeRule> listEventRule) {
this.listEventRule = listEventRule;
}
}
在xml配置文件中并不需要对bean进行特殊的配置,Spring在在配置文件完成该bean的所有赋值后,会检查该bean是否实现了InitializingBean接口,如果实现就调用bean的afterPropertiesSet方法。
2. init-method配置
Spring虽然可以通过InitializingBean完成一个bean初始化后调用这个bean自定义方法,但是这种方式要求bean实现InitializingBean接口。一但bean实现了InitializingBean接口,那么这个bean的代码就和Spring耦合到一起了。可以使用Spring提供的init-method的功能来执行一个bean自定义的初始化方法。
以下代码中,类MonitorKafka不实现任何Spring的接口,定义一个没有参数的方法monitorKafkaMsg()。
public class MonitorKafka {
private String topic;
public MonitorKafka() {
}
public void monitorKafkaMsg() {
...
}
protected void destroy() {
try {
if (null != consumerConnector) {
consumerConnector.shutdown();
logger.debug("[kafka通用组件]停止消费线程组");
}
if (null != executor) {
executor.shutdownNow();
logger.debug("[kafka通用组件]停止线程池");
}
} catch (RuntimeException e) {
logger.error("[kafka通用组件]停止消费线程或停止线程池组异常.{}", e);
}
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getTopic() {
return topic;
}
}
<bean id="monitorPaymentKafka" class="com.xxx.xxx.kafka.MonitorKafka" scope="singleton" init-method="monitorKafkaMsg" destroy-method="destroy">
<property name="topic" value="test" />
</bean>
注:destroy-method是该bean销毁前调用指定的方法。
init-method配置中的monitorKafkaMsg()方法将会在该bean初始化完成后被调用,Spring要求init-method是一个无参数的方法,否则会抛出异常,Spring将中止这个Bean的后续处理,并且抛出一个 org.springframework.beans.factory.BeanCreationException异常。
总结:
1. InitializingBean和init-method可以一起使用,Spring会先处理InitializingBean再处理init-method。init-method是通过反射执行的,而afterPropertiesSet是直接执行的,所以 afterPropertiesSet的执行效率比init-method要高,不过init-method消除了bean对Spring依赖,推荐使用init-method。
2. 如果一个bean被定义为非单例的,那么afterPropertiesSet和init-method在bean的每一个实例被创建时都会执行。单例bean的afterPropertiesSet和init-method只在bean第一次被实例时调用一次。一般情况下afterPropertiesSet和init-method都应用在单例的bean上。
3. @PostConstruct和@PreDestory可以通过在类方法上注解方式实现类似的功能。
spring中afterPropertiesSet方法与init-method配置描述的更多相关文章
- Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/spring/applicationContext.xml]: Invocation of init method failed;
我报的错: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSes ...
- spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...
- Java第三阶段学习(十一、Servlet基础、servlet中的方法、servlet的配置、ServletContext对象)
一.Servlet简介 1.什么是servlet: sun公司提供的一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API动态的向 ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- 《Java虚拟机原理图解》1.5、 class文件中的方法表集合--method方法在class文件中是怎样组织的
0. 前言 了解JVM虚拟机原理是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描 ...
- Spring中ApplicationContext加载机制和配置初始化
Spring中ApplicationContext加载机制. 加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet. ...
- java中的方法引用(method reference)官方文档总结
2017/7/5 转载写明出处:http://www.cnblogs.com/daren-lin/p/java-method-reference.html 今天要说的是java中的一项新特性,方法引用 ...
- 【Spring】Spring中的Bean - 1、Baen配置
Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...
- Spring中依赖注入的使用和配置
使用方法1: //在执行此实例化的时候就会完成所有注入 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ...
随机推荐
- <Spark><Spark Streaming><作业分析><JobHistory>
Intro 这篇是对一个Spark (Streaming)作业的log进行分析.用来加深对Spark application运行过程,优化空间的各种理解. Here to Start 从我这个初学者写 ...
- 2019.3.22 Week 12 : ZigBee and T/H chamber test
Test purposes Remove backside center ventilation holes, pls help to conduct climatic chamber test of ...
- css , dl , dt , dd 的使用. calc
dl .dt, dd 虽然很少使用, 但是 有时使用会有 更好的效果: 一: 展示图片: ------------------------- 代码: <!DOCTYPE html> < ...
- vue 自定义过滤器 格式化金额(保留两位小数)
1.js部分 import Vue from 'vue' Vue.filter('money', function(val) { val = val.toString().replace(/\$|\, ...
- WHID Injector:将HID攻击带入新境界
HID Attack是最近几年流行的一类攻击方式.HID是Human Interface Device的缩写,意思是人机接口设备.它是对鼠标.键盘.游戏手柄这一类可以操控电脑设备的统称. 由于电脑对这 ...
- socket长连接理解
在一个tcp连接上可以连续发送多个数据包,在tcp连接保持期间,如果没有数据包发送,需要双方发检测包以维持此连接,一般需要自己做在线维持. 长连接指建立socket连接后不管是否使用都保持连接,但安全 ...
- JAVA中的Set
Set中存放的是没有重复的数据,下说记录一下使用中的小细节. 1.HashSet 区分大小写: Set<String> set1 = new HashSet<String>() ...
- Spring的PropertyPlaceholderConfigurer
在项目中我们一般将配置信息(如数据库的配置信息)配置在一个properties文件中,如下: jdbcUrl=jdbc:mysql://localhost:3306/flowable?useUnico ...
- 第二十六课 典型问题分析(Bugfix)
问题1: glibc中的strdup实现如下: 没有对参数s进行空指针判断. 我们的Exception.cpp中应做改进: 在第12行进行判断空指针操作. 问题2: t1在析构时会抛出异常,我们在re ...
- 【leetcode】69-Sqrt(x)
problem Sqrt(x) code class Solution { public: int mySqrt(int x) {// x/b=b long long res = x;// while ...