`
tian602564031
  • 浏览: 22834 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ApplicationContext.xml 单例

 
阅读更多

因为applicationContext.xml 是唯一的,所以可以写成单例模式

package com.founder.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

final public class ApplicationContextUtil {
	/**
	 * 由于applicationContext.xml 的唯一性,在这里可以把它写成单例模式
	 */
	private static ApplicationContext applicationContext = null;
	private  ApplicationContextUtil(){} //空的构造方法
	
	static{
		/**
		 * static 静态代码快,只在该类被加载时,执行一次
		 */
		applicationContext = new ClassPathXmlApplicationContext("com/founder/config/applicationContext.xml");
	}
	public static ApplicationContext getApplicationContext(){
		
		return applicationContext;
	}

}

 

package com.founder.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.founder.service.UserService;
import com.founder.util.ApplicationContextUtil;

public class Test {
	
	public static void main(String[] arg){
		
		/**
		 * 传统的方法,调用UserService 的 sayHello()方法
		 */
		
		/*UserService userService = new UserService();
		userService.setName("Aloys");
		userService.sayHello();*/
		
		/**
		 * 接下来,我们使用Spring的框架
		 * 
		 * 1.引入Spring的开发包
		 * 2.添加到build path路径
		 * 3.创建applicationContext.xml
		 * 4.配置bean
		 * 5.得到spring容器对象
		 */
		/*ApplicationContext appContex =  new ClassPathXmlApplicationContext("com/founder/config/applicationContext.xml"); //通过类路径来加载配置文件
		
		UserService userService = (UserService) appContex.getBean("userService");
		userService.sayHello();*/
		
		((UserService)(ApplicationContextUtil.getApplicationContext().getBean("userService"))).sayHello();
	}
}

 

单例模式:

 0

Java 单例相关问题5

1.分析下面代码,各自有什么问题

Singleton 模式主要保证在JAVA应用程序中,一个Class只有一个实例存在。一般Singleton模式通常有几种形式:

//第一种形式:定义一个类,他的构造函数为private的,它有一个静态的私有变量,在类初始化时实例化,通过一个public的getIntance方法获取对它的引用,继而调用其中的方法。



public class Singleton {

private static Singleton instance = new Singleton();

private Singleton(){}

public static Singleton getInstance() {

return instance;

}

}

//第二种形式:

public class Singleton {

private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {

if(instance == null) {

return new Singleton ();

}

return instance;

}

}

}

 

 


问题补充:

 

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

 

//还可以再优化: 
public class Singleton { 
private static Singleton instance = null; 
public static Singleton getInstance() { 
if (instance !=null ) 
return instance ; 
else { 

} 

instance=new Singleton(); 
return instance; } 
} 


synchronized 
 
 
 
//还可以再优化: 
public class Singleton { 
private static Singleton instance = null; 
public static Singleton getInstance() { 
if (instance !=null ) 
return instance ; 
else { 

} 

instance=new Singleton(); 
return instance; } 
} 


synchronized 

 
 

你看完这篇http://coolshell.cn/articles/265.html
估计就全明白了

 
第一种不是懒惰加载,初始化会费时一点点,也许花了时间初始化后面不一定真用得上.

第二种方法如果经常被调用,因为有同步问题,会对性能有影响.


其实单例有更换的办法,可以使用内部类,枚举,或者双重锁.推荐我收藏的几个Blog给你看看.

http://chenjianjx.iteye.com/blog/1839117
http://www.importnew.com/6461.html
http://jiangzhengjun.iteye.com/blog/652440

第一种问题,对象开始可以不初始化,等到获取实例的时候才初始化;
第二种问题,没有线程同步,会有线程安全问题。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics