模拟 spring IOC

本文介绍了一个简单的Spring IOC容器实现过程,包括定义DAO层接口及其实现类、Service层接口及其实现类,并通过XML配置文件完成Bean的装配,最终演示了如何通过IOC容器获取并使用Service层对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

package com.dao;

import com.entry.Student;

public interface StudentDao {
    void add(Student student);
}


----------
package com.dao;

import com.entry.Student;

public class StudentDaoImpl implements StudentDao {

    @Override
    public void add(Student student) {
        System.out.println(student.getId() + "----" + student.getName());
    }

}


----------
package com.entry;

public class Student {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


----------
package com.service;

import com.entry.Student;

public interface StudentService {
    void add(Student student);
}


----------
package com.service;

import com.dao.StudentDao;
import com.entry.Student;

public class StudentServiceImpl implements StudentService {

    private StudentDao stuDao;

    public StudentDao getStuDao() {
        return stuDao;
    }

    public void setStuDao(StudentDao stuDao) {
        this.stuDao = stuDao;
    }

    @Override
    public void add(Student student) {
        System.out.println("student was saved!");
        stuDao.add(student);
    }

}


----------
package com.spring.ioc;
/**
*只是为了模拟spring,本程序中此接口毫无意义
*/
public interface BeanFactory {
    Object getBean(String id);
}


----------
package com.spring.ioc;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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.JDOMException;
import org.jdom.input.SAXBuilder;

public class ClassPathXmlApplicationContext implements BeanFactory {
    private Map<String, Object> beans = new HashMap<String, Object>();

    @SuppressWarnings("unchecked")
    public ClassPathXmlApplicationContext() throws JDOMException, IOException,
            ClassNotFoundException, InstantiationException,
            IllegalAccessException, NoSuchMethodException, SecurityException,
            IllegalArgumentException, InvocationTargetException {
        // 1.读取配置文件,生成docment对象
        SAXBuilder sb = new SAXBuilder();
        Document doc = sb.build(this.getClass().getClassLoader()
                .getResourceAsStream("beans.xml"));
        Element root = doc.getRootElement();
        List<Element> list = root.getChildren("bean");

        // 2.将对象实例化放入容器
        for (int i = 0; i < list.size(); i++) {
            Element ele = list.get(i);
            String id = ele.getAttributeValue("id");
            String clazz = ele.getAttributeValue("class");
            Object obj = Class.forName(clazz).newInstance();
            beans.put(id, obj);

            // 3.遍历property注入对象
            for (Element property : (List<Element>) ele.getChildren("property")) {
                String name = property.getAttributeValue("name");
                String bean = property.getAttributeValue("bean");
                Object objSet = beans.get(bean);

                String methodName = "set" + name.substring(0, 1).toUpperCase()
                        + name.substring(1);
                Method m = obj.getClass().getMethod(methodName,
                        objSet.getClass().getInterfaces()[0]);
                m.invoke(obj, objSet);
            }

        }
    }

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

}


----------
package com.test;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.jdom.JDOMException;
import org.junit.Test;

import com.entry.Student;
import com.service.StudentService;
import com.spring.ioc.ClassPathXmlApplicationContext;

public class TestClass {

    @Test
    public void testIOC() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            NoSuchMethodException, SecurityException, IllegalArgumentException,
            InvocationTargetException, JDOMException, IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
        StudentService service = (StudentService) context.getBean("stuService");
        Student student = new Student();
        student.setId(1);
        student.setName("tt");
        service.add(student);
    }
}


----------


<beans>    
    <bean id="stuDao" class="com.dao.StudentDaoImpl" />    
    <bean id="stuService" class="com.service.StudentServiceImpl" >    
        <property name="stuDao" bean="stuDao"/>    
    </bean>    
</beans> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值