【Spring实战】—— 3 使用facotry-method建立单例Bean总结

若是有这样的需求spring

  1 不想再bean.xml加载的时候实例化bean,而是想把加载bean.xml与实例化对象分离。安全

  2 实现单例的beanspa

以上的状况,均可以经过工厂方法factory-method来建立bean线程

这样再加载bean.xml时,不会直接实例化bean,而是当调用factory-method所指的方法时,才开始真正的实例化。code

  首先看一下传统的单例模式的实现方式:orm

  1 最原始的实现单例模式的方法(存在线程不安全):

public class SingletonOne { private static SingletonOne instance = null; private SingletonOne() {} public static SingletonOne getInstance() { if (instance == null) { instance = new SingletonOne(); } return instance; } } 

  可是这种方法有一个弊端,就是存在线程的不安全!xml

  好比当两个线程同时进入if(instance == null)时,一个线程判断了当前为空,而后切换到另外一个线程,这个线程也判断为空。而后切换回第一个线程,进行实例化,再切换到第二个线程,进行实例化。这样就存在了两个实例。对象

  2 经过关键字Synchronized强制线程同步 

package com.something.singleton; public class SingletonTwo { private static SingletonTwo instance = null; private SingletonTwo() {} public static synchronized SingletonTwo getInstance() { if (instance == null) { instance = new SingletonTwo(); } return instance; } }

  这样当线程进行到getInstance会同步的进行,不会有线程安全问题,可是不单单是实例化,每次调用也须要同步,这样就会形成不少资源的浪费。blog

  3 经过静态内部类进行单例

public class SingletonThree { private static class SingletonHolder{ static SingletonThree instance = new SingletonThree(); } 
  private SingletonThree() {} public static SingletonThree getInstance() { return SingletonHolder.instance; } }

  这种方法时最推荐的一种方法,因为Java的调用机制,SingletonHolder只有在调用getInstance的时候才会加载,而内部的静态类只会被加载一次,所以又是线程安全的。资源

  总结起来

  第一种方法,是存在线程安全问题的。

  第二种方法,则消耗了必定的资源。

  第三种方法,比较推荐。

 

  下面经过spring的factory-method来建立单例的bean

  首先经过静态内部类建立一个单例对象

package com.spring.test.factorymethod; public class Stage { public void perform(){ System.out.println("演出开始..."); } private Stage(){ } private static class StageSingletonHolder{ static Stage instance = new Stage(); } public static Stage getInstance(){ return StageSingletonHolder.instance; } }

  在spring配置文件中指定加载的方法getInstance

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="theStage" class="com.spring.test.factorymethod.Stage" factory-method="getInstance"></bean>
</beans>

  经过应用上下文调用bean获取实例

package com.spring.test.factorymethod; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); Stage stage = ((Stage)ctx.getBean("theStage")).getInstance(); stage.perform(); } }

  执行结果

一月 24, 2015 6:38:18 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@512dbd1a: startup date [Sat Jan 24 18:38:18 CST 2015]; root of context hierarchy 一月 24, 2015 6:38:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [bean.xml] 一月 24, 2015 6:38:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2d1879ea: defining beans [duke,sonnet29,poeticDuke,theStage]; root of factory hierarchy 演出开始...
相关文章
相关标签/搜索