- 浏览: 3436220 次
- 性别:
- 来自: China
文章分类
- 全部博客 (536)
- ajax (1)
- Algorithm (14)
- Android (40)
- CSS/HTML... (2)
- defy (3)
- DesignPattern (2)
- dorado (0)
- Drools (6)
- English/日本語 (7)
- Flex (2)
- Framework (0)
- Google (3)
- hibernate (13)
- homework (3)
- HTML5 (0)
- IDE (29)
- java (45)
- javaee (7)
- Javascript (14)
- java组件 (5)
- jQuery (4)
- jsp (8)
- jsf (2)
- Linux (2)
- lucene (0)
- mysql (6)
- news (3)
- Oracle (8)
- other (4)
- PHP (5)
- Python (0)
- Software Engineering (3)
- spring (7)
- struts1.x (14)
- struts2.x (14)
- strolling in cloud (1)
- subject:javaEnhance (20)
- Tomcat (7)
- validator (3)
- 学习·方法·心得 (8)
- .NET (2)
- vba (6)
- groovy (5)
- grails (2)
- SWT (0)
- big data (1)
- perl (1)
- objective-c (50)
- product (1)
- mac (7)
- ios (188)
- ios-phone (2)
- ios-system (15)
- ios-network (5)
- ios-file (4)
- ios-db (1)
- ios-media (3)
- ios-ui (27)
- ios-openSource (6)
- ios-animation (5)
- ios-drawing (7)
- c (2)
- ios-app (2)
- ios-course (15)
- ios-runtime (14)
- ios-code (8)
- ios-thread (8)
- ios-LBS (2)
- ios-issue (1)
- ios-design (2)
- Jailbreak (2)
- cocos2d (0)
- swift (16)
- ios-framework (4)
- apple watch (4)
- ios-web (1)
- react native (3)
- TVOS (1)
- OpenGL (1)
最新评论
-
xiaobinggg:
...
Session机制详解 -
菜鸟学生会:
Drools规则工作流引擎开发教程网盘地址:http://pa ...
Drools入门-----------环境搭建,分析Helloworld -
wangyudong:
不是很好用,不支持自动化测试RESTful API,也不支持自 ...
Simple REST Client POST使用方法 -
Paul0523:
很棒的一篇文章,感谢楼主分享
Session机制详解 -
啸笑天:
获取原型对象的三种方法<script>functi ...
复习JavaScript面向对象技术
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personDao" class="junit.test12.PersonDaoBean"/> <bean id="personService" class="junit.test12.PersonServiceBean"> <property name="id" value="24"/> <property name="name" value="zyj"/> <!-- <property name="personDao" ref="personDao"/> --> </bean> </beans>
package junit.test12; import java.util.ArrayList; import java.util.List; /** * xml中的<bean/>的定义 * @author Administrator * */ public class BeanDefinition { private String id; private String clazz; private List<PropertyDefinition> propertyDefinitions=new ArrayList<PropertyDefinition>(); public BeanDefinition(String id, String clazz) { super(); this.id = id; this.clazz = clazz; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClazz() { return clazz; } public void setClazz(String clazz) { this.clazz = clazz; } public List<PropertyDefinition> getPropertyDefinitions() { return propertyDefinitions; } public void setPropertyDefinitions(List<PropertyDefinition> propertyDefinitions) { this.propertyDefinitions = propertyDefinitions; } }
package junit.test12; public class PropertyDefinition { private String name; private String ref; private String value; public PropertyDefinition(String name, String ref,String value) { super(); this.name = name; this.ref = ref; this.value=value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
package junit.test12; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD,ElementType.METHOD}) public @interface UserDefinedResource { public String name() default ""; }
//使用dom4j读取spring配置文件
package junit.test12; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import junit.test12.BeanDefinition; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; /** * 自定义容器 * @author Administrator * */ public class UserDefinedClassPathXMLApplicationContext { private List<BeanDefinition> beanDefinitions=new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons=new HashMap<String, Object>(); public UserDefinedClassPathXMLApplicationContext(String filename){ this.readXML(filename); this.instanceBeans(); this.annotationInject(); this.injectObject(); } /** * 读取xml配置文件 * @param filename */ private void readXML(String filename){ SAXReader saxReader=new SAXReader(); Document document=null; try { URL xmlPath=this.getClass().getClassLoader().getResource(filename); document=saxReader.read(xmlPath); XPath xPath=document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径。从根路径开始 Map<String, String> nsMap=new HashMap<String, String>(); nsMap.put("ns", "http://www.springframework.org/schema/beans");//加入命名空间 xPath.setNamespaceURIs(nsMap);//设置命名空间 List<Element> beans=xPath.selectNodes(document);//获取文档下所有bean节点 for (Element element : beans) { String id=element.attributeValue("id"); String clazz=element.attributeValue("class"); BeanDefinition beanDefinition=new BeanDefinition(id, clazz); XPath xPath2=element.createXPath("ns:property");//从相对路径开始 xPath2.setNamespaceURIs(nsMap); List<Element> propertys=xPath2.selectNodes(element); for (Element element2 : propertys) { String name=element2.attributeValue("name"); String ref=element2.attributeValue("ref"); String value=element2.attributeValue("value"); PropertyDefinition propertyDefinition=new PropertyDefinition(name, ref,value); beanDefinition.getPropertyDefinitions().add(propertyDefinition); } beanDefinitions.add(beanDefinition); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 完成bean的实例化 */ private void instanceBeans(){ try { for (BeanDefinition beanDefinition : beanDefinitions) { if (beanDefinition.getClazz()!=null&&!"".equals(beanDefinition.getClazz().trim())) { sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClazz()).newInstance()); } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 为bean对象的属性注入值 * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ private void injectObject() { try { for (BeanDefinition beanDefinition : beanDefinitions) { Object bean=sigletons.get(beanDefinition.getId()); if (bean!=null) { PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for(PropertyDefinition propertyDefinition:beanDefinition.getPropertyDefinitions()){ for (PropertyDescriptor propertyDescriptor : ps) { if (propertyDefinition.getName().equals(propertyDescriptor.getName())) { Method setterMethod=propertyDescriptor.getWriteMethod();//获取属性的setter方法 if (setterMethod!=null) { Object temp=null; if (propertyDefinition.getRef()!=null&&!"".equals(propertyDefinition.getRef().trim())) { temp=sigletons.get(propertyDefinition.getRef()); }else if (propertyDefinition.getValue()!=null&&!"".equals(propertyDefinition.getValue().trim())) { temp=ConvertUtils.convert(propertyDefinition.getValue(), propertyDescriptor.getPropertyType()); } setterMethod.setAccessible(true);//防止setter方法为private setterMethod.invoke(bean, temp);//把引用对象注入到属性 } break; } } } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 通过注解实现注入依赖对象 * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ private void annotationInject(){ try { for (String beanName : sigletons.keySet()) { Object bean=sigletons.get(beanName); if (bean!=null) { PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : ps) { Method setterMethod=propertyDescriptor.getWriteMethod(); if (setterMethod!=null&&setterMethod.isAnnotationPresent(UserDefinedResource.class)) { UserDefinedResource userDefinedResource=setterMethod.getAnnotation(UserDefinedResource.class); Object temp = null; if(userDefinedResource.name()!=null && !"".equals(userDefinedResource.name())){ //一旦指定了name属性,就只能按名称装配了 temp = sigletons.get(userDefinedResource.name()); }else{ temp = sigletons.get(propertyDescriptor.getName()); if(temp==null){ for(String key : sigletons.keySet()){ if(propertyDescriptor.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){ temp = sigletons.get(key); break; } } } } setterMethod.setAccessible(true); setterMethod.invoke(bean, temp);//把引用对象注入到属性 } } Field[] fields = bean.getClass().getDeclaredFields(); for(Field field : fields){ if(field.isAnnotationPresent(UserDefinedResource.class)){ UserDefinedResource userDefinedResource = field.getAnnotation(UserDefinedResource.class); Object temp = null; if(userDefinedResource.name()!=null && !"".equals(userDefinedResource.name())){ temp = sigletons.get(userDefinedResource.name()); }else{ temp = sigletons.get(field.getName()); if(temp==null){ for(String key : sigletons.keySet()){ if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){ temp = sigletons.get(key); break; } } } } field.setAccessible(true);//允许访问private字段 field.set(bean, temp); } } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取bean实例 * @param beanName * @return */ public Object getBean(String beanName){ return this.sigletons.get(beanName); } }
package junit.test12; import static org.junit.Assert.*; import junit.test12.PersonService; import junit.test12.UserDefinedClassPathXMLApplicationContext; import org.junit.Test; public class SpringTest { @Test public void instanceSpring() { UserDefinedClassPathXMLApplicationContext applicationContext=new UserDefinedClassPathXMLApplicationContext("beans.xml"); PersonService personService=(PersonService) applicationContext.getBean("personService"); personService.save(); } }
- itcastSpring.rar (7 MB)
- 下载次数: 20
发表评论
-
java正则表达式修改html标签中属性
2013-04-21 11:45 4829package com.ez; import ... -
String.intern()解析
2012-07-16 22:53 1577String.intern()解析 Str ... -
Java中的访问控制public,private,protected,package
2012-05-08 02:41 28041. Java中的访问控制表1-1 可见/ ... -
Beginning SWT
2012-04-12 15:41 0SWT(Standard Widget Toolkit, ... -
Java IO读写大文件的几种方式及测试
2012-03-13 17:24 3396读取文件大小:1.45G 第一种,OldIO: (注意文件和系 ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-03-13 17:25 1318最近的机器内存又爆满了,除了新增机器内存外,还应该好好r ... -
复习nio
2012-03-13 17:24 1727简介: JDK 1.4 中引入的新输入输出 (NIO) 库在 ... -
The Closeable, Flushable, Readable, and Appendable interfaces
2012-03-13 17:23 1851Java SE 5.0引入了4个附加的接口:C ... -
Java IO复习
2012-03-13 17:22 1395什么是流: 流是一个抽象的概念。当Java程序需要 ... -
spring事务(附件pdf)
2011-12-08 21:27 1643事务传播属性 REQUIRED ... -
Spring aop 简单总结
2011-12-08 16:59 2524先用jdk,cglib模拟下: 使用JDK动态代理 //当 ... -
Spring 积累
2011-12-08 17:23 1865三种实例化bean的方式 1.使用类构造器实例化 & ... -
Drools与Spring集成 登录测试
2011-11-26 23:09 21787Drools5.2.0.Final与Spring3集成测试 ... -
javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录)
2011-10-28 15:59 11886看到很多用getResourcesAsStream()来读取. ... -
java程序员常用英语
2011-11-14 08:16 5563干程序员这行实在是 ... -
Spring管理filter和servlet,无硬编码bean
2011-10-25 08:23 5265Spring管理filter和servlet,无需硬编码 ... -
Oracle在2011年取得的Java主要成就
2011-10-20 08:29 2010甲骨文公司继续推动Java前进的战略。自2010年1月收购 ... -
Java规则引擎与其API(JSR-94)
2011-10-19 10:02 4206转自:http://www.ibm.com/developer ... -
Core Java , Volume 1 笔记
2011-07-17 11:36 051 StringBuffer:效率略低于String ... -
树形显示
2011-07-17 11:26 1638/** 树形结构应用十分广泛。 下面这段代码根据 ...
相关推荐
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
这是个模拟spring ioc的学习例子,是个java工程。有助于对aop和ioc的理解
练习共三部分: 1、XML读取,用于配置文件读取 2、模拟SPRING IOC 3、模拟STRUTS
基于xml注册,简单模拟spring ioc容器。快速理解ioc容器
NULL 博文链接:https://lgd-java2eye.iteye.com/blog/759878
基于java简单模拟实现spring_ioc
模拟spring框架的ioc功能,简单实用的实现,高效。
该代码主要用于spring IOC的实现(墨者革离),模拟一个城门叩问的编剧场景
主要介绍了使用Java注解模拟spring ioc容器过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
3. 如果一切正常,你会看到使用Java集合、XML API,以及反射技术模拟Spring的get注入和自定义的IoC容器实现的例子--get注入了Hello类和一个MyFrame类,并且实例化之后可以运行! 4. 看一下testDemoSpringDI()方法,...
java系统源码 IoC 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理。 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过。 但是 spring 源码存在一个...
(3)MarketDao类:模拟数据库访问,包含属性对象 Market m,和方法Product findByid(String id)来实现根据商品编号查找商品,方法List<product> findAll()来实现返回仓库(商品集合),方法void addpro(Product p)...
https://blog.csdn.net/qq_43564410/article/details/116142744
/*模拟spring的实现 * 1.通过构造器获取配置文件 * 2.通过dom4j解析配置文件,得到list集合(存放Bean标签的id和class属性) * 3.通过反射实例化得到对应的实例化对象,放置在map中(map是键值对,可根据id获取值)...
说明:使用Spring IOC模拟实现账户添加功能程序 1.创建JavaBean类:Account.java 2.创建DAO类: AccountDao.java,模拟账户添加操作 3.创建Service类:AccountService.java,模拟账户添加业务 4.创建Controller类:...
JAVA练习之XML读取,此代码将在模拟SPRING IOC 和 STRUTS中使用 共三部分: 1、XML读取,用于配置文件读取 2、模拟SPRING IOC 3、模拟STRUTS
SpringDemodemo简单模拟了spring ClasspathXxmlApplicationContext的工作机制,学习了spring是如何实现IOC。控制反转:将某一接口的具体实现类的选择控制权从调用类中移除,转交给第三方决定,即由Spring容器借由bean...
我的春天 Spring IOC的模拟
一个基于注解的轻量级IOC容器,模拟 Spring IOC,支持循环依赖处理。 Maven引入 <id>byx-maven-repo <name>byx-maven-repo <url>https://gitee.com/byx2000/maven-repo/raw/master/</url> <groupId>byx...