com.qa.framework.ioc.IocContainer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smart-ui-framework Show documentation
Show all versions of smart-ui-framework Show documentation
Support web and moblie automaton based on selenium and appium
package com.qa.framework.ioc;
import com.qa.framework.ioc.annotation.Page;
import com.qa.framework.ioc.annotation.Service;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by kcgw001 on 2016/4/13.
*/
public class IocContainer {
private static final Logger logger = Logger.getLogger(IocContainer.class);
private static final Map, Object> container = new HashMap, Object>();
static {
try {
// 获取应用包路径下所有的类
List> classList = ClassFinder.getClassList();
for (Class cls : classList) {
if (cls.isAnnotationPresent(Service.class)
|| cls.isAnnotationPresent(Page.class)) {
Object instance = cls.newInstance();
container.put(cls, instance);
logger.info("Add the class " + cls.getName() + " ioc container");
}
}
} catch (Exception e) {
throw new RuntimeException("初始化类出错!" + e.getCause());
}
}
/**
* Gets ioc map.
*
* @return the ioc map
*/
public static Map, Object> getIocMap() {
return container;
}
/**
* Gets ioc object.
*
* @param the type parameter
* @param cls the cls
* @return the ioc object
*/
@SuppressWarnings("unchecked")
public static T getIocObject(Class cls) {
if (!container.containsKey(cls)) {
return null;
}
return (T) container.get(cls);
}
/**
* Sets ioc object.
*
* @param cls the cls
* @param obj the obj
*/
public static void setIocObject(Class cls, Object obj) {
container.put(cls, obj);
}
}