Spring_加载beans.xml简单模拟
2015-12-15 16:11
393 查看
使用spring框架时,都会在xml中配置一些bean。这样spring就会把我们配置的组件进行管理,让我们用起来很方便,但是一直想知道它到底是怎么一回事。上网查找资料,用jdom模拟解析xml。
beans.xml
面向接口编程:
BeanFactory.java
ClassPathXmlApplicationContext.java
testParse.java
beans.xml
<beans> <bean id="u" class="com.code.dao.impl.UserDAOImpl" /> <bean id="userService" class="com.code.service.UserService" > <property name="userDAO" bean="u"/> </bean> </beans>
面向接口编程:
BeanFactory.java
package com.code.spring; public interface BeanFactory { public Object getBean(String id); }
ClassPathXmlApplicationContext.java
package com.code.spring; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class ClassPathXmlApplicationContext implements BeanFactory{ <span style="white-space:pre"> </span>HashMap<String, Object> beans = new HashMap<String, Object>(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public ClassPathXmlApplicationContext() throws Exception { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>SAXBuilder builder = new SAXBuilder(); <span style="white-space:pre"> </span>Document document = builder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); <span style="white-space:pre"> </span>Element root = document.getRootElement(); <span style="white-space:pre"> </span>List<Element> list = root.getChildren("bean"); <span style="white-space:pre"> </span>for (int i = 0; i < list.size(); i++) { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>Element element = list.get(i); <span style="white-space:pre"> </span>String id = element.getAttributeValue("id"); <span style="white-space:pre"> </span>String clazz = element.getAttributeValue("class"); <span style="white-space:pre"> </span>Object o = Class.forName(clazz).newInstance(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>System.out.println("====id==========> " + id); <span style="white-space:pre"> </span>System.out.println("====clazz=======> " + clazz); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>beans.put(id, o); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>for (Element propertyEl : (List<Element>)element.getChildren("property")) { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>String name = propertyEl.getAttributeValue("name"); <span style="white-space:pre"> </span>String bean = propertyEl.getAttributeValue("bean"); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>System.out.println("====name=========> " + name); <span style="white-space:pre"> </span>System.out.println("====bean=========> " + bean); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>Object objBean = beans.get(bean); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); <span style="white-space:pre"> </span>System.out.println("====method name===> " + methodName); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>Method m = o.getClass().getMethod(methodName, objBean.getClass().getInterfaces()[0]); <span style="white-space:pre"> </span>m.invoke(o, objBean); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public Object getBean(String id) { <span style="white-space:pre"> </span>return beans.get(id); <span style="white-space:pre"> </span>} }
testParse.java
import com.code.dao.UserDAO; import com.code.model.User; import com.code.spring.BeanFactory; import com.code.spring.ClassPathXmlApplicationContext; public class TestPrase { public static void main(String[] args) throws Exception { User user = new User(); user.setUsername("admin"); user.setPassword("password"); BeanFactory beanFactory = new ClassPathXmlApplicationContext(); UserDAO userDAO = (UserDAO) beanFactory.getBean("u"); userDAO.save(user); System.out.println(userDAO); } }
相关文章推荐
- eclipse下JSP环境的配置
- 轻量级JavaEE企业应用实战(三)
- Spring AOP 5种切面方式的应用(顶)
- spring AOP @AfterThrowing没有返回值的原因
- 关于myeclipse+maven(svn)的原本正确项目报红叉问题
- java中的@retention and @interface
- MyBatis学习七:spring和MyBatis整合、逆向工程
- Spring jdbcTemplat插入,查询
- java.lang.IllegalStateException: getOutputStream() has already been called for this response解决方案
- Java创建多线程的几种方法
- spring配置文件详解--真的蛮详细
- java关键字
- Java静态内部类的实例化问题
- java 接口和抽象
- Jdk1.8的安装
- Java国际化和Spring国际化比较
- 初识struts
- 使用Java匿名类直接实例化接口
- MyEclipse has detected that less than 5% of the 22MB of PS Survivor Space (Heap memory) space remain
- java之JDBC相关整理