⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beancontainerfactory.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
字号:
/*
 * Created on 2004-8-18
 *
 */
package com.esimple.framework.bean;

import java.util.List;

import javax.servlet.ServletException;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.esimple.framework.util.StringUtils;

/**
 * @author steven
 *
 */
public class BeanContainerFactory {
	private Log log = LogFactory.getLog(this.getClass());
	private static BeanContainerFactory bcf;
	private BeanContainer bc;
	
	public static BeanContainerFactory getInstance(){
		if( bcf == null ){
			bcf = new BeanContainerFactory();
		}
		return bcf;
	}
	
	private BeanContainerFactory(){
		bc = new BeanContainer();
	}
	
	public static BeanContainer getBeanContainer(){
		if( bcf == null || bcf.bc == null ){
			throw new RuntimeException ("BeanContainer has not inited");
		}
		return bcf.bc;
	}
	
	public void initFromURL(List urls)
	throws ServletException{
		try {
				DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
				XmlBeanDefinitionReader reader = 
					new XmlBeanDefinitionReader( factory );
		
				for ( int i = 0; i< urls.size() ; i++){
					log.debug("beans file path:" + urls.get(i) );
					reader.loadBeanDefinitions(
						(UrlResource)urls.get(i)
					);
				}
				bc.setFactory( factory );				
			} catch (Exception ex) {
				ex.fillInStackTrace();
				log.error(ex, ex);
				throw new ServletException("Unable to initialize Spring bean factory");
			}
	}
	
	public void initFromFile(String beansConfig)
	throws Exception{
		try {
			DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
			XmlBeanDefinitionReader reader = 
				new XmlBeanDefinitionReader( factory );
			String[] paths = StringUtils.split(beansConfig,",",true,true);
			
			for ( int i = 0; i< paths.length; i++){
				log.debug("beans file path:" + paths[i] );
				reader.loadBeanDefinitions(
						new FileSystemResource( paths[i] )
				);
			}
			bc.setFactory( factory );	
		} catch (Exception ex) {
			ex.fillInStackTrace();
			log.error(ex, ex);
			throw new Exception("Unable to initialize Spring bean factory");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -