package com.natureframework.bean;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.natureframework.bean.exception.NotFoundBeanException;
import com.natureframework.bean.lang.Bean;
import com.natureframework.bean.lang.Context;
import com.natureframework.bean.lang.Property;
/**
* 应用程序Bean Context配置信息类,在此类中将完成配置的信息读取、调度、解析、初始实体Bean、并完成必要的依赖注入; 此类依赖Digester
* XML解析工具类,如XML中有新元素要修改、添加,请在com.natureframework.bean.lang.Context类
* 修改相应的解析规则,目前此类只支持Bean的组件初始化及依赖注入,如需要增连接池、AOP等其它功能需要完善...【Bond 2014/3/31】
*
* @author Bond
*
*/
public class ApplicationContext {
private static Map<String, Bean> contexts = new HashMap<String, Bean>();
private static Map<String, Object> beans = new HashMap<String, Object>();
private static boolean isInit = false;
public ApplicationContext() {
}
/**
* 设置配置并完成初始化
*
* @param configs
*/
public void configration(String[] configs) {
synchronized (configs) {
if (!isInit) {
for (String config : configs) {
Context ctx = Context.newInstance(config);
contexts.putAll(ctx.getBeans());
}
initialize();
isInit = true;
}
}
}
/**
* 获取Bean容器的Bean组件,如为原型,则根据配置信息完成Bean组件初始化
*
* @param beanId
* Bean的ID属性
* @return
*/
public Object getBean(String beanId) {
Bean bean = contexts.get(beanId);
try {
if (bean == null) {
throw new NotFoundBeanException(beanId);
}
if (!bean.getScope().equals("prototype")) {
return beans.get(beanId);
} else {
return createBean(bean);
}
} catch (NotFoundBeanException e) {
e.printStackTrace();
}
return null;
}
/**
* 初始化Bean context的内容
*/
private void initialize() {
Set<String> keysets = contexts.keySet();
for (String key : keysets) {
Bean bean = contexts.get(key);
beans.put(bean.getId(), initializeBean(bean));
}
}
/**
* 创建相应对象并完成Bean组件的依赖注入,此方法只用于初始化时之用
*
* @param bean
* 相应的Bean配置信息
* @return
*/
private Object initializeBean(Bean bean) {
try {
// 如果是原型不进初始化
if (bean.getScope().equals("prototype")) {
return null;
}
Class<?> clz = Class.forName(bean.getClassName());
Object object = beans.get(bean.getId());
if (object == null) {
object = clz.newInstance();
}
for (Property property : bean.getProperties()) {
if (property.getRef() == null) {
throw new NotFoundBeanException(property.getRef());
}
Object sub = beans.get(property.getRef());
Bean subBean = contexts.get(property.getRef());
if (sub == null) {
sub = initializeBean(subBean);
beans.put(property.getRef(), sub);
}
String methodName = property.getName();
methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = clz.getMethod(methodName, sub.getClass());
method.invoke(object, sub);
}
return object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 创建相应对象并完成Bean组件的依赖注入,此方法用于当Bean组件的scope属性为prototype时,在高层代码获取Bean组件时才用
*
* @param bean
* 相应的Bean配置信息
* @return
*/
private Object createBean(Bean bean) {
try {
if (!bean.getScope().equals("prototype")) {
return beans.get(bean.getId());
}
Class<?> clz = Class.forName(bean.getClassName());
Object object = clz.newInstance();
for (Property property : bean.getProperties()) {
if (property.getRef() == null) {
throw new NotFoundBeanException(property.getRef());
}
Bean subBean = contexts.get(property.getRef());
Object sub = createBean(subBean);
String methodName = property.getName();
methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = clz.getMethod(methodName, sub.getClass());
method.invoke(object, sub);
}
return object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.natureframework.bean.exception.NotFoundBeanException;
import com.natureframework.bean.lang.Bean;
import com.natureframework.bean.lang.Context;
import com.natureframework.bean.lang.Property;
/**
* 应用程序Bean Context配置信息类,在此类中将完成配置的信息读取、调度、解析、初始实体Bean、并完成必要的依赖注入; 此类依赖Digester
* XML解析工具类,如XML中有新元素要修改、添加,请在com.natureframework.bean.lang.Context类
* 修改相应的解析规则,目前此类只支持Bean的组件初始化及依赖注入,如需要增连接池、AOP等其它功能需要完善...【Bond 2014/3/31】
*
* @author Bond
*
*/
public class ApplicationContext {
private static Map<String, Bean> contexts = new HashMap<String, Bean>();
private static Map<String, Object> beans = new HashMap<String, Object>();
private static boolean isInit = false;
public ApplicationContext() {
}
/**
* 设置配置并完成初始化
*
* @param configs
*/
public void configration(String[] configs) {
synchronized (configs) {
if (!isInit) {
for (String config : configs) {
Context ctx = Context.newInstance(config);
contexts.putAll(ctx.getBeans());
}
initialize();
isInit = true;
}
}
}
/**
* 获取Bean容器的Bean组件,如为原型,则根据配置信息完成Bean组件初始化
*
* @param beanId
* Bean的ID属性
* @return
*/
public Object getBean(String beanId) {
Bean bean = contexts.get(beanId);
try {
if (bean == null) {
throw new NotFoundBeanException(beanId);
}
if (!bean.getScope().equals("prototype")) {
return beans.get(beanId);
} else {
return createBean(bean);
}
} catch (NotFoundBeanException e) {
e.printStackTrace();
}
return null;
}
/**
* 初始化Bean context的内容
*/
private void initialize() {
Set<String> keysets = contexts.keySet();
for (String key : keysets) {
Bean bean = contexts.get(key);
beans.put(bean.getId(), initializeBean(bean));
}
}
/**
* 创建相应对象并完成Bean组件的依赖注入,此方法只用于初始化时之用
*
* @param bean
* 相应的Bean配置信息
* @return
*/
private Object initializeBean(Bean bean) {
try {
// 如果是原型不进初始化
if (bean.getScope().equals("prototype")) {
return null;
}
Class<?> clz = Class.forName(bean.getClassName());
Object object = beans.get(bean.getId());
if (object == null) {
object = clz.newInstance();
}
for (Property property : bean.getProperties()) {
if (property.getRef() == null) {
throw new NotFoundBeanException(property.getRef());
}
Object sub = beans.get(property.getRef());
Bean subBean = contexts.get(property.getRef());
if (sub == null) {
sub = initializeBean(subBean);
beans.put(property.getRef(), sub);
}
String methodName = property.getName();
methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = clz.getMethod(methodName, sub.getClass());
method.invoke(object, sub);
}
return object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 创建相应对象并完成Bean组件的依赖注入,此方法用于当Bean组件的scope属性为prototype时,在高层代码获取Bean组件时才用
*
* @param bean
* 相应的Bean配置信息
* @return
*/
private Object createBean(Bean bean) {
try {
if (!bean.getScope().equals("prototype")) {
return beans.get(bean.getId());
}
Class<?> clz = Class.forName(bean.getClassName());
Object object = clz.newInstance();
for (Property property : bean.getProperties()) {
if (property.getRef() == null) {
throw new NotFoundBeanException(property.getRef());
}
Bean subBean = contexts.get(property.getRef());
Object sub = createBean(subBean);
String methodName = property.getName();
methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = clz.getMethod(methodName, sub.getClass());
method.invoke(object, sub);
}
return object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}