在Spring IOC 容器的代码是org.springframework.beans包中的BeanFactory接口,该接口提供了IOC容器的基本功能。
而org.springframework.context包下的ApplicationContext接口扩展了BeanFactory,还提供了与Spring AOP集成、国际化处理、事件传播及提供不同层次的context实现 (如针对web应用的WebApplicationContext)。简单的说BeanFactory提供了IOC容器的最基本功能,而Application提供了更多的企业应用的支持。
简单的对容器的实现做下总结。
import java.io.File;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
/**
* IOC容器的实现
* */
public class ContainerTest {
/**
* beanfactory实现,从文件系统中获取资源
*/
@Test
public void beanFactoryBaseonFileSystem(){
//从文件系统中获取配置文件,默认为相对路径,也可以取绝对路径
File file=new File("bin/resource/chapter01.xml");
Resource resource=new FileSystemResource(file);
//初始化容器
BeanFactory bean=new XmlBeanFactory(resource);
//从容器中获取bean
GreetServiceImp service=bean.getBean("greetservice", GreetServiceImp.class);
//执行操作
service.sayGreeting();
}
/**
* beanfactory实现,从classpath获取资源
*/
@Test
public void beanFactoryBaseOnClassPath(){
//准备配置文件,从当前类加载路径中获取配置文件
Resource resource=new ClassPathResource("resource/chapter01.xml");
//初始化容器
BeanFactory bean=new XmlBeanFactory(resource);
//从容器中获取bean
GreetServiceImp service=bean.getBean("greetservice", GreetServiceImp.class);
//执行操作
service.sayGreeting();
}
/**
* ApplicationContext实现,从文件系统中获取资源
*/
@Test
public void applicationContextBaseOnFileSystem(){
//准备配置文件,从文件系统中获取配置文件,默认为相当路径,也可以写绝对路径
BeanFactory bean=new FileSystemXmlApplicationContext("bin/resource/chapter01.xml");
//从容器中获取bean
GreetServiceImp service=bean.getBean("greetservice", GreetServiceImp.class);
//执行操作
service.sayGreeting();
}
/**
* ApplicationContext实现,从ClassPath中获取资源
*/
@Test
public void applicationContextBaseOnClassPath(){
//准备配置文件,从当前类加载路径中获取配置文件
BeanFactory bean=new ClassPathXmlApplicationContext("resource/chapter01.xml");
//从容器中获取bean
GreetServiceImp service=bean.getBean("greetservice",GreetServiceImp.class);
//执行操作
service.sayGreeting();
}
}
上述代码是IOC实现的常用几种方法。
分享到:
相关推荐
Spring IoC容器实现的结构分析
spring Ioc容器配置 IOC容器数据源配置 <!-- 配置数据源 --> destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost:3306/demo <value>root ...
NULL 博文链接:https://ziyoujiedao.iteye.com/blog/262360
详细描述了spring IOC的工作原理和实现细节
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
spring ioc容器部署实现
spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合
自己动手实现Spring IoC容器, 写Spring IOC容器.wps写Spring IOC容器.wps写Spring IOC容器.wps
Java-Spring-SpringIoC容器-SpringIoC的学习 SpringIoC容器的学习笔记 主要介绍了IoC容器工作原理以及如何配置IoC容器 通过标签和注解配置容器
迷你书是《Spring揭秘》的精选版,节选了原书中介绍Spring IoC容器的6个章节。《Spring揭秘》以幽默生动的语言、辅以有趣的故事和典故,循循善诱地阐述了Spring框架的方方面面。针对Spring框架的主要功能以及开发者...
自己实现的简易的模拟Spring的IoC容器,实现注解自动装配
熟练使用Spring IOC容器,如有意见或者建议
讲的非常不错的Spring书籍,特别有利于对IOC容器的理解,特别推荐哦
spring IOC容器依赖注入XML配置详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring的IoC容器初始化源码解析,包括资源定位、加载、注册3个过程
仿照Spring写的一个Ioc容器,注释很详细!
我们从一个简单的容器开始,一步步的重构,最后实现一个基本的Spring框架的雏形,为了帮助我们更加深入的理解Spring的IoC的原理...【SSH进阶之路】一步步重构容器实现Spring的IoC——工厂+反射+配置文件实现IoC容器(十)
基于xml注册,简单模拟spring ioc容器。快速理解ioc容器
本篇文章主要介绍了Spring实现一个简单的SpringIOC容器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
Spring实现原理、IoC容器的优点及在Eclipse 中创建Spring的 Web应用