Spring依赖注入

新建PersonIDao 和PersonDao底实现Save方法:

public interface PersonIDao {
public void save();
}
public class PersonDaoImpl  implements PersonIDao{
@Override
public void save() {
System.out.println("我是dao的Save方法");
}
}

在Bean.xml中注入PersonIDao,并将PersonIDao注入到PersonService中:

<bean id="personIDao" class="cn.dao.impl.PersonDaoImpl" />
<bean id="personIService" class="cn.server.impl.PersonServiceImpl">
<property name="personIDao" ref="personIDao" />
</bean>

在PersonService中添加PersonIDao类型属性并实现属性的set方法,然后调用PersonIDao的save方法

public class PersonServiceImpl implements PersonIService {
private PersonIDao personIDao; public PersonIDao getPersonIDao() {
return personIDao;
}
public void setPersonIDao(PersonIDao personIDao) {
this.personIDao = personIDao;
}
@Override
public void save() {
personIDao.save();
}
}

然后测试:

@Test
public void testSave() {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)ac.getBean("personIService");
personIService.save();
}

编码剖析Spring依赖注入的原理

通过编码的方法式来剖析Spring 依赖注入的原理方法,新建PropertyDefinition类用来承载Bean中注入的property的属性:

public class PropertyDefinition {
private String name;
private String ref;
public PropertyDefinition(){}
public PropertyDefinition(String name, String ref) {
super();
this.name = name;
this.ref = ref;
} 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用来承载解析到的Bean属性

public class BeanDefinition {
private String id;
private String className;
private List<PropertyDefinition> properties=new ArrayList<PropertyDefinition>();
public BeanDefinition(){}
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> getProperties() {
return properties;
}
public void setProperties(List<PropertyDefinition> properties) {
this.properties = properties;
}
}

新建OtherClassPathXMLApplicationContext 用来解析bean.xml

public class OtherClassPathXMLApplicationContext {
private List<BeanDefinition> list=new ArrayList<BeanDefinition>();
private Map<String,Object> beans=new HashMap<String, Object>(); public OtherClassPathXMLApplicationContext(String fileName){
this.readXML(fileName);
this.instanceBeans();
this.instanceProperties();
}
private void instanceBeans(){
for(BeanDefinition bean : list){
try {
// 创建Bean实例,并放到Map中
if(bean.getClassName()!=null && !bean.getClassName().trim().equals("")){
beans.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void instanceProperties(){
for(BeanDefinition beanDefinition : list){
Object bean=beans.get(beanDefinition.getId());
if(bean!=null){
try {
// 获取bean下所有的属性定义描述
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDefinition propertyDefinition : beanDefinition.getProperties()){
for(PropertyDescriptor propertyDescriptor : ps){
// 如果bean下的属性名字与当前的BeanDefinition下的属性名称一样的话,则将引用对象注入到属性
if(propertyDefinition.getName().equals(propertyDescriptor.getName())){
Method setter=propertyDescriptor.getWriteMethod(); // 获取Bean的所有写入的方法 即 setter方法
setter.setAccessible(true); // true: 直接访问私有属性,将例子中的私有属性改值。
Object value=beans.get(propertyDefinition.getRef());
setter.invoke(bean, value); // 把引用对象注入属性
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void readXML(String fileName){
SAXReader saxReader = new SAXReader();
Document doc=null;
try{
// JAVA里面对于类进行调用配置资源的文件数据,以this.getClass().getResourceAsStream()来读取比较合适。
// 路径采用相对路径直接可以从工程的path路径去找。
URL xmlpath=this.getClass().getClassLoader().getResource(fileName);
doc=saxReader.read(xmlpath);
Map<String,String> nsMap=new HashMap<String,String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans"); // 加入命名空间
XPath xsub=doc.createXPath("//ns:beans/ns:bean"); // 创建 beans/bean的查询路径
xsub.setNamespaceURIs(nsMap); // 设置命名空间
List beans=xsub.selectNodes(doc); // 获取文档下的所有bean节点
for(Object node : beans){
Element element=(Element)node;
String id=element.attributeValue("id");
String className=element.attributeValue("class");
BeanDefinition bean=new BeanDefinition(id,className); // 编码剖析Spring依赖注入的原理
// 加载bean下的Property
XPath xproperty=element.createXPath("ns:property"); // 为xproperty 添加查询路径
xproperty.setNamespaceURIs(nsMap);
List properties=xproperty.selectNodes(element); // 查询出bean下的所有property
for(Object propertyNode : properties){
Element propertyElement=(Element)propertyNode;
String name=propertyElement.attributeValue("name");
String ref=propertyElement.attributeValue("ref");
bean.getProperties().add(new PropertyDefinition(name, ref));
}
list.add(bean);
} }catch(Exception e){
e.printStackTrace();
}
}
// 通过Id名称,获取Bean
public Object getBean(String name){
return beans.get(name);
}
}

测试代码:

	@Test
public void testSave2() {
OtherClassPathXMLApplicationContext ac = new OtherClassPathXMLApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)ac.getBean("personIService");
personIService.save();
}

Spring、Spring依赖注入与编码剖析Spring依赖注入的原理的更多相关文章

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

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

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

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

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

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

  4. (转)编码剖析@Resource注解的实现原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52860046 上文我们已经学会使用@Resource注解注入属性.学是学会了,但也仅限于会使用 ...

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

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

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

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

  7. 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 ...

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

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

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

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

随机推荐

  1. TensorFlow深度学习笔记 循环神经网络实践

    转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 加 ...

  2. Nginx 配置指令的执行顺序(一)

    大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个 location 配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭.于是许多人选择了 ...

  3. jsp 2种include标签的区别

    众所周知,jsp中有2种标签用于包含其他jsp或者文件 1.include指令,其实是java代码 <%@ include file="xxx.jsp"%> 2.jsp ...

  4. SQL别名解析(转载)

    通过使用 SQL,可以为列名称和表名称指定别名(Alias). 其实,select列的时候取别名有三种方法,这三种方法并不是所有数据库都适用. 方法一.直接在字段名称后面加上别名,中间以空格隔开. 方 ...

  5. NDEF-NFC数据交换格式

    为实现NFC标签.NFC设备以及NFC设备之间的交互通信,NFC论坛(NFC FROUM)定义了称为NFC数据交换格式(NDEF)的通用数据格式.NDEF是轻量级的紧凑的二进制格式,可带有URL,vC ...

  6. linux下维护服务器之常用命令

    linux下维护服务器之常用命令! 第1套如下: 正则表达式: 1.如何不要文件中的空白行和注释语句: [root@localhost ~]# grep -v '^$' 文件名 |grep -v '^ ...

  7. VC实现图片拖拽及动画

    基础知识 1.PictureBox控件的使用 2.加载位图文件 1.通过文件路径获得位图句柄 //获得位图句柄  void CMovePictureDlg::GetHandleFromPath(CSt ...

  8. C#中ref和out的使用小结

    ref传递的参数是变量的地址,在传入函数后,函数可以使用这些地址处的值,同时函数执行完后,这些变量被带回了调用者.ref传递的参数既可作传入值,也可作返回值. out传递的参数是变量的地址,在传入函数 ...

  9. php float 转int

    round(x,prec) 参数 描述 x 可选.规定要舍入的数字. prec 可选.规定小数点后的位数. <?php echo(round(0.60)); echo(round(0.50)); ...

  10. MAC中在eclipse luna上搭建移动平台自己主动化測试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录

    这几天由于原来在用的hp laptop的电池坏掉了,机器一不小心就断电.所以仅仅能花时间在自己的mackbook pro上又一次搭建整套环境.大家都知道搭建好开发环境是个非常琐碎须要耐心的事情,特别是 ...