public class Singleton {
private static Singleton instance = null; [懒汉式]
protected Singleton() {
// Exists only to defeat instantiation.
}
public static Singleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
public synchronized static Singleton getInstance() {
if(singleton == null) {
singleton = new Singleton();
}
logger.info("created singleton: " + singleton);
return singleton;
}
public class Singleton {
private static Singleton instance = new Singleton();[饿汉式]
}
public static Singleton getInstance() {
return Singleton.instance;
}
public static Class applicationClass(String className,
ClassLoader classLoader)
throws ClassNotFoundException {
if (classLoader == null) {
// Look up the class loader to be used
classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = RequestUtils.class.getClassLoader();
}
}
// Attempt to load the specified class
return (classLoader.loadClass(className));
}
Return an Action
instance that will be used to process
* the current request, creating a new one if necessary.
protected Action processActionCreate(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws IOException {
String className = mapping.getType();
Action instance;
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
}
// Create and return a new Action instance
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
}
try {
instance = (Action) RequestUtils.applicationInstance(className);
// Maybe we should propagate this exception
// instead of returning null.
} catch (Exception e) {
log.error(getInternal().getMessage("actionCreate",
mapping.getPath()), e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("actionCreate", mapping.getPath()));
return (null);
}
actions.put(className, instance);
if (instance.getServlet() == null) {
instance.setServlet(this.servlet);
}
}
return (instance);
}
public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
Assert.notNull(beanName, "'beanName' must not be null");
synchronized (this.singletonObjects) {
// Re-check singleton cache within synchronized block.
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName,
"Singleton bean creation not allowed while the singletons of this factory are in destruction " +
"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (logger.isDebugEnabled()) {
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
beforeSingletonCreation(beanName);//singletonsCurrentlyInCreation,同步的集合,添加BeanName
try {
singletonObject = singletonFactory.getObject();//返回这个对象.
}
finally {
afterSingletonCreation(beanName);//singletonsCurrentlyInCreation.同步的集合中,移除BeanName
}
addSingleton(beanName, singletonObject);//添加对象到singletonObjects
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
}
* 1. beforeSingletonCreation(beanName); //监视创建对象前
* 2. singletonObject = singletonFactory.getObject();//返回对象
*singletonFactory.getObject();这是接口方法,这个接口由程序在调用的时候实现.
*getSingleton(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
try {
return createBean(beanName, mbd, args);//创建方法就是在子类要重写的
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
}
});
* 3.afterSingletonCreation(beanName);//监视创建对象后
* 4.addSingleton(beanName, singletonObject);//添加对象到singletonObjects
* 5.返回该对象.
* 综上所述:在应用于系统资源时,为了提供同一接口,需要同步信息,但是Struts和Spring都使用了代码块同步,我们写程序的时候也要向
* 那些大师学习,尽量同步代码块.
*
*/
}