http://blog.csdn.net/yerenyuan_pku/article/details/52860046

上文我们已经学会使用@Resource注解注入属性。学是学会了,但也仅限于会使用而已,故我们要深入剖析其内部原理,才算真正明白。接下来我们就来编码剖析@Resource注解的实现原理,但这都是建立在用@Resource注解完成属性装配的案例基础上的。 
由于我们要使用dom4j工具来读取Sping的配置文件——beans.xml,所以需要将dom4j所需的jar包导入项目中。dom4j所需的jar包为:

  • dom4j-1.6.1.jar
  • jaxen-1.1-beta-6.jar

使用dom4j工具读取到诸如

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
...
</bean>

这样的内容时,务必需要将读取到的bean的信息存到一个JavaBean对象中。于是我们可在junit.test包下创建这样一个JavaBean——BeanDefinition.Java,其代码为:

/**
* 将读取到的bean的信息存到一个JavaBean对象中
* @author li ayun
*
*/
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;
}
}
  • 1

接着,我们还要在junit.test包下新建一个JavaBean——PropertyDefinition.java,该JavaBean专门用于存放<property ...>的信息,其代码如下:

/**
* 该JavaBean专门用户存放<property>的信息
* @author li ayun
*
*/
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;
}
}
  • 1

我们模拟Spring容器注入基本类型属性时,须将本身为字符串的值转成相应的属性类型的值,所以就要用到commons-beanutils工具,即要将commons-beanutils-1.9.2.jar包导入到项目中去。这样,项目中共须jar包有:
 
接下来,我们在junit.test包中新建一个注解类——ItcastResource.java,其代码如下:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface ItcastResource {
public String name() default "";
}
  • 1

以上所有事情做好之后,我们就要对传智播客版的Spring容器修修改改了,即将ItcastClassPathXMLApplicationContext类的代码修改为:

/**
* 传智播客版Spring容器
* @author li ayun
*
*/
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.injectObject();
this.annotationInject();
} 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 propertyDesc : ps) {
Method setter = propertyDesc.getWriteMethod(); // 获取属性的setter方法
if (setter != null && setter.isAnnotationPresent(ItcastResource.class)) { // setter方法存在注解
ItcastResource resource = setter.getAnnotation(ItcastResource.class);
Object value = null;
if (resource.name() != null && !"".equals(resource.name())) { // 指定了注解的name属性
value = sigletons.get(resource.name());
} else {
value = sigletons.get(propertyDesc.getName());
if (value == null) {
for (String key : sigletons.keySet()) {
// isAssignableFrom(xxx)方法判断propertyDesc.getPropertyType()获得的类型是否是xxx的接口或父类,或者是xxx本身
if (propertyDesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())) {
value = sigletons.get(key);
break;
}
}
}
}
setter.setAccessible(true); // 允许访问私有的setter方法
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())) { // 指定了注解的name属性
value = sigletons.get(resource.name());
} else {
value = sigletons.get(field.getName());
if (value == null) {
for (String key : sigletons.keySet()) {
// isAssignableFrom(xxx)方法判断field.getPropertyType()获得的类型是否是xxx的接口或父类,或者是xxx本身
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 propertyDesc : ps) {
if (propertyDefinition.getName().equals(propertyDesc.getName())) {
Method setter = propertyDesc.getWriteMethod(); // 获取属性的setter方法,private
if (setter != null) { // 最好判断有无setter方法,因为属性可以没有setter方法
/*
Object value = sigletons.get(propertyDefinition.getRef());
setter.setAccessible(true); // 允许访问私有的setter方法
setter.invoke(bean, value); // 把引用对象注入到属性中
*/ Object value = null;
if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim()) ) {
value = sigletons.get(propertyDefinition.getRef());
} else { // 注入基本类型
value = ConvertUtils.convert(propertyDefinition.getValue(), propertyDesc.getPropertyType()); // 把本身为字符串的值转成相应的属性类型的值
}
setter.setAccessible(true); // 允许访问私有的setter方法
setter.invoke(bean, value); // 把引用对象注入到属性中
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} }
}
} /**
* 完成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");
// System.out.println(propertyName + "=" + propertyRef); 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);
}
}

传智播客版Spring容器写好之后,我们就要试验一把了。首先将@ItcastResource注解使用在属性的setter方法上,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name; @ItcastResource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} public PersonServiceBean() {} public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
} @Override
public void save() {
// System.out.println(name);
personDao.add();
}
}
  • 1

接着我们将单元测试类——SpringTest.java的代码修改为:

public class SpringTest {

    @Test
public void instanceSpring() {
ItcastClassPathXMLApplicationContext ctx = new ItcastClassPathXMLApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
personService.save();
} }

测试instanceSpring()方法,可看到Eclipse控制台打印: 
 
当然了,我们也可将@ItcastResource注解用在字段上,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
@ItcastResource private PersonDao personDao;
private String name; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} public PersonServiceBean() {} public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
} @Override
public void save() {
// System.out.println(name);
personDao.add();
}
}

再次测试instanceSpring()方法,仍可看到Eclipse控制台打印: 
 
如要查看源码,可点击编码剖析@Resource注解的实现原理进行下载。

(转)编码剖析@Resource注解的实现原理的更多相关文章

  1. Spring2.5学习3.2_编码剖析@Resource注解的实现原理

    首先看一下J2EE提供的@Resource注解:该注解默认安照名称进行装配,名称能够通过name属性进行指定, 假设没有指定name属性,当注解写在字段上时,默认取字段名进行依照名称查找,假设注解写在 ...

  2. Spring(八)编码剖析@Resource注解的实现原理

    配置文件beans2.xml <?xml version="1.0" encoding="UTF-8"? > <beans xmlns=&qu ...

  3. Spring、Spring依赖注入与编码剖析Spring依赖注入的原理

    Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...

  4. (转)编码剖析Spring管理Bean的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...

  5. (转)编码剖析Spring装配基本属性的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52856465 上回我们已经讲到了Spring依赖注入的第一种方式,现在我们来详解第二种方式,须 ...

  6. (转)编码剖析Spring依赖注入的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...

  7. 编码剖析Spring管理bean的原理

    project目录 MyClassPathXMLApplicationContext读取xml,以及实例化bean. 因为是一开始实例化配置文件所有bean,所以需要构造器完成这些工作. packag ...

  8. Spring、编码剖析Spring管理Bean的原理

    引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...

  9. Spring第三弹—–编码剖析Spring管理Bean的原理

    先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

随机推荐

  1. EOJ Monthly 2018.4 (E.小迷妹在哪儿(贪心&排序&背包)

    ultmaster 男神和小迷妹们玩起了捉迷藏的游戏. 小迷妹们都希望自己被 ultmaster 男神发现,因此她们都把自己位置告诉了 ultmaster 男神,因此 ultmaster 男神知道了自 ...

  2. CreateRemoteThread注入DLL

    DLL注入的常用方式之一远程线程注入,实现代码如下 // CreateRemoteThread.cpp : Defines the entry point for the application.// ...

  3. View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows

    Windows Every iOS application needs at least one window—an instance of the UIWindow class—and some m ...

  4. Windows下允许redis远程访问

    1.安装很简单,下载安装包安装即可,略过: 下载msi的安装包,会自动安装进服务: 2.远程访问 Redis默认只允许本地访问,要使Redis可以远程访问,需修改配置文件: 我用的redis3.2 , ...

  5. 简析hotjar录屏功能实现原理

    简析hotjar录屏功能实现原理 众所周知,hotjar中录屏功能是其重要的一个卖点,看着很牛X酷炫的样子,今天就简单的分析一下其可能实现(这里只根据其请求加上个人理解分析,并不代表hotjar中真实 ...

  6. 2016 Multi-University Training Contest 1 GCD【RMQ+二分】

    因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺.废话不多说了,补题... update////2 ...

  7. hdu1085 Holding Bin-Laden Captive!【生成函数】

    列出生成函数的多项式之后暴力乘即可 #include<iostream> #include<cstdio> #include<cstring> using name ...

  8. Luogu P1265修复公路【Prim最小生成树】By cellur925

    题目传送门 政府审批的规则如下: (1)如果两个或以上城市申请修建同一条公路,则让它们共同修建: (2)如果三个或以上的城市申请修建的公路成环.如下图,A申请修建公路AB,B申请修建公路BC,C申请修 ...

  9. stylus基础教程,stylus实例教程,stylus语法总结

    stylus特点富于表现力.具有健壮性.功能丰富.动态编码不需要写CSS的冒号.分号.大括号和LESS.SASS功能类似,会这些的入手很快stylus特点安装使用stylus语法(一)选择器(二)变量 ...

  10. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) 题解

    真心简单的一场比赛 就是坑比较多(自己太蠢) A是一个水题 3分钟的时候过了 B也是一个比较简单的题 类似的套路见得多了 但是我当时可能比较困 想了一会才想出来 19分钟的时候过掉了 C同样很显然 性 ...