spring 学习笔记(1)--模拟spring

1.spring配置文件 beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<bean id="u" class="org.sh.spring.impl.IUserDAOImpl"></bean>
	<bean id="userservice" class="org.sh.spring.Services.UserServices">
		<property name="impl" bean="u"></property>
	</bean>
</beans>

可以简单理解为将要new的对象 放在xml配置文件中 .然后从配置问价中取出来,注入。

userservice 中有个impl属性,说白了有一个setImp()方法,property 就说明类中有一个setName 的方法,该属性(impl)就是org.sh.spring.Services.UserServices

2.模拟ClassPathXmlApplicationContext

package org.sh.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ClassPathXmlApplicationContext implements BeanFactory {
	private Map<String,Object> beans= new HashMap<String , Object>();
	public ClassPathXmlApplicationContext()throws Exception{
		SAXBuilder sb = new SAXBuilder();
		Document doc = sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml"));
		Element root = doc.getRootElement();//得到根元素
		List list = root.getChildren("bean");//得到所有名字为bean的节点
		for(int i = 0;i<list.size();i++){
			Element element = (Element) list.get(i);
			String id = element.getAttributeValue("id");
			String clazz = element.getAttributeValue("class");
			System.out.println("**********"+id+":"+clazz);
			Object o = Class.forName(clazz).newInstance();
			beans.put(id, o);
			for(Element propertyElement : (List<Element>)element.getChildren("property")){
				String name = propertyElement.getAttributeValue("name");
				String bean = propertyElement.getAttributeValue("bean");
				Object beanObject = beans.get(bean);
				String methodName = "set"+name.substring(0,1).toUpperCase()+name.substring(1);//构造set方法
				System.out.println("methodName:"+methodName);
				Method m = o.getClass().getMethod(methodName,beanObject.getClass().getInterfaces()[0]);//setImpl(IUserDAO.class)
				m.invoke(o,beanObject);
			}
		}
	}
	@Override
	public Object getBean(String name) {
		return beans.get(name);
	}
	
}

用sax解析 和反射 从配置文件中取出实例,然后注入。这样就不用在中new对象

3.测试用例

package org.sh.spring.Services.Test;

import static org.junit.Assert.*;

import java.lang.reflect.Proxy;

import org.junit.Before;
import org.junit.Test;
import org.sh.aop.LogInterceptor;
import org.sh.spring.BeanFactory;
import org.sh.spring.ClassPathXmlApplicationContext;
import org.sh.spring.DAO.IUserDAO;
import org.sh.spring.Services.UserServices;
import org.sh.spring.impl.IUserDAOImpl;
import org.sh.spring.model.User;

public class UserServicesTest {

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testSave() {
		BeanFactory factory = null;
		try {
			factory = new ClassPathXmlApplicationContext();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		IUserDAO service = (IUserDAO)factory.getBean("userservice");
		User user = new User();
		user.setName("suhao");
		user.setPassword("12345678");
		service.save(user);
	}
	@Test
	public void proxy(){
		IUserDAO dao = new IUserDAOImpl();
		LogInterceptor li = new LogInterceptor();
		li.setTarget(dao);
		IUserDAO userdaoProxy = (IUserDAO)Proxy.newProxyInstance(IUserDAO.class.getClassLoader(),new Class[]{IUserDAO.class},li);
		userdaoProxy.save(new User());
		userdaoProxy.delete();
		}
}
结果:

**********userservice:org.sh.spring.Services.UserServices
**********这里
methodName:setImpl
user saved
测试成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值