- 浏览: 981835 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (223)
- JDBC技术 (29)
- Java Base (21)
- SSH框架的集合 (5)
- Javascript部分 (16)
- Sping2.5框架 (6)
- Hibernate框架 (19)
- JDBC中的数据库连接 (5)
- Ajax技术应用 (9)
- Java工具包下载 (3)
- Database (4)
- Java Web (13)
- 我的生活 (4)
- Struts2 (25)
- S2SH (1)
- Prototype (1)
- jfreeChart (5)
- JPA (7)
- JBPM (1)
- JasperReport (6)
- DWR (1)
- freeMark (2)
- IBatis (2)
- FCKeditor (2)
- POI (1)
- JQuery (6)
- JSON (1)
- JavaMail (3)
- Flex (2)
- CSS收藏夹 (1)
- 润乾报表 (2)
- 开源技术 (1)
- Android (2)
- Android_UI (1)
- Android_OS (6)
- Android_OpenLibs (3)
- Volley (1)
- JS_OpenLibs (1)
- Hessian (3)
- OS (1)
最新评论
-
damoluomu:
简单易懂
Java Web开发Session超时设置 -
di1984HIT:
学习了~~~~~~
Java Web开发Session超时设置 -
lord_is_layuping:
...
Java Web开发Session超时设置 -
15111285554:
...
Java Web开发Session超时设置 -
sfg1314:
暂停后重新下载就会报错:ClientAbortExceptio ...
Java Web 文件下载之断点续传
作为一个应用Java的反射和注解的一个使用。
首简写一个XML的配置如下:
<?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="studao" class="di.dao.imple.StudentDaoImple"></bean> <bean id="stuService" class="di.service.imple.StudentServiceImple"> <property name="stuDao" ref="studao"></property> <property name="stuName" value="xiaobojava"></property> <property name="stuAge" value="23"></property> </bean> </beans>
基于上面的XML内容设计两个对象(get,set方法省略了):
/** * Bean的属性定义 * * @author 张明学 */ public class PropertyDefinition { private String name; private String ref; private String value; }
/** * Bean定义 * * @author 张明学 */ public class BeanDefinition { private String id; private String className; private List<PropertyDefinition> propertys = null; }
编写一个读取上面XML内容的工具类如下:
/** * 读取Spring配置文件XML的工具类 * * @author 张明学 * * 2010-6-1 */ public class ReadXMLUtil { public static List<BeanDefinition> readXMLBeanDefintion(String fileName) { List<BeanDefinition> beanDefintionList = new ArrayList<BeanDefinition>(); SAXReader saxReader = new SAXReader(); Document document = null; try { // 加载配置文件 URL xmlPath = ReadXMLUtil.class.getClassLoader().getResource(fileName); document = saxReader.read(xmlPath); // 设置一个命名空间 Map<String, String> nameSpaceMap = new HashMap<String, String>(); nameSpaceMap.put("ns","http://www.springframework.org/schema/beans"); // 创建查询Bean路径 XPath xsub = document.createXPath("//ns:beans/ns:bean"); xsub.setNamespaceURIs(nameSpaceMap); List<Element> beans = xsub.selectNodes(document); BeanDefinition bd = null; XPath propertyPath = null; List<Element> propertys = null; PropertyDefinition property = null; for (Element element : beans) { String id = element.attributeValue("id"); if (id == null) { id = element.attributeValue("name"); } String name = element.attributeValue("class"); bd = new BeanDefinition(id, name); // 查找属性信息 propertyPath = element.createXPath("//ns:property"); propertyPath.setNamespaceURIs(nameSpaceMap); propertys = propertyPath.selectNodes(document); // Bean的属性对象集合 List<PropertyDefinition> propertyList = new ArrayList<PropertyDefinition>(); for (Element proElement : propertys) { String proName = proElement.attributeValue("name"); String proRef = proElement.attributeValue("ref"); if(null != proRef && !"".equals(proRef)){ property = new PropertyDefinition(proName,proRef); }else{ String proValue = proElement.attributeValue("value"); property = new PropertyDefinition(); property.setName(proName); property.setValue(proValue); } propertyList.add(property); } bd.setPropertys(propertyList); beanDefintionList.add(bd); } } catch (Exception e) { e.printStackTrace(); } return beanDefintionList; } }
构造一个Bean工厂如下:
public class ClassPathXMLApplicationContext { private List<BeanDefinition> beanDefintionList = null; private Map<String, Object> sigletons = new HashMap<String, Object>(); public ClassPathXMLApplicationContext(String configFileName) { beanDefintionList = ReadXMLUtil.readXMLBeanDefintion(configFileName); this.instanceBeans(); this.injectObject(); this.annotationInject(); } /** * 用于注解形式的注入 * */ private void annotationInject() { for (String beanName : sigletons.keySet()) { Object bean = getBean(beanName); if (null != bean) { try { // 获取所有的属性 PropertyDescriptor pd[] = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for (PropertyDescriptor descriptor : pd) { // 获取set方法 Method setter = descriptor.getWriteMethod(); // 若在set方法设置了MyResource注解 if(null != setter && setter.isAnnotationPresent(MyResource.class)){ MyResource myResource = setter.getAnnotation(MyResource.class); String diName = null; Object diObject = null; // 设置了name属性值 if(null != myResource.name() && !"".equals(myResource.name())){ diName = myResource.name(); }else{//按默认的属性值装配置 diName = descriptor.getName(); } diObject = getBean(diName); setter.setAccessible(true); setter.invoke(bean, diObject); } } // 获取所有字段 Field[] fields = bean.getClass().getDeclaredFields(); for (Field field : fields) { if(field.isAnnotationPresent(MyResource.class)){ MyResource myResource = field.getAnnotation(MyResource.class); String diName = null; Object diObject = null; // 设置了name属性值 if(null != myResource.name() && !"".equals(myResource.name())){ diName = myResource.name(); }else{//按默认的属性值装配置 diName = field.getName(); } diObject = getBean(diName); field.setAccessible(true); field.set(bean, diObject); } } } catch (Exception e) { e.printStackTrace(); } } } } /** * 基于XML配置注入Bean * */ private void injectObject() { for (BeanDefinition beanDefinition : beanDefintionList) { Object obj = getBean(beanDefinition.getId()); if (null != obj) { // 属性信息 List<PropertyDefinition> propertys = beanDefinition.getPropertys(); if (null != propertys && propertys.size() > 0) { try { // 内省访问一个JavaBean获取所有属性信息 PropertyDescriptor[] ps = Introspector.getBeanInfo(obj.getClass()).getPropertyDescriptors(); for (PropertyDescriptor descriptor : ps) { for (PropertyDefinition property : propertys) { if (descriptor.getName().equals(property.getName())) { // 引用型的ref if (null != property.getRef()&& !"".equals(property.getRef())) { // 获取要注入的依赖对象 Object diObject = getBean(property.getRef()); // 注入依赖对象 descriptor.getWriteMethod().invoke(obj,diObject); } else {// value型的 BeanUtils.setProperty(obj, property.getName(), property.getValue()); } } } } } catch (Exception e) { e.printStackTrace(); } } } } } /** * 实例化Bean * */ private void instanceBeans() { for (BeanDefinition beanDefinition : beanDefintionList) { try { Object obj = Class.forName(beanDefinition.getClassName()).newInstance(); this.sigletons.put(beanDefinition.getId(), obj); } catch (Exception e) { e.printStackTrace(); } } } /** * 获取Bean实例 * * @param beanName * @return */ public Object getBean(String beanName) { return this.sigletons.get(beanName); } }
注解类如下:
/** * 自定义的一个注入的注解 * * @author 张明学 */ @Retention(RetentionPolicy.RUNTIME) @Target( { ElementType.FIELD, ElementType.METHOD }) public @interface MyResource { String name() default ""; }
测试:
public interface StudentDao { public abstract void add(); }
public class StudentDaoImple implements StudentDao { public void add() { System.out.println("StudentdaoImple的add方法"); } }
public interface StudentService { public abstract void save(); }
public class StudentServiceImple implements StudentService { private StudentDao stuDao; private String stuName; private Integer stuAge; public void save() { System.out.println(stuName); System.out.println(stuAge); stuDao.add(); } public void setStuAge(Integer stuAge) { this.stuAge = stuAge; } public void setStuName(String stuName) { this.stuName = stuName; } public void setStuDao(StudentDao stuDao) { this.stuDao = stuDao; } }
public class StudentServiceImple2 implements StudentService { @MyResource private StudentDao stuDao; public void save() { stuDao.add(); } }
public static void main(String[] args) { ClassPathXMLApplicationContext ctx = new ClassPathXMLApplicationContext("beans2.xml"); StudentService stuMght = (StudentService) ctx.getBean("stuService"); stuMght.save(); }
- MySpring.rar (709.6 KB)
- 下载次数: 72
发表评论
-
HttpURLConnection附件上传
2015-05-15 18:25 3358整合了一个自己写的采用Http做附件上传的工具,分享一下! ... -
java自带线程池和队列详细讲解
2014-11-29 16:39 8464Java线程池使用说明 一简介 线程的使用在java中占 ... -
使用CGLIB的代理技术
2010-06-09 19:04 1492import java.lang.reflect.Method ... -
Java基础十:JDK 动态代理类分析(java.lang.reflect.Proxy使用)
2010-05-28 19:25 4102/** * JDK 动态代理类分析(java.lang.r ... -
Java基础九:Java中的类加载器说明,可以对class文件加密,自定义ClassLoader
2010-05-28 19:22 3679/** * Java 类加载器 * * @aut ... -
Java基础八:泛型,?通配符
2010-05-28 19:17 6237/** * JDK1.5 泛型 * * @aut ... -
Java基础七:注解Annotation使用说明
2010-05-28 19:14 3980import java.lang.annotation.Ele ... -
Java基础六:apache的Beanutils简单使用对JavaBean操作的封装
2010-05-28 19:08 2132/** * apache的Beanutils简单使用 ... -
Java基础五:内省(对JavaBean的操作)
2010-05-28 19:05 2315/** * Person JavaBean * ... -
Java基础四: 反射(构造方法,成员变量,普通方法,数组)
2010-05-28 18:58 5727/** * 反射(构造方法,成员变量,普通方法,数组) ... -
Java基础三: 枚举高级应用(向枚举值对象中添加变量和方法)
2010-05-28 18:45 3803/** * JDK1.5 枚举高级应用(向枚举值对象中添加 ... -
Java基础二: hashCode的作用简要说明,可能导致对象在集合中无法删除
2010-05-28 18:30 1594import java.util.ArrayList; im ... -
Java基础一: 自动装箱,折箱、静态导入、可变参数
2010-05-28 18:24 1580自动装,折箱 /** * JDK1.5 自动装箱,折箱 ... -
apache StringUtils常见用法说明
2009-10-28 17:24 43851.空字符串检查使用函数: StringUtils.isBla ... -
产生随机字符串(a-z A-Z 0-9)
2009-09-25 17:47 3058实现方法一: public class RandomFile ... -
计算离当前日期之前或之后任意天的日期
2009-06-18 13:33 1466/** * 日期计算 * * @author z ... -
计算Java日期Date,DateFormat,GregorianCalendar的使用
2009-06-16 17:09 17923概要 不管你是处理财务交易还是计划着下一步的行动,你都要知 ... -
Java计算日期今天,昨天,上周,上月
2009-06-16 16:53 8383功能:今天,昨天,本周,上周,本月,上月 范围:以周一作为一 ... -
@SuppressWarnings(unchecked)作用解释.doc
2009-05-13 09:17 34575解释一: 屏蔽某些编译时的警告信息 ... -
JDK实现AOP动态代码
2009-04-20 14:29 62代理类: import java.lang.refle ...
相关推荐
Spring依赖注入——java项目中使用spring注解方式进行注入.rar
Spring Ioc 注解 依赖注入
详细阐明spring依赖注入工作原理,基于注解的一个完整例子,当然数据库需要自己去创建
Spring的依赖注入,与前置通知的实例
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
综合运用Java反射、泛型和注解实现的类似Spring的核心注入
Spring注解注入属性
介绍如何利用反射和注解去简单的模拟JUnit4单元测试的使用,之所以选择JUnit4是因为4.0以后最大的改进就是使用了注解。需要注意的是这里并不是完全的模拟,只是简单实现了一下Runner类和JUnit注解相关的工作流程。...
Spring与IoC系列四:基于注解的依赖注入。对于DI使用注解,将不再需要在Spring配置文件中声明Bean实例。Spring中使用注解,需要在原有Spring运行环境基础上再做一些改变
详解Spring基于Annotation的依赖注入实现
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
主要介绍了详解Java Spring各种依赖注入注解的区别的相关资料,需要的朋友可以参考下
公司的java项目中spring使用注解依赖jar
Java-Spring各种依赖注入注解的区别.doc
spring框架的注解的介绍,有需要的可以下载看一下,不错的选择
注解和注释以及Spring和SpringMVC常用的注解 1.两者区别 注解 :参与代码编译,以@开头的。它是给应用程序看的,单独使用注解毫无意义,一定要跟工具一起使用,这个所谓的工具实际就是能读懂注解的应用程序 注释 :...
Spring注解依赖注入 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
NULL 博文链接:https://lunzi.iteye.com/blog/1675871
自定义注解得使用,模拟spring通过注解方式创建bean实例!