这个类要在启动程序时候调用一下
package com.sinitek.sirm.common.spring;
import com.sinitek.base.starter.IStarter;
import com.sinitek.sirm.common.utils.URLUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.core.io.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* File Desc:
* Product Name: SIRM
* Module Name: BaseDase
* Author: 刘建
* History: 11-6-2 created by 刘建
*/
public class SpringContextStarter extends AbstractXmlApplicationContext implements IStarter {
private ApplicationContext appCtxt = null;
private Resource[] configResources;
public SpringContextStarter() {
}
public SpringContextStarter(Resource[] resources) {
super(null);
this.configResources = resources;
refresh();
}
protected Resource[] getConfigResources() {
return this.configResources;
}
public void start(Properties properties) throws Exception {
// 资源存放地
String _location = properties.getProperty("location");
int _len = _location.length();
try {
List _resources = new ArrayList();
ClassLoader _loader = Thread.currentThread().getContextClassLoader();
for (Enumeration _enum = _loader.getResources(_location); _enum.hasMoreElements();) {
URL _url = (URL) _enum.nextElement();
String _protocol = _url.getProtocol();
String _externalForm = URLUtils.toExternalForm(_url);
String _path = URLUtils.getPath(_url);
if (_protocol.equals("jar")) {
String _root = _location + "/";
int i = _path.indexOf('!');
String _prefix = _path.substring(0, i + 1);
ZipFile _file = new ZipFile(i >= 0 ? _path.substring(6, i) : _path);
for (Enumeration _enumeration = _file.entries(); _enumeration.hasMoreElements();) {
ZipEntry _entry = (ZipEntry) _enumeration.nextElement();
if (!_entry.isDirectory()) {
String _name = _entry.getName();
if (_name.startsWith(_root)) {
if (_name.endsWith(".xml")) {
_resources.add(new UrlResource(_externalForm + _name.substring(_len)));
}
}
}
}
} else if (_protocol.equals("file")) {
File[] _files = new File(_url.toURI()).listFiles();
for (int i = 0; i < _files.length; i++) {
File _file = _files[i];
if (!_file.isDirectory()) {
_resources.add(new FileSystemResource(_file));
}
}
}
}
appCtxt = new SpringContextStarter((Resource[]) _resources.toArray(new Resource[0]));
SpringFactory.setAppCtxt(appCtxt);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public void destroy() {
((AbstractApplicationContext) appCtxt).destroy();
}
}
工厂类:
package com.sinitek.sirm.common.spring;
import org.springframework.context.ApplicationContext;
import java.util.ArrayList;
import java.util.List;
/**
* File Desc:
* Product Name: SIRM
* Module Name: BaseDase
* Author: 刘建
* History: 11-6-2 created by 刘建
*/
public class SpringFactory {
private static ApplicationContext appCtxt = null;
public static <T> T getBean(Class<T> clazz) {
T _result = null;
if (appCtxt != null) {
String[] _names = appCtxt.getBeanNamesForType(clazz);
if (_names != null && _names.length > 0) {
_result = (T) appCtxt.getBean(_names[0]);
}
}
return _result;
}
public static <T> T getBean(String beanName, Class<T> clazz) {
return appCtxt != null ? (T) appCtxt.getBean(beanName, clazz) : null;
}
public static Object getBean(String beanName) {
return appCtxt != null ? appCtxt.getBean(beanName) : null;
}
public static <T> T[] getBeans(Class<T> clazz) {
T[] _result = null;
String[] _names = appCtxt.getBeanNamesForType(clazz);
if (_names != null) {
List<T> _list = new ArrayList<T>();
for (String _name : _names) {
_list.add(getBean(_name, clazz));
}
_result = (T[]) _list.toArray();
}
return _result;
}
public static void setAppCtxt(ApplicationContext appCtxt) {
SpringFactory.appCtxt = appCtxt;
}
}