Bean组件管理浅谈

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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值