SSH框架-》spring深度解耦01

今天开始框架spring学习,想法演变过程:https://download.csdn.net/download/weixin_44537122/11832415
此方法不是解耦不是特别完全,一步一步往深学!
在这里插入图片描述
spring深度解耦
在这里插入图片描述

package com.anbo.vo;

public class Car {
	private IEnginee en;

	public IEnginee getEn() {
		return en;
	}

	public void setEn(IEnginee en) {
		this.en = en;
	}
	
	public void run(){
		//null.start();//空指针异常
		en.start();
		System.out.println("小轿车开车...");
	}
	
}

package com.anbo.vo;

public class Enginee implements IEnginee{
	public void start(){
		System.out.println("v0发动机启动");
	}
}

package com.anbo.vo;

public class EngineeT implements IEnginee{
	public void start(){
		System.out.println("V8启动。。。。");
	}
}

package com.anbo.vo;

public interface IEnginee {
	public void start();
}

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	
	<!-- Enginee enn = new Enginee -->
	<bean id="enn" class="com.anbo.vo.Enginee">
	
	</bean>
	
	<bean id="enn1" class="com.anbo.vo.EngineeT">
	
	</bean>
	
	<!-- Car enn = new Car(); -->
	<bean id="car" class="com.anbo.vo.Car">
		<!-- 对象和对象的关系 -->
		<!-- 0.8启动 -->
		<!-- w26启动 -->
		<property name="en" ref="enn"></property>
	</bean>

</beans>



package com.anbo.spring;

public interface ApplicationContext {
	//获取对象
	public Object getBean(String name);
}

package com.anbo.spring;

import java.util.List;

public class BeanMessage {
	private String classname;
	private List<PropertyMessage> properties;
	public String getClassname() {
		return classname;
	}
	public void setClassname(String classname) {
		this.classname = classname;
	}
	public List<PropertyMessage> getProperties() {
		return properties;
	}
	public void setProperties(List<PropertyMessage> properties) {
		this.properties = properties;
	}
	
	
}

package com.anbo.spring;

public class PropertyMessage {
	//属性名
	private String propertyname;
	//引用的对象
	private String refname;
	public String getPropertyname() {
		return propertyname;
	}
	public void setPropertyname(String propertyname) {
		this.propertyname = propertyname;
	}
	public String getRefname() {
		return refname;
	}
	public void setRefname(String refname) {
		this.refname = refname;
	}
	
}

package com.anbo.spring;


import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/*
 * 解析 配置文件
 * 管理bean
 * 管理bean和bean之间的关系
 * 给用户提供对应的对象 
 * 
 */
public class ClassPathXmlApplicationContext 
			implements ApplicationContext {
	
	//spring使用map做了容器
	public static Map<String, BeanMessage> 
			beanmap=new HashMap<String, BeanMessage>();
	
	//在构造器中对配置文件进行解析
	public ClassPathXmlApplicationContext(String path){
		//开始解析xml文件
		InputStream in=this.getClass().getClassLoader().getResourceAsStream(path);
		SAXReader reader=new SAXReader();
		Document doc=null;
		try {
			doc=reader.read(in);
			Element beans=doc.getRootElement();
			List<Element> beanlist=beans.elements("bean");
			for (Element bean:beanlist) {//所有的bean
				//id解析
				String id=bean.attributeValue("id");
				//类名字解析
				String classname=bean.attributeValue("class");
				//所有的property解析
				List<PropertyMessage> properties=new ArrayList<PropertyMessage>();
				List<Element> propertys=bean.elements("property");
				for (Element el:propertys) {//bean中所有的properties
					PropertyMessage propertymessage=new PropertyMessage();
					propertymessage.setPropertyname(el.attributeValue("name"));
					propertymessage.setRefname(el.attributeValue("ref"));
					properties.add(propertymessage);
				}
				BeanMessage beanmessage=new BeanMessage();
				beanmessage.setClassname(classname);
				beanmessage.setProperties(properties);
				beanmap.put(id, beanmessage);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}		
	}
	/*
	 * 返回给客户的bean是已经创建好的
	 * 同时,属性已经设置完毕了的...
	 * @see com.anbo.spring.ApplicationContext#getBean(java.lang.String)
	 */
	public Object getBean(String name) {
		//通过名字得到类
		BeanMessage beanmessage=beanmap.get(name);
		Object obj=null;
		try {
			obj=Class.forName(beanmessage.getClassname()).newInstance();
			Class cla=obj.getClass();
			List<PropertyMessage> list=beanmessage.getProperties();
			//System.out.println(list.get(0).getPropertyname()+"  "+list.get(0).getRefname());
			for (PropertyMessage pro:list) {
				String fname=pro.getPropertyname();
				Field f=cla.getDeclaredField(fname);
				String methodname="set"+fname.substring(0,1).toUpperCase()+fname.substring(1);
				Method method=cla.getDeclaredMethod(methodname, f.getType());
				Object param=Class.forName(beanmap.get(pro.getRefname()).getClassname()).newInstance();
				method.invoke(obj, param);	
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值