- 浏览: 336750 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
alafqq:
很好的一篇启蒙hashmap的文章;HASHTABLE的93行 ...
使用数组和链表实现hash表存储信息 -
小帅1127:
我擦,我还以为有什么大坑呢,这也写出来。。。
if..else if和if..if的区别 -
fncj:
转下http://www.dodoer.com
hadoop单机版搭建图文详解 -
yueshang520:
Spring注解原理的详细剖析与实现 -
fncj:
转下,谢谢http://www.whohelpme.com/b ...
Spring注解原理的详细剖析与实现
一、注入依赖对象
基本类型对象注入:
<bean id=”orderService” class=”com.wxy.service.OrderServiceBean”>
<constructor-arg index=”0” type=”java.lang.String” value=”xxx”/>//构造器注入
<property name=”name” value=”wxy”/>//属性setter方法注入
</bean>
注入其他bean:
方式一:
<bean id=”orderDao” class=”com.wxy.service.OrderDaoBean”/>
<bean id=”orderService” class=”com.wxy.service.OrderServiceBean”>
<property name=”orderDao” ref=”orderDao”/>
</bean>
方法二:(使用内部bean,但该bean不能被其他bean使用)
<bean id=”orderService” class=”com.wxy.service.OrderServiceBean”>
<property name=”orderDao”>
<bean class=”com.wxy.service.OrderDaoBean”/>
</property>
</bean>
二、依赖注入(Dependency Injection)
所谓依赖注入就是指:在运行期,由外部容器动态的将依赖对象注入到组件中。
三、编码实现依赖注入功能:
1、 修改beans.xml,添加依赖配置:
1.1 <?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="peopleDao" class="com.wxy.dao.impl.PeopleDaoBean"></bean>
<bean id="peopleService" class="com.wxy.service.impl.PeopleServiceBean">
<property name="peopleDap" ref="peopleDao"></property>
<property name="xx" ref="xxDao"></property>
</bean>
</beans>
创建dao类:
package com.wxy.dao.impl;
import com.wxy.dao.PeopleDao;
/**
* Dao类,实现数据的持久化操作
* @create-time 2011-8-10 下午07:25:26
* @revision $Id
*/
public class PeopleDaoBean implements PeopleDao {
/* (non-Javadoc)
* @see com.wxy.dao.impl.PeopleDao#add()
*/
public void add() {
System.out.println("this is the method PeopleDaoBean.add()!");
}
}
1.2创建dao接口:
package com.wxy.dao;
public interface PeopleDao {
public abstract void add();
}
2、新建PropertyDefinition类,存放bean的property属性:
package com.wxy.bean;
/**
* 存放bean的property属性
* @create-time 2011-8-10 下午04:40:07
* @revision $Id
*/
public class PropertyDefinition {
private String name; //属性名
private String ref; //属性依赖对象
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the ref
*/
public String getRef() {
return ref;
}
/**
* @param ref the ref to set
*/
public void setRef(String ref) {
this.ref = ref;
}
public PropertyDefinition(String name, String ref) {
super();
this.name = name;
this.ref = ref;
}
}
2.1、 在BeanDefinition中加入PropertyList属性,存放property列表
package com.wxy.bean;
import java.util.ArrayList;
import java.util.List;
public class BeanDefinition {
private String id;
private String className;
private List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>(); //存放bean的属性列表
public BeanDefinition(String id, String className) {
this.id = id;
this.className = className;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the className
*/
public String getClassName() {
return className;
}
/**
* @param className the className to set
*/
public void setClassName(String className) {
this.className = className;
}
/**
* @return the properties
*/
public List<PropertyDefinition> getProperties() {
return properties;
}
/**
* @param properties the properties to set
*/
public void setProperties(List<PropertyDefinition> properties) {
this.properties = properties;
}
}
2.2配置peopleServiceBean,实现注入功能:
package com.wxy.service.impl;
import com.wxy.dao.PeopleDao;
import com.wxy.service.PeopleService;
/**
* PeopleServiceBean
* @create-time 2011-8-9 下午11:07:03
* @revision $Id:PeopleServiceBean.java
*/
public class PeopleServiceBean implements PeopleService {
private PeopleDao peopleDao;
/**
* @return the peopleDao
*/
public PeopleDao getPeopleDao() {
return peopleDao;
}
public void save() {
System.out.println("--> the method is called save()!");
peopleDao.add();
}
/**
* @param peopleDao the peopleDao to set
*/
public void setPeopleDao(PeopleDao peopleDao) {
this.peopleDao = peopleDao;
}
}
3、进入WxyClassPathXMLApplicationContext中,自己编码实现依赖注入内部功能:
package com.wxy.content;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
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 org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import com.wxy.bean.BeanDefinition;
import com.wxy.bean.PropertyDefinition;
/**
* 自定义IoC容器
* BeanDefinition的resource定位:readXML();
* BeanDefinition的载入和解析 :readXML();
* BeanDefinition在IoC容器中的注册 instanceBeans();
* @create-time 2011-8-10 上午09:19:17
* @revision $Id
*/
public class WxyClassPathXMLApplicationContext {
//存放BeanDefinition的列表,在beans.xml中定义的bean可能不止一个
private final List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
//将类名作为索引,将创建的Bean对象存入到Map中
private final Map<String, Object> sigletons = new HashMap<String, Object>();
public WxyClassPathXMLApplicationContext(String fileName) {
//读取xml配置文件
this.readXML(fileName);
//实例化bean
this.instanceBeans();
//注入对象
this.injectObject();
}
/**
* 注入对象方法
*/
private void injectObject() {
for (BeanDefinition beanDefinition : beanDefines) {
//获取beanDefines中的对象
Object bean = sigletons.get(beanDefinition.getId());
if (bean != null) {
//如果存在,利用反射技术将值注入
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass())
.getPropertyDescriptors();
for (PropertyDefinition propertyDefinition : beanDefinition.getProperties()) {
for (PropertyDescriptor properdesc : ps) {
//取得属性名字与propertyDefinition中的属性做比较
if (propertyDefinition.getName().equals(properdesc.getName())) {
//获得属性的setter方法
Method setter = properdesc.getWriteMethod();
if (setter != null) {
Object value = sigletons.get(propertyDefinition.getRef());
//把引用对象注入到属性
setter.setAccessible(true);//允许访私有方法
setter.invoke(bean, value);
}
}
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 读取XML配置文件,获取BeanDefinition内容,存入到beanDefinition列表中
* @param fileName xml配置文件名称
*/
private void readXML(String fileName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
//通过类加载器获取Resource资源路径,实现BeanDefinition的resource定位
URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
//将xml读入到document中
document = saxReader.read(xmlPath);
Map<String, String> nsMap = new HashMap<String, String>();
//加入命名空间
nsMap.put("ns", "http://www.springframework.org/schema/beans");
//创建beans/bean查询路径,注意:路径前要注明命名空间,便于解析
XPath xsub = document.createXPath("//ns:beans/ns:bean");
//设置命名空间
xsub.setNamespaceURIs(nsMap);
//获取文档下的所有Bean节点
List<Element> beans = xsub.selectNodes(document);
for (Element element : beans) {
//获取id属性值
String id = element.attributeValue("id");
//获取class属性值
String clazz = element.attributeValue("class");
BeanDefinition beanDefinition = new BeanDefinition(id, clazz);
//创建bean/property查询路径
XPath propertysub = element.createXPath("ns:property");
//设置命名空间
propertysub.setNamespaceURIs(nsMap);
//获取bean的property列表节点
List<Element> properties = propertysub.selectNodes(element);
for (Element property : properties) {
String propertyName = property.attributeValue("name");
String propertyref = property.attributeValue("ref");
PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName,
propertyref);
System.out.println(property);
//将property属性添加到beanDefinition中
beanDefinition.getProperties().add(propertyDefinition);
}
//将新创建的BeanDefinition赌侠ing放入到BeanDeifnitions中
beanDefines.add(beanDefinition);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
/**
* 实例化bean,存入到sigletons中
*/
private void instanceBeans() {
for (BeanDefinition beanDefinition : beanDefines) {
try {
if (beanDefinition.getClassName() != null
&& !(beanDefinition.getClassName().isEmpty())) {
//利用java反射机制,生成BeanDefinition实例,并将其注册到sigletons中
sigletons.put(beanDefinition.getId(), Class.forName(
beanDefinition.getClassName()).newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 根据ID名获取实例bean
* return 返回一个Object对象,用户使用时,需要对获取的结果进行转换类型
*/
public Object getBean(String beanName) {
return this.sigletons.get(beanName);
}
}
测试一下看是否取到了property的属性值:
测试结果: --------------------------------------- --> the method is called save()! ---------------------------------------- public class Test {
public static void main(String[] args) {
//IOC容器实例化
WxyClassPathXMLApplicationContext ac = new WxyClassPathXMLApplicationContext("beans.xml");
PeopleService peopleService = (PeopleService) ac.getBean("peopleService");
peopleService.save();
}
}
this is the method PeopleDaoBean.add()!
发表评论
-
apache日志信息详解
2011-11-06 21:19 6270一、访问日志的格式 Apache内建了记录服务器 ... -
使用CGLIB实现AOP功能与AOP概念解释 .
2011-08-19 10:30 1331第三方创建代理对象的框架cglib,目标类可以不实现接口 ... -
浏览器如何工作
2011-08-19 08:57 0http://taligarsiel.com/Projects ... -
编码实现用JDK中的Proxy实现springAOP功能
2011-08-18 15:04 744http://blog.csdn.net/iamtheevil ... -
Spring注解原理的详细剖析与实现
2011-08-14 23:09 84235本文主要分为三部分: ... -
Spring装配基本属性的原理分析与代码实现
2011-08-11 15:37 1419首先,做一个配置属性的基本测试。修改beans.xml,使引用 ... -
Spring的三种实例化Bean的方法
2011-08-10 14:03 1Spring的三种实例化Bean的方法 1、 使用 ... -
Spring管理bean的原理自定义实现
2011-08-10 10:44 61871、Spring通过BeanDefinition管理基于S ... -
spring环境搭建与测试
2011-08-10 08:40 3409Chapter1、搭建与测试spring的环境 1、 ... -
java回调机制实现
2011-08-08 09:06 2036Java的接口支持提供了一种获得回调的等价功能的 ... -
log4j的使用与详细分析
2011-08-05 13:32 2626一、什么是log4j? http://logging.a ... -
log4j使用详解
2011-08-04 23:05 2http://logging.apache.org/log4j ... -
spring入门及Demo
2011-07-29 16:01 0一、Spring是什么? Spring是一个开源框架,它 ... -
ibatis入门及环境搭建
2011-07-23 12:27 1413使用ibatis的理由: 1. 知道怎样操作10种以上 ... -
山寨Hibernate
2011-04-09 23:32 0---------------------------- ... -
java解析XML的四种方法的学习与比较
2011-03-30 20:55 7229四种XML解析方法: ... -
自定义日志模块实现
2011-03-30 09:58 1102package wxy.XXXX.Utils; impo ... -
synchronized(this)
2011-03-29 09:17 69371、当两个并发线程访问同一个对象object中的这个synch ... -
详细解析Java中抽象类和接口的区别(转)
2011-03-24 23:48 923在Java语言中, abstract cl ... -
首次配置hibernate碰到的各种问题
2011-03-15 21:18 14891、java.lang.NoClassDefFoundErro ...
相关推荐
NULL 博文链接:https://coolszy.iteye.com/blog/512193
编码剖析Spring装配基本属性的原理
03_黑马程序员_黎活明_Spring2.5视频教程_编码剖析Spring管理Bean的原理
这个是搭建与测试Spring的开发环境和编码剖析Spring 管理Bean的原理的代码,适合刚接触spring的人学习,如何搭建spring开发环境,以及了解spring是如何管理bean的。这个是由传智播客的老师讲的。
07_编码剖析Spring依赖注入的原理 08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类型的属性 10_使用构造器装配属性 11_用@Resource注解完成属性装配 12_编码剖析@Resource注解的实现原理 ...
01_全面阐释Spring及其各项功能 02_搭建与测试Spring的开发环境 03_编码剖析Spring管理Bean的原理 ...07_编码剖析Spring依赖注入的原理 08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类
深入剖析Spring Web源码 学习spring的好资料
深度剖析Spring Boot自动装配机制实现原理(csdn)————程序
深入剖析Spring Web源码(含一二版)带目录,给csdn友们下载
《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的出色架构和设计思想,还能从...
Spring 框架之 AOP 原理剖析.html 抓下来打包成了HTML文件, 方便离线观看
深入解析SPRING架构与设计原理(第2版) 带标签 高清版,深入剖析spring运行机制
spring揭秘 spring源码剖析 spring入门到精通 spring内幕等技术书籍集合 链接:https://pan.baidu.com/s/17aaL4J0cRPXzL-dbMK3s3g 密码:见文件
深入剖析Spring_Web源码.pdf深入剖析Spring_Web源码.pdf
国内第一本基于Spring3.0的著作,从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从木书中参透Spring框架的优秀架构和设计思想,而且还能从Spring优雅的实现源码...
编码剖析@Resource注解的实现原理
本书从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的优秀架构和设计思想,还能从Spring优雅的实现源码中一窥Java语言的精髓。本书在开篇...