1. 程式人生 > >Spring再接觸 模擬Spring

Spring再接觸 模擬Spring

() tin getpass con upper java 耦合 clas ica

項目分層:

1.最土的方法是直接寫到main中去

2。分出model層

2.如下

技術分享圖片

4.在抽象一個對數據庫的訪問層(跨數據庫實現)

技術分享圖片

面向抽象編程

User.java

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; } }

UserService.java

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; } }

UserDAOimpl

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!"); } }

BeanFactory

package com.bjsxt.spring;

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

ClasspathxmlApplicationContext.java

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");//取名字為disk的所有元素
        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);
    }

}

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"/>   //指的是set方法
    </bean>
    
</beans>

testadd

    @Test
    public void testAdd() throws Exception {
        BeanFactory applicationContext = new ClassPathXmlApplicationContext();
        
        
        UserService service = (UserService)applicationContext.getBean("userService");   //模擬Spring自動幫裝配了
        
        //UserService service = new Userservice();
//Userdao userdao = new userdaoimpl(); 編程起來方便,低耦合 User u
= new User(); u.setUsername("zhangsan"); u.setPassword("zhangsan"); service.add(u); }

Spring再接觸 模擬Spring