Spring IOC

来源:互联网 发布:cat linux 指定行 编辑:程序博客网 时间:2024/05/29 12:47

1、IOC容器
IOC,Invcerse of Control,控制翻转。
作用:IOC容器负责对象的实例化、定位、配置,以及建立对象间的依赖关系。应用程序无需在代码中实例化相关的对象,全部由IOC容器进行组装。在Spring中,BeanFactory是IOC容器的实际代表者。

2、Bean
Bean:IOC容器管理的组成应用程序的对象。
装配(wiring):创建应用对象之间协作关系的行为,即依赖注入(Depedency Injection,DI)的本质。

IOC装配Bean的三种方式:

  • 自动装配,@Component(创建Bean)、@Autowired(注入Bean)
  • Java代码装配,@Bean
  • XML装配

3、Spring Hello World
(1)准备jar包
核心jar包:从下载的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目录查找如下jar包
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar

依赖的jar包:从下载的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依赖jar包
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.collections-3.2.1.jar

(2)创建Java工程

public interface HelloService {      public void sayHello();    }  
public class HelloServiceImpl implements HelloService{      public void sayHello(){          System.out.println("Hello World!");      }  } 
<?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:context="http://www.springframework.org/schema/context"      xsi:schemaLocation="          http://www.springframework.org/schema/beans                  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-3.0.xsd">      <!-- id 表示组件的名字,class表示组件类 -->      <bean id="helloService" class="com.ljq.test.HelloServiceImpl" />  </beans>  
import org.junit.Test;  import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;  public class HelloServiceTest {      @Test      public void testHelloWorld() {          // 1、读取配置文件实例化一个IOC容器          ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");          // 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”          HelloService helloService = context.getBean("helloService", HelloService.class);          // 3、执行业务逻辑          helloService.sayHello();      }  }  

自此一个完整的Spring Hello World已完成。

代码详解:
1)org.springframework.beans包中的BeanFactory接口提供了IOC容器最基本功能;

2)org.springframework.context包下的ApplicationContext接口扩展了BeanFactory,还提供了与Spring AOP集成、国际化处理、事件传播及提供不同层次的context实现 (如针对web应用的WebApplicationContext)。

3)ApplicationContext接口获取Bean方法简介:

  • Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换;
  • T getBean(String name, Class<> requiredType) 根据名称和指定的类型返回一个Bean,客户端无需自己进行类型转换,如果类型转换失败,容器抛出异常;
  • T getBean(Class<> requiredType) 根据指定的类型返回一个Bean,客户端无需自己进行类型转换,如果没有或有多于一个Bean存在容器将抛出异常;
  • Map<> getBeansOfType(Class<> type) 根据指定的类型返回一个键值为名字和值为Bean对象的Map,如果没有Bean对象存在则返回空的Map。

4)IOC容器到底如何工作,以xml配置方式来分析一下:

  • 准备配置文件
    在配置文件中声明Bean定义,即为Bean配置元数据。
  • IOC容器解析元数据
    IOC容器的Bean Reader读取并解析配置文件,根据定义生成BeanDefinition配置元数据对象,IOC容器根据BeanDefinition进行实例化、配置及组装Bean。
  • 实例化IOC容器
    由客户端实例化容器,获取需要的Bean。