您的位置:首页 > 编程语言 > Java开发

【spring】1.spring ioc原理和demo

2015-06-22 21:34 399 查看
我们先做一个简单的spring例子。

面向接口编程,我们先来定接口

IHelloWorld

package com.services;

public interface IHelloWorld {

public void sayHello();

}


写一个实现类,并且声明一个String属性,提供set方法注入

package com.services.impl;

import com.services.IHelloWorld;

public class HelloWorld implements IHelloWorld {

private String helloWorld;

public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}

@Override
public void sayHello() {
System.out.println("hello world");
}

}


编写配置文件

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
<bean id="helloWorldBean" class="com.services.impl.HelloWorld">
<property name="helloWorld">
<value>helloworld !!!</value>
</property>
</bean>

</beans>




注意看。配置文件的含义:

beans里面是各种bean对象的声明。

bean对象里面描述了bean的信息,里面包括bean名称,对应的class文件,class文件里的属性和值。

编程测试文件:

package com;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;

import com.services.IHelloWorld;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
IHelloWorld hello = (IHelloWorld) factory.getBean("helloWorldBean");
hello.sayHello();
}

}


ioc原理简单分析:

我们看看测试代码:

//这里其实是读取xml文件

ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");

//创建bean工厂,把读取到的载入我们的xml的reader当中

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);

//这里其实是通过反射,获得bean对象

IHelloWorld hello = (IHelloWorld) factory.getBean("helloWorldBean");
hello.sayHello();

简述ioc原理:如果我们不使用spring,那么bean对象创建就是通过new等方式硬代码的方式写入程序中。

ioc其实是把bean的信息写入xml文件,通过jdom去解析到bean节点信息,然后里面包含bean的id或者name,还有bean对应的class全路径,那么我们通过getBean方法,就可以获取到bean的类全路径,通过反射就可以得到bean对象,程序内部其实是用hashMap来保存xml解析出来的信息的,hashMap的key其实就是我们写在xml里面的id,而值就是class对应的类全路径,然后通过对象工厂,得到bean工厂后getBean就可以反射得到bean对象。类的创建过程是spring来帮你完成,我们自己的代码中并未创建对象,也没有对属性赋值,整个过程spring容器完成,这也就是通常所说的依赖注入,或者说叫控制反转(创建对象的过程是spring容器完成的,控制权发生了变化)

ioc其实里面应用到了jcom(解析xml),反射(xml里面的类全路径后getBean得到对象),工厂设计模式。

部分源码:

BeanFactory为顶层接口:该接口有很多常见的子接口和实现类,大家可能还会看到很多其他人代码用的XmlBeanFactory等子类实现,该接口有很多方法,我们常用获得bean的方法getBean方法如下:





而大家代码第一步获取xml对象的方法,对应接口其实是Resource接口,里面也有很多常见的实现类。



太具体的源码 大家自己看看api就都懂了 粗浅的原理还是很容易懂得
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: