您的位置:首页 > 编程语言 > Java开发

Spring will invoke specific constructor no matter this constructor is public or private

2016-12-04 21:47 513 查看
I have confirm this conclusion via debugging my own code.

Spring will invoke specific constructor no matter this constructor is public or private

This is my demo:

package com.frank.zk;

/**
*
* @author liushaomingdev@163.com
*
*/
public class ZooKeeperManager extends ZookeeperClient implements Watcher {
private static ZooKeeperManager INSTANCE;
private ZooKeeperManager() {
INSTANCE = this;
}
public static ZooKeeperManager getInstance() {
return INSTANCE;
}
}

this is configuration file--applicationContext.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <bean id="zookeeper" class="com.frank.zk.ZooKeeperManager" lazy-init="false" />
</beans>


Program execute to this break point finally.

private ZooKeeperManager() {
INSTANCE = this;
}


So,  below conclusion is confirmed:
Spring will invoke specific constructor no matter this constructor is public or private

This program use Spring to initialize singleton creating bean. It also uses Factory pattern to instantiate object.

Demo code

// bean 'ZooKeeperManager' is registered as singleton bean in Spring Container after this line code.
ApplicationContext appContext = new ClassPathXmlApplicationContext("appContext.xml");

ZooKeeperManager manager = ZooKeeperManager.getInstance();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐