首页 > 编程语言 > 详细

Spring 架构的简单模拟实现

时间:2014-05-25 01:45:16      阅读:589      评论:0      收藏:0      [点我收藏+]


  Spring 架构主要有两大特点: 

    IOC /DI: 控制反转/依赖注入 

   AOP  切面编程 .

  

    今天主要是实现IOC这一特点.


    主要的逻辑如下:

    User 用户类.

    UserDAO  User类的访问接口. 

    UserDAOImpl  实现UserDAO 接口以实现拓展. 

    UserService  包含一系列的用户功能 ,如addUser


   关于以上类的代码:

   

package com.bjsxt.model;

public class User {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

package com.bjsxt.dao;
import com.bjsxt.model.User;


public interface UserDAO {
	public void save(User user);
}

package com.bjsxt.dao.impl;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;


public class UserDAOImpl implements UserDAO {

	public void save(User user) {
		//Hibernate
		//JDBC
		//XML
		//NetWork
		System.out.println("user saved!");
	}

}

package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;



public class UserService {
	private UserDAO userDAO;  
	public void add(User user) {
		userDAO.save(user);
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}

然后运行test代码

import com.bjsxt.dao.UserDAO;
import com.bjsxt.dao.impl.UserDAOImpl;
import com.bjsxt.model.User;
import com.bjsxt.service.UserService;
import com.bjsxt.spring.BeanFactory;
import com.bjsxt.spring.ClassPathXmlApplicationContext;


public class Test {

	public static void main(String[] args)throws Exception{
		
		
		UserService service =new UserService();
		UserDAO userdao =new UserDAOImpl();
		service.setUserDAO(userdao);
		User u =new User();
		service.add(u);
	}
}

注意这里直接用UserDAOImpl() 来代替UserDAO , 也是体现面向对象的灵活性.  比如可以针对不同的数据库设计不同的UserDAOImpl(). 


为了实现IOC 的装配功能, 比如说我不想在 代码中体现new 的是哪种UserDAO的实现, 而是直接在配置文件中进行设置.


配置文件beans.xml如下:

  
<beans>
	<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" />
	
	<bean id="userService" class="com.bjsxt.service.UserService" >
		<property name="userDAO" bean="u"/>
	</bean>
	
</beans>


这里可以根据bean 的id  userService  找到 所属的UserService类, 在属性userDAO中  ,设置其为id 为 "u"的bean . 而上述文件中的 id 为"u"的bean , 就是UserDAOImp .


当然, 需要借助Factory 接口 和该接口的实现类 ClassPathXmlApplicationContext .


package com.bjsxt.spring;

public interface BeanFactory {
	public Object getBean(String id);
}

package com.bjsxt.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>();
	
	
	//IOC Inverse of Control DI Dependency Injection
	public ClassPathXmlApplicationContext() throws Exception {
		SAXBuilder sb=new SAXBuilder();
	    
	    Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象
	    Element root=doc.getRootElement(); //获取根元素HD
	    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");
	       Object o = Class.forName(clazz).newInstance();
	       System.out.println(id);
	       System.out.println(clazz);
	       beans.put(id, o);
	       
	       for(Element propertyElement : (List<Element>)element.getChildren("property")) {
	    	   String name = propertyElement.getAttributeValue("name"); //userDAO
	    	   String bean = propertyElement.getAttributeValue("bean"); //u
	    	   Object beanObject = beans.get(bean);//UserDAOImpl instance
	    	   
	    	   String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
	    	   System.out.println("method name = " + methodName);
	    	   
	    	   Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
	    	   m.invoke(o, beanObject);
	       }
	       
	       
	    }  
	  
	}



	public Object getBean(String id) {
		return beans.get(id);
	}

}


ClassPathXmlApplicationContext 利用反射机制, 在构造函数中根据xml文件内容实现装配功能.

新的test函数:

import com.bjsxt.dao.UserDAO;
import com.bjsxt.dao.impl.UserDAOImpl;
import com.bjsxt.model.User;
import com.bjsxt.service.UserService;
import com.bjsxt.spring.BeanFactory;
import com.bjsxt.spring.ClassPathXmlApplicationContext;


public class Test {

	public static void main(String[] args)throws Exception{
		
		BeanFactory factory =new ClassPathXmlApplicationContext();
		
		UserService service =(UserService)factory.getBean("userService");
	//	UserDAO userdao =new UserDAOImpl();
	//	service.setUserDAO(userdao);
		User u =new User();
		service.add(u);
	}
}



Spring 架构的简单模拟实现,布布扣,bubuko.com

Spring 架构的简单模拟实现

原文:http://blog.csdn.net/ltianchao/article/details/26586831

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!