- 浏览: 142321 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
flyingcatjj:
很适合我这种刚接触的
myeclipse xfire 开发webservice实例 -
tangzlboy:
非常好,非常详细,非常中用。
linux入门 -
yzz9i:
楼主说的很是详细。
myeclipse xfire 开发webservice实例 -
zqx888191:
很再理阿!
老程序员的教诲 -
simplecat123:
...
SQLite数据读取
七 编码剖析@Resource注解的实现原理 ItcastResource.java view plaincopy to clipboardprint? 01.package junit.test; 02. 03.import java.lang.annotation.ElementType; 04.import java.lang.annotation.Retention; 05.import java.lang.annotation.RetentionPolicy; 06.import java.lang.annotation.Target; 07. 08.@Retention(RetentionPolicy.RUNTIME) 09.@Target({ElementType.FIELD, ElementType.METHOD}) 10.public @interface ItcastResource { 11. public String name() default ""; 12.} package junit.test; 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 ItcastResource { public String name() default ""; } PropertyDefinition .java view plaincopy to clipboardprint? 01.package junit.test; 02. 03.public class PropertyDefinition { 04. private String name; 05. private String ref; 06. private String value; 07. 08. public String getValue() { 09. return value; 10. } 11. 12. public void setValue(String value) { 13. this.value = value; 14. } 15. 16. public PropertyDefinition(String name, String ref, String value) { 17. this.name = name; 18. this.ref = ref; 19. this.value = value; 20. } 21. 22. public String getName() { 23. return name; 24. } 25. public void setName(String name) { 26. this.name = name; 27. } 28. public String getRef() { 29. return ref; 30. } 31. public void setRef(String ref) { 32. this.ref = ref; 33. } 34. 35.} package junit.test; public class PropertyDefinition { private String name; private String ref; private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public PropertyDefinition(String name, String ref, String value) { 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; } } BeanDefinition.java view plaincopy to clipboardprint? 01.package junit.test; 02. 03.import java.util.ArrayList; 04.import java.util.List; 05. 06.public class BeanDefinition { 07. private String id; 08. private String className; 09. private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>(); 10. 11. public BeanDefinition(String id, String className) { 12. this.id = id; 13. this.className = className; 14. } 15. public String getId() { 16. return id; 17. } 18. public void setId(String id) { 19. this.id = id; 20. } 21. public String getClassName() { 22. return className; 23. } 24. public void setClassName(String className) { 25. this.className = className; 26. } 27. public List<PropertyDefinition> getPropertys() { 28. return propertys; 29. } 30. public void setPropertys(List<PropertyDefinition> propertys) { 31. this.propertys = propertys; 32. } 33. 34.} package junit.test; import java.util.ArrayList; import java.util.List; public class BeanDefinition { private String id; private String className; private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>(); public BeanDefinition(String id, String className) { this.id = id; this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<PropertyDefinition> getPropertys() { return propertys; } public void setPropertys(List<PropertyDefinition> propertys) { this.propertys = propertys; } } ItcastClassPathXMLApplicationContext.java view plaincopy to clipboardprint? 01.package junit.test; 02. 03.import java.beans.Introspector; 04.import java.beans.PropertyDescriptor; 05.import java.lang.reflect.Field; 06.import java.lang.reflect.Method; 07.import java.net.URL; 08.import java.util.ArrayList; 09.import java.util.HashMap; 10.import java.util.List; 11.import java.util.Map; 12. 13.import org.apache.commons.beanutils.ConvertUtils; 14.import org.dom4j.Document; 15.import org.dom4j.Element; 16.import org.dom4j.XPath; 17.import org.dom4j.io.SAXReader; 18. 19./** 20. * 传智传客版容器 21. * 22. */ 23.public class ItcastClassPathXMLApplicationContext { 24. private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); 25. private Map<String, Object> sigletons = new HashMap<String, Object>(); 26. 27. public ItcastClassPathXMLApplicationContext(String filename){ 28. this.readXML(filename); 29. this.instanceBeans(); 30. this.annotationInject(); 31. this.injectObject(); 32. } 33. /** 34. * 通过注解实现注入依赖对象 35. */ 36. private void annotationInject() { 37. for(String beanName : sigletons.keySet()){ 38. Object bean = sigletons.get(beanName); 39. if(bean!=null){ 40. try { 41. PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); 42. for(PropertyDescriptor properdesc : ps){ 43. Method setter = properdesc.getWriteMethod();//获取属性的setter方法 44. if(setter!=null && setter.isAnnotationPresent(ItcastResource.class)){ 45. ItcastResource resource = setter.getAnnotation(ItcastResource.class); 46. Object value = null; 47. if(resource.name()!=null && !"".equals(resource.name())){ 48. value = sigletons.get(resource.name()); 49. }else{ 50. value = sigletons.get(properdesc.getName()); 51. if(value==null){ 52. for(String key : sigletons.keySet()){ 53. if(properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){ 54. value = sigletons.get(key); 55. break; 56. } 57. } 58. } 59. } 60. setter.setAccessible(true); 61. setter.invoke(bean, value);//把引用对象注入到属性 62. } 63. } 64. Field[] fields = bean.getClass().getDeclaredFields(); 65. for(Field field : fields){ 66. if(field.isAnnotationPresent(ItcastResource.class)){ 67. ItcastResource resource = field.getAnnotation(ItcastResource.class); 68. Object value = null; 69. if(resource.name()!=null && !"".equals(resource.name())){ 70. value = sigletons.get(resource.name()); 71. }else{ 72. value = sigletons.get(field.getName()); 73. if(value==null){ 74. for(String key : sigletons.keySet()){ 75. if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){ 76. value = sigletons.get(key); 77. break; 78. } 79. } 80. } 81. } 82. field.setAccessible(true);//允许访问private字段 83. field.set(bean, value); 84. } 85. } 86. } catch (Exception e) { 87. e.printStackTrace(); 88. } 89. } 90. } 91. } 92. 93. /** 94. * 为bean对象的属性注入值 95. */ 96. private void injectObject() { 97. for(BeanDefinition beanDefinition : beanDefines){ 98. Object bean = sigletons.get(beanDefinition.getId()); 99. if(bean!=null){ 100. try { 101. PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); 102. for(PropertyDefinition propertyDefinition : beanDefinition.getPropertys()){ 103. for(PropertyDescriptor properdesc : ps){ 104. if(propertyDefinition.getName().equals(properdesc.getName())){ 105. Method setter = properdesc.getWriteMethod();//获取属性的setter方法 ,private 106. if(setter!=null){ 107. Object value = null; 108. if(propertyDefinition.getRef()!=null && !"".equals(propertyDefinition.getRef().trim())){ 109. value = sigletons.get(propertyDefinition.getRef()); 110. }else{ 111. value = ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType()); 112. } 113. setter.setAccessible(true); 114. setter.invoke(bean, value);//把引用对象注入到属性 115. } 116. break; 117. } 118. } 119. } 120. } catch (Exception e) { 121. } 122. } 123. } 124. } 125. /** 126. * 完成bean的实例化 127. */ 128. private void instanceBeans() { 129. for(BeanDefinition beanDefinition : beanDefines){ 130. try { 131. if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim())) 132. sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); 133. } catch (Exception e) { 134. e.printStackTrace(); 135. } 136. } 137. 138. } 139. /** 140. * 读取xml配置文件 141. * @param filename 142. */ 143. private void readXML(String filename) { 144. SAXReader saxReader = new SAXReader(); 145. Document document=null; 146. try{ 147. URL xmlpath = this.getClass().getClassLoader().getResource(filename); 148. document = saxReader.read(xmlpath); 149. Map<String,String> nsMap = new HashMap<String,String>(); 150. nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间 151. XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径 152. xsub.setNamespaceURIs(nsMap);//设置命名空间 153. List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点 154. for(Element element: beans){ 155. String id = element.attributeValue("id");//获取id属性值 156. String clazz = element.attributeValue("class"); //获取class属性值 157. BeanDefinition beanDefine = new BeanDefinition(id, clazz); 158. XPath propertysub = element.createXPath("ns:property"); 159. propertysub.setNamespaceURIs(nsMap);//设置命名空间 160. List<Element> propertys = propertysub.selectNodes(element); 161. for(Element property : propertys){ 162. String propertyName = property.attributeValue("name"); 163. String propertyref = property.attributeValue("ref"); 164. String propertyValue = property.attributeValue("value"); 165. PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref, propertyValue); 166. beanDefine.getPropertys().add(propertyDefinition); 167. } 168. beanDefines.add(beanDefine); 169. } 170. }catch(Exception e){ 171. e.printStackTrace(); 172. } 173. } 174. /** 175. * 获取bean实例 176. * @param beanName 177. * @return 178. */ 179. public Object getBean(String beanName){ 180. return this.sigletons.get(beanName); 181. } 182.} package junit.test; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; 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 org.apache.commons.beanutils.ConvertUtils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; /** * 传智传客版容器 * */ public class ItcastClassPathXMLApplicationContext { private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons = new HashMap<String, Object>(); public ItcastClassPathXMLApplicationContext(String filename){ this.readXML(filename); this.instanceBeans(); this.annotationInject(); this.injectObject(); } /** * 通过注解实现注入依赖对象 */ private void annotationInject() { for(String beanName : sigletons.keySet()){ Object bean = sigletons.get(beanName); if(bean!=null){ try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for(PropertyDescriptor properdesc : ps){ Method setter = properdesc.getWriteMethod();//获取属性的setter方法 if(setter!=null && setter.isAnnotationPresent(ItcastResource.class)){ ItcastResource resource = setter.getAnnotation(ItcastResource.class); Object value = null; if(resource.name()!=null && !"".equals(resource.name())){ value = sigletons.get(resource.name()); }else{ value = sigletons.get(properdesc.getName()); if(value==null){ for(String key : sigletons.keySet()){ if(properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){ value = sigletons.get(key); break; } } } } setter.setAccessible(true); setter.invoke(bean, value);//把引用对象注入到属性 } } Field[] fields = bean.getClass().getDeclaredFields(); for(Field field : fields){ if(field.isAnnotationPresent(ItcastResource.class)){ ItcastResource resource = field.getAnnotation(ItcastResource.class); Object value = null; if(resource.name()!=null && !"".equals(resource.name())){ value = sigletons.get(resource.name()); }else{ value = sigletons.get(field.getName()); if(value==null){ for(String key : sigletons.keySet()){ if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){ value = sigletons.get(key); break; } } } } field.setAccessible(true);//允许访问private字段 field.set(bean, value); } } } catch (Exception e) { e.printStackTrace(); } } } } /** * 为bean对象的属性注入值 */ private void injectObject() { for(BeanDefinition beanDefinition : beanDefines){ Object bean = sigletons.get(beanDefinition.getId()); if(bean!=null){ try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for(PropertyDefinition propertyDefinition : beanDefinition.getPropertys()){ for(PropertyDescriptor properdesc : ps){ if(propertyDefinition.getName().equals(properdesc.getName())){ Method setter = properdesc.getWriteMethod();//获取属性的setter方法 ,private if(setter!=null){ Object value = null; if(propertyDefinition.getRef()!=null && !"".equals(propertyDefinition.getRef().trim())){ value = sigletons.get(propertyDefinition.getRef()); }else{ value = ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType()); } setter.setAccessible(true); setter.invoke(bean, value);//把引用对象注入到属性 } break; } } } } catch (Exception e) { } } } } /** * 完成bean的实例化 */ private void instanceBeans() { for(BeanDefinition beanDefinition : beanDefines){ try { if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim())) sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } catch (Exception e) { e.printStackTrace(); } } } /** * 读取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); Map<String,String> nsMap = new HashMap<String,String>(); nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间 XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap);//设置命名空间 List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点 for(Element element: beans){ String id = element.attributeValue("id");//获取id属性值 String clazz = element.attributeValue("class"); //获取class属性值 BeanDefinition beanDefine = new BeanDefinition(id, clazz); XPath propertysub = element.createXPath("ns:property"); propertysub.setNamespaceURIs(nsMap);//设置命名空间 List<Element> propertys = propertysub.selectNodes(element); for(Element property : propertys){ String propertyName = property.attributeValue("name"); String propertyref = property.attributeValue("ref"); String propertyValue = property.attributeValue("value"); PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref, propertyValue); beanDefine.getPropertys().add(propertyDefinition); } beanDefines.add(beanDefine); } }catch(Exception e){ e.printStackTrace(); } } /** * 获取bean实例 * @param beanName * @return */ public Object getBean(String beanName){ return this.sigletons.get(beanName); } } 实际上也就是通过了反射技术来构造对象并且赋值,只是用到了注解的方法,并且利用@interface来构造自定义的注解类型。
发表评论
-
十八 Spring2.5+Hibernate3.3+Struts2整合开发
2011-07-04 17:16 766十八 Spring2.5+Hibernate3.3+Strut ... -
十七 Spring2.5+JPA+Struts1.3整合开发
2011-07-04 17:16 1186十七 Spring2.5+JPA+Struts1.3整合开发 ... -
十六 Spring2.5+Hibernate3.3+Struts1.3整合开发
2011-07-04 17:13 880十六 Spring2.5+Hibernate3.3+Strut ... -
十五 用xml方式配置事务
2011-07-04 17:11 789十五 用xml方式配置事务 ... -
十四 spring容器的声明式事务管理
2011-07-04 17:10 925spring在运行期(unchecked ... -
十三 Spring+JDBC组合开发
2011-07-04 17:09 913十三 Spring+JDBC组合开发 ... -
十二 spring的AOP编程
2011-07-04 17:06 1341Aspect(切面):指横切性关 ... -
十一 使用CGLIB实现AOP功能与AOP概念解释
2011-07-04 17:05 773十一 使用CGLIB实现AOP功能与AOP概念解释 前面 ... -
十 使用JDK中的Proxy技术实现AOP功能
2011-07-04 17:03 760十 使用JDK中的Proxy技术实现AOP功能 通过代理 ... -
九 自动扫描和管理bean
2011-07-04 17:02 710九 自动扫描和管理bean ... -
八 @Autowire注解与自动装配
2011-07-04 17:01 825使用了@Autowired的注解方式,这种默认按类型查找符合的 ... -
六 用@Resource注解完成属性装配
2011-07-04 16:58 1823六 用@Resource注解完成属性装配 java代 ... -
五 依赖注入
2011-07-04 16:57 708五 依赖注入 1>set注入方式 1 对象注入 ... -
四 spring管理bean的作用域
2011-07-04 16:57 923四 spring管理bean的作用域 先看配置 < ... -
三 实例化bean的方法
2011-07-04 16:56 681三 实例化bean的方法 1接口 package cn. ... -
二 开启spring之旅
2011-07-04 16:55 6891 首先下载spring 最佳答案 版本2.5.6,官 ... -
一 spring的介绍
2011-07-04 16:54 6731 Spring是什么 Spring是一个开源的控制反转(I ... -
Spring学习笔记(4)----Bean节点信息配置
2010-10-29 16:53 738默认情况下,Spring的Ioc容器启动时会初始化bean ... -
Spring学习笔记(3)----编码剖析Spring管理Bean的原理
2010-10-29 16:47 689Xml代码 <?xml versio ... -
Sping学习笔记(2)----实例化Bean的三种方式
2010-10-29 16:45 781Spring的实例化Bean有三种方式: 使用类构造器 ...
相关推荐
编码剖析@Resource注解的实现原理
Spring Ioc源码分析系列--@Autowired注解的实现原理.doc
12_编码剖析@Resource注解的实现原理 13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP...
@ConfigurationProperties注解使用方法(源代码) 目录 @ConfigurationProperties注解使用方法 前言 一、Spring配置方式 1.1 第一阶段:xml配置 1.2 第二阶段:注解配置 1.3 第三阶段:Java配置(java config) 二、@...
资源名称:拓薪教育-Spring内幕深入剖析和实战精讲资源目录:【】01.拓薪教育-spring3.2-序【】02.拓薪教育-Spring3.2-介绍IOC上【】03.拓薪教育-spring3.2-介绍IOC下【】04.拓薪教育-spring3.2-AOP和其他功能介绍...
主要介绍了Spring实战之使用@Resource配置依赖操作,结合实例形式分析了Spring使用@Resource配置依赖具体步骤、实现及测试案例,需要的朋友可以参考下
《卷积编码与解码的MATLAB实现及性能分析》.docx《卷积编码与解码的MATLAB实现及性能分析》.docx《卷积编码与解码的MATLAB实现及性能分析》.docx《卷积编码与解码的MATLAB实现及性能分析》.docx《卷积编码与解码的...
首先,我们从应用启动类与配置入手,详细解析了@PropertySource和@EnableDubbo两个重要注解的作用和实现方式。然后,我们深入研究了@EnableDubboConfig和@DubboComponentScan两个核心注解,详细解读了它们在Spring...
Select函数实现原理分析Select函数实现原理分析Select函数实现原理分析Select函数实现原理分析
编码剖析Spring依赖注入的原理
LZW 是一种无损数据压缩算法,是对1978 年发表的 LZ78 的改进。LZW应用于Unix系统的标准工具、GIF图片格式以及TIFF格式等。同时LZW压缩算法对于较大规模的英文文本的压缩具有良好的效果,一般可以压缩到原来大小的...
主要介绍了Spring实战之@Autowire注解用法,结合实例形式详细分析了Spring @Autowire注解具体实现步骤与相关使用技巧,需要的朋友可以参考下
深度剖析BREW实现原理 深度剖析BREW实现原理 深度剖析BREW实现原理
DPCM的基本原理 任何信号,不论语音或图像,采用直接采样-量化-编码的方式进行编码,都会发现码组之间具有很强的相关性。由于相关性的存在,传输数据中... DPCM就是通过预测和差分编码方式来减少冗余,实现数据压缩的目的。
03_黑马程序员_黎活明_Spring2.5视频教程_编码剖析Spring管理Bean的原理
摘要:全面介绍了区间编码算法的理论基础和实现方式。详细讨论了区间编码原理、正规化操作、区间编码实现、二进制索引树的理论和实现等技术。并给出了一个切实可行的应用程序。
摘要:全面介绍了区间编码算法的理论基础和实现方式。详细讨论了区间编码原理、正规化操作、区间编码实现、二进制索引树的理论和实现等技术。并给出了一个切实可行的应用程序。
这个是搭建与测试Spring的开发环境和编码剖析Spring 管理Bean的原理的代码,适合刚接触spring的人学习,如何搭建spring开发环境,以及了解spring是如何管理bean的。这个是由传智播客的老师讲的。
license机制实现原理分析,参考。。。