首先分析。

1: 肯定要利用dom4j读取xml配置文件,将所有的bean的配置信息读取出来

2: 利用反射技术,实例化所有的bean

3: 写注解处理器, 利用注解和内省实现依赖对象的注入。

4: 利用XML中<property>信息,通过内省beanUtils实现基本数据类型的注入

实现:

package cn.gbx.example;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
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 MyClassPathXmlApplicationContext {
private List<DefBean> defBeans = new ArrayList<DefBean>();
private Map<String, Object> singletons = new HashMap<String, Object>(); public MyClassPathXmlApplicationContext(String filename) {
this.readXML(filename);
this.instanceBean();
this.injectAnotation();
this.injectXML(); } //注入基本类型的属性
private void injectXML() {
//枚举bean
for (DefBean defBean : defBeans) {
Object bean = singletons.get(defBean.getId());
if (bean != null) {
//枚举该bean的Property看有没有基本数据类型的注入
for (DefProperty defProperty : defBean.getDefPropertys()) {
if (defProperty.getValue() != null){
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for (PropertyDescriptor p : ps) {
if (p.getName().equals(defProperty.getName())) {
Method setter = p.getWriteMethod();
//保证有set方法
if (setter != null) {
Object value = ConvertUtils.convert(defProperty.getValue(), p.getPropertyType());
setter.setAccessible(true);
setter.invoke(bean, value);
}
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
} //注解处理器
private void injectAnotation() {
//枚举实例化的bean
for (String key : singletons.keySet()) {
Object bean = singletons.get(key);
if (bean != null) { /*
* 检查setter方法
*/
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
//枚举每个bean的属性
for (PropertyDescriptor p : ps) {
Method setter = p.getWriteMethod();
Object value = null;
//存在set方法, 并且有setter上边有Resource注解
if (setter != null && setter.isAnnotationPresent(GbxResource.class)){
GbxResource gbxResource = setter.getAnnotation(GbxResource.class);
//若有name 则按name查找
if (gbxResource.name() != null && !"".equals(gbxResource.name())) {
value = singletons.get(gbxResource.name());
} else { //若没有,先安名字查找, 再按数据类型查找
value = singletons.get(p.getName());
if (value == null) {
for (String key2 : this.singletons.keySet()) {
if (p.getPropertyType().isAssignableFrom(this.singletons.get(key2).getClass())) {
value = this.singletons.get(key2);
break;
}
}
}
}
setter.setAccessible(true);
setter.invoke(bean, value);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*
* 检查字段
*/
Field[] fileds = bean.getClass().getDeclaredFields();
for (Field field : fileds) {
if (field.isAnnotationPresent(GbxResource.class)) {
GbxResource gbxResource = field.getAnnotation(GbxResource.class);
Object value = null;
if (gbxResource.name() != null && !"".equals(gbxResource.name())) {
value = this.singletons.get(gbxResource.name());
} else {
value = this.singletons.get(field.getName());
if (value == null) {
for (String key2 : this.singletons.keySet()) {
if (field.getType().isAssignableFrom(this.singletons.get(key2).getClass())) {
value = this.singletons.get(key2);
break;
}
}
}
}
field.setAccessible(true);
try {
field.set(bean, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
} } //实例化bean对象
private void instanceBean() {
for (DefBean bean : defBeans) {
System.out.println(bean.getId() + " : " + bean.getClassName());
if (bean.getClassName() != null && !"".equals(bean.getClassName())) {
try {
singletons.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//读取xml文件
private void readXML(String filename) {
SAXReader reader = new SAXReader();
Document document = null;
URL xmlPath = this.getClass().getClassLoader().getResource(filename); try {
//的到document
document = reader.read(xmlPath);
//设置命名空间
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");
//创建查询路径
XPath xPath = document.createXPath("//ns:beans/ns:bean");
xPath.setNamespaceURIs(nsMap); List<Element> beans = xPath.selectNodes(document);
DefBean defBean = null;
for (Element e : beans) {
String id = e.attributeValue("id");
String className = e.attributeValue("class");
defBean = new DefBean(id, className); XPath xPath2 = e.createXPath("ns:property");
xPath2.setNamespaceURIs(nsMap);
List<Element> propertys = xPath2.selectNodes(e); DefProperty defProperty = null;
for (Element e2 : propertys) {
String name = e2.attributeValue("name");
String ref = e2.attributeValue("ref");
String value = e2.attributeValue("value"); defProperty = new DefProperty(name, ref, value);
defBean.getDefPropertys().add(defProperty);
}
defBeans.add(defBean);
}
} catch (Exception e) {
e.printStackTrace();
}
} public Object getBean(String key) {
return singletons.get(key);
}
}

  

package cn.gbx.serviceimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import cn.gbx.daoimpl.PersonDao;
import cn.gbx.example.GbxResource;
import cn.gbx.service.PersonService; public class PersonServiceImpl implements PersonService {
@GbxResource
private PersonDao personDao;
private String name; public void save() {
personDao.save();
System.out.println("Name = " + name);
System.out.println("service层的 save方法");
} public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public PersonDao getPersonDao() {
return personDao;
} }

  

注解:

package cn.gbx.example;

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.METHOD, ElementType.FIELD})
public @interface GbxResource {
public String name() default "";
}

  

编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入的更多相关文章

  1. Spring学习--通过注解配置 Bean (三)

    组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...

  2. Spring 与 @Resource注解

    Spring 中支持@Autowired注解,能够实现bean的注入.同时,Spring 也支持@Resource注解,它和@Autowired类似,都是实现bean的注入.该注解存在javax.an ...

  3. Spring之使用注解实例化Bean并注入属性

    1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...

  4. Spring学习--通过注解配置 Bean (二)

    在 classpath 中扫描组件: 当在组件类上使用了特定的注解之后 , 还需要在 Spring 的配置文件中声明 <context:component-scan>: base-pack ...

  5. Spring中@Resource注解报错

    描述:Spring框架中,@Resource注解报错,在书写时没有自动提示 解决方法:因为maven配置文件的pom.xml文件中缺少javax.annotation的依赖,在pom.项目路中加入依赖 ...

  6. Spring中用@DependsOn注解控制Bean的创建顺序

    1. 概述 Spirng容器自己会管理bean的生命周期和bean实例化的顺序,但是我们仍然可以根据我们自己的需求进行定制.我可以可以选择使用SmartLifeCycle接口,也可以用@Depends ...

  7. Spring_day01--Spring的bean管理(xml方式)_属性注入介绍

    Spring的bean管理(xml方式) Bean实例化的方式 1 在spring里面通过配置文件 创建对象 2 bean实例化(创建对象)三种方式实现 第一种 使用类的无参数构造创建(重点) Use ...

  8. spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)

    利用扫描的方式将组件注入容器,就也可以不用操作bean来实例化对象了. 下面我做一个例子 我用的spring3.2.2版本的 首先写一个spring.xml. <?xml version=&qu ...

  9. Spring学习--通过注解配置 Bean (一)

    在 classpath 中扫描组件: 组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件. 特定组件包括: @ ...

随机推荐

  1. Backup: Date and Time in Perl6

    时间 Date #Operators ==, <, <= , >, >=, !=, eq, lt, le # Methods $date = Date.new(YEAR, MO ...

  2. Oracle集合运算符 交集 并集 差集

     集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集  一.union求并集,公共部分只有包含一次 例:求emp表ename中含’A‘或含有‘M’ SQL&g ...

  3. rsync 排除指定目录或文件进行同步

    很常见的情况:我想同步/myweb下的 一些php文件 ,  但是不想复制/myweb/log/里边的一些日志文件,因为这些文件太大了,备份也是没有意义的. 现在如果想避开某个路径  直接添加—exc ...

  4. rtsp里面的音频类型,大部分都默认规定了样本大小的。

    从src/audio_output/common.c里面的函数aout_BitsPerSample可知,rtsp里面的音频类型,大部分都默认规定了样本bit大小的.即是,一个音频编码类型,对应一个固定 ...

  5. [ios][swift]Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等)

    自己写的不一定是最好的! 轮子:http://www.hangge.com/blog/cache/detail_765.html

  6. 求两个数的最大公约数(Java)

    获得两个随机数(100以内),并放入数组中 public int[] getTwoRandom(){ int[] t = new int[2]; Random rand = new Random(); ...

  7. 【转】MYSQL入门学习之十二:存储过程的基本操作

    转载地址:http://www.2cto.com/database/201212/177380.html 存储过程简单来说,就是为以后的使用而保存的一条或多条MySQL语句的集合.可将其视为批文件,虽 ...

  8. 【转】MYSQL入门学习之九:索引的简单操作

    转载地址:http://www.2cto.com/database/201212/176772.html 一.创建索引  www.2cto.com           MYSQL常用的索引类型主要有以 ...

  9. I'm Telling the Truth(二分图)

    .I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

  10. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5879

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5879 解法:我们知道到某个极限之后结果相同,所以找到那个极限,其他保存之后输出就好了 #include&l ...