Spring再接触 模拟Spring

Posted Blueburble

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring再接触 模拟Spring相关的知识,希望对你有一定的参考价值。

项目分层:

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的主要内容,如果未能解决你的问题,请参考以下文章

Spring再接触 自动装配

Spring再接触 生命周期

Spring再接触 集合注入

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

Spring再接触 Scope范围

Spring再接触 注入类型