一、IoC背景
Spring Ioc(DI) 容器提供了POJO全面的服务(依赖注入、线程池服务、事务服务、安全性服务、生命周期管理服务)。
IoC容器负责对象的创建、依赖关系设置等工作从而,开发者有更多的精力放在业务逻辑上。
二、
(1) BeanFactory 和 ApplicationContext
BeanFactory 提供最基础的IoC容器,提供配置框架和基础功能,适用于环境内存受限的场合,如Applet环境
ApplicationContext 继承自BeanFactory ,增加了大量的企业级特征。简化了同Spring AOP集成、支持消息资源的国际化(il8n)处理、提供事件支持、针对web应用的做了许多辅助工作。更适合企业级应用。
(2) 宿主DI容器配置元数据方式
1.基于XML的DI容器配置元数据
java类
- package cn.beanfactory.itf;
- public interface IHelloStr {
- String getContext();
- }
- package cn.beanfactory.itf;
- public interface IHelloWorld {
- String getContext();
- }
- package cn.beanfactory.bean;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import cn.beanfactory.itf.IHelloStr;
- @ForYou
- public class FileHelloStrImpl implements IHelloStr {
- private String propfilename;
- private String helloWorld;
- private Log log = LogFactory.getLog(FileHelloStrImpl.class);
- public FileHelloStrImpl(String propfilename){
- log.info(" FileHelloStrImpl(String propfilename)");
- this.propfilename=propfilename;
- }
- public FileHelloStrImpl(String propfilename,TestMain t){
- log.info(" FileHelloStrImpl(String propfilename,TestMain t)");
- this.propfilename=propfilename;
- }
- public FileHelloStrImpl(TestMain t,String propfilename){
- log.info(" FileHelloStrImpl(TestMain t,String propfilename)");
- this.propfilename=propfilename;
- }
- @Override
- public String getContext() {
- // TODO Auto-generated method stub
- try{
- Properties properties = new Properties();
- InputStream is = getClass().getClassLoader().getResourceAsStream(propfilename);
- properties.load(is);
- is.close();
- helloWorld = properties.getProperty("helloWorld");
- }catch (IOException e) {
- // TODO: handle exception
- log.error("",e);
- }
- return helloWorld;
- }
- }
- package cn.beanfactory.bean;
- import cn.beanfactory.itf.IHelloStr;
- import cn.beanfactory.itf.IHelloWorld;
- public class HelloWorld implements IHelloWorld {
- private IHelloStr helloStr;
- public void setHelloStr(IHelloStr helloStr){
- this.helloStr=helloStr;
- }
- @Override
- public String getContext() {
- // TODO Auto-generated method stub
- return helloStr.getContext();
- }
- public static HelloWorld getHelloWorld(){
- return new HelloWorld();
- }
- }
- package cn.beanfactory.main;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- import cn.beanfactory.itf.IHelloWorld;
- public class BeanFactoryMain {
- private static Log log =LogFactory.getLog(BeanFactoryMain.class);
- /**
- * BeanFactory
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Resource resource = new ClassPathResource("applicationContext.xml");
- BeanFactory beanFactory = new XmlBeanFactory(resource);
- IHelloWorld hw = (IHelloWorld) beanFactory.getBean("helloWorld");
- log.info(hw.getContext());
- }
- }
XML 配置文件
- <?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 name="helloWorld" class="cn.beanfactory.bean.HelloWorld">
- <property name="helloStr" ref="fileHello"></property>
- </bean>
- <bean name="fileHello" class="cn.beanfactory.bean.FileHelloStrImpl">
- <constructor-arg value="helloWorld.properties" />
- </bean>
- </beans>
2.基于注解的DI容器配置元数据