自定义java类实现类似spring的依赖注入功能
来源:互联网 发布:怎样打开Windows目录 编辑:程序博客网 时间:2024/05/16 15:58
package com.zhjjie.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.io.Resource;
import com.zhjjie.action.StudentAction;
/**
* @author ; Administrator
* @Description : TODO
* @CreateDate ; Aug 21, 2012 8:56:56 PM
* @lastModified ; Aug 21, 2012 8:56:56 PM
* @version ; 1.0
*/
public class MyXmlApplicationContextUtil implements ApplicationContext {
//保存容器中所有单例模式的bean
private Map<String,Object> objPool = Collections.synchronizedMap(new HashMap<String,Object>());
//保存配置文件中对应的文档对象
private Document doc;
//保存配置文件里面的根元素
private Element root;
/**
*
*/
public MyXmlApplicationContextUtil() {
super();
}
/**
*
*/
public MyXmlApplicationContextUtil(String filePath)throws Exception{
SAXReader reader = new SAXReader();
doc = reader.read(new File(filePath));
root = doc.getRootElement();
initPool();
initProp();
}
public Object getBean(String name) throws BeansException {
Object target = objPool.get(name);
Object obj = null;
if(target.getClass() != String.class){
obj = target;
}else{
String clazz = (String)target;
try{
obj = Class.forName(clazz).newInstance();
}catch(Exception e){
e.printStackTrace();
}
}
return obj;
}
private void initPool()throws Exception{
for(Object obj:root.elements()){//遍历每个bean元素
Element ele = (Element)obj;
String beanId = ele.attributeValue("id");
String beanClazz = ele.attributeValue("class");
String beanScope = ele.attributeValue("scope");
if(beanScope == null || beanScope.equals("singleton")){
objPool.put(beanId, Class.forName(beanClazz).newInstance());
}else{
objPool.put(beanId, beanClazz);
}
}
}
private void initProp()throws Exception{
for(Object obj:root.elements()){//遍历每个bean元素
Element ele = (Element)obj;
String beanId = ele.attributeValue("id");
String beanScope = ele.attributeValue("scope");
if(beanScope == null || beanScope.equals("beanScope")){
Object bean = objPool.get(beanId);
for(Object prop:ele.elements()){
Element propElem = (Element)prop;
String propName = propElem.attributeValue("name");
String propVal = propElem.attributeValue("value");
String propRef = propElem.attributeValue("ref");
String propNameOfFirstLetter = propName.substring(0,1).toUpperCase()+propName.substring(1);
if(propVal != null && propVal.length()>0){
Method setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, String.class);
setter.invoke(bean, propVal);
}
if(propRef !=null && propRef.length()>0){
Object target = objPool.get(propRef);
if(target == null){};
Method setter = null;
for(Class superInterface:target.getClass().getInterfaces()){
try{
setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, superInterface);
setter.invoke(bean, propVal);
break;
}catch(NoSuchMethodException e){
continue;
}
}
if(setter == null){
setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, target.getClass());
}
setter.invoke(bean, propVal);
}
}
}else{
//objPool.put(beanId, beanClazz);
}
}
}
public static void main(String[] args)throws Exception{
ApplicationContext ac = new MyXmlApplicationContextUtil("D:\\Myeclipse6.5\\webBookShop\\WebRoot\\bean.xml");
StudentAction s = (StudentAction)ac.getBean("StudentAction");
System.out.println(s);
}
}
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.io.Resource;
import com.zhjjie.action.StudentAction;
/**
* @author ; Administrator
* @Description : TODO
* @CreateDate ; Aug 21, 2012 8:56:56 PM
* @lastModified ; Aug 21, 2012 8:56:56 PM
* @version ; 1.0
*/
public class MyXmlApplicationContextUtil implements ApplicationContext {
//保存容器中所有单例模式的bean
private Map<String,Object> objPool = Collections.synchronizedMap(new HashMap<String,Object>());
//保存配置文件中对应的文档对象
private Document doc;
//保存配置文件里面的根元素
private Element root;
/**
*
*/
public MyXmlApplicationContextUtil() {
super();
}
/**
*
*/
public MyXmlApplicationContextUtil(String filePath)throws Exception{
SAXReader reader = new SAXReader();
doc = reader.read(new File(filePath));
root = doc.getRootElement();
initPool();
initProp();
}
public Object getBean(String name) throws BeansException {
Object target = objPool.get(name);
Object obj = null;
if(target.getClass() != String.class){
obj = target;
}else{
String clazz = (String)target;
try{
obj = Class.forName(clazz).newInstance();
}catch(Exception e){
e.printStackTrace();
}
}
return obj;
}
private void initPool()throws Exception{
for(Object obj:root.elements()){//遍历每个bean元素
Element ele = (Element)obj;
String beanId = ele.attributeValue("id");
String beanClazz = ele.attributeValue("class");
String beanScope = ele.attributeValue("scope");
if(beanScope == null || beanScope.equals("singleton")){
objPool.put(beanId, Class.forName(beanClazz).newInstance());
}else{
objPool.put(beanId, beanClazz);
}
}
}
private void initProp()throws Exception{
for(Object obj:root.elements()){//遍历每个bean元素
Element ele = (Element)obj;
String beanId = ele.attributeValue("id");
String beanScope = ele.attributeValue("scope");
if(beanScope == null || beanScope.equals("beanScope")){
Object bean = objPool.get(beanId);
for(Object prop:ele.elements()){
Element propElem = (Element)prop;
String propName = propElem.attributeValue("name");
String propVal = propElem.attributeValue("value");
String propRef = propElem.attributeValue("ref");
String propNameOfFirstLetter = propName.substring(0,1).toUpperCase()+propName.substring(1);
if(propVal != null && propVal.length()>0){
Method setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, String.class);
setter.invoke(bean, propVal);
}
if(propRef !=null && propRef.length()>0){
Object target = objPool.get(propRef);
if(target == null){};
Method setter = null;
for(Class superInterface:target.getClass().getInterfaces()){
try{
setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, superInterface);
setter.invoke(bean, propVal);
break;
}catch(NoSuchMethodException e){
continue;
}
}
if(setter == null){
setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, target.getClass());
}
setter.invoke(bean, propVal);
}
}
}else{
//objPool.put(beanId, beanClazz);
}
}
}
public static void main(String[] args)throws Exception{
ApplicationContext ac = new MyXmlApplicationContextUtil("D:\\Myeclipse6.5\\webBookShop\\WebRoot\\bean.xml");
StudentAction s = (StudentAction)ac.getBean("StudentAction");
System.out.println(s);
}
}
- 自定义java类实现类似spring的依赖注入功能
- 实现Spring依赖注入功能的想法
- 模拟spring依赖注入,自定义注入类
- spring-依赖注入的实现
- java模拟spring实现依赖注入
- Spring IoC依赖注入的实现
- spring依赖注入的实现原理
- spring依赖注入的实现原理
- spring依赖注入的实现原理
- spring是怎么实现依赖注入的
- Spring基于annotation的依赖注入实现
- spring DI 实现的模拟,依赖注入
- spring-依赖注入的实现方式
- 自定义Dialog实现类似Toast的功能
- java简单模拟Spring的依赖注入
- spring接口实现类 依赖注入
- spring 的依赖注入
- Spring的依赖注入
- commons-fileupload介绍
- HDU 1690 Bus System
- poj 3237 加强版 query on a tree 树链剖分
- POJ 1755 Triathlon【半平面交】
- Webkit中HTML5 Video的实现分析 (四) - 视频加载及播放的时序图
- 自定义java类实现类似spring的依赖注入功能
- ext4 date 汉化
- 开源项目管理软件redmine-什么时候用,怎么用?(二)
- Receiver XXX type for instance message is a forward declaration
- C++学习:一个简单适用的跨平台Log类(1)
- Vim+cscope+ctags+tags阅读源代码
- 我的java笔记(集合)
- 我要录视频-XML(一)
- 面向对象—在线文件管理模块