核心技术-IoC容器

1、IoC容器简介

控制反转IoC(Inversion of Control)又称依赖注入DI(dependency injection),由org.springframework.beans和org.springframework.context基础包构成了IoC容器,其中BeanFactory接口提供了一种先进的配置机制能够管理任何类型的对象,ApplicationContext是BeanFactory的一个子类,它更加简便的集成了Spring的AOP特性,通常情况下使用ApplicationContext,因为它提供了BeanFactory的完整超集,如果想用BeanFactory替代ApplicationContext可以参考 Section 6.16, “The BeanFactory”
Spring IoC容器工作图描述

Paste_Image.png

2、源数据(Metadata)配置

可以基于以下三种方式配置:

  • xml注入

 
      
     
      
     

允许组合多个bean配置xml

 
     
     
     

     
    

使用容器加载bean

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List userList = service.getUsernameList();
  • Annotation-based configuration

 
  

  • Java-based configuration
@Configuration
public class AppConfig { 
    @Bean
    public MyService myService() { 
      return new MyServiceImpl(); 
    }
}

加载bean

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
MyService myService = ctx.getBean(MyService.class); 
myService.doStuff();
3、实例化bean
  • 通过构造函数实例化

  • 通过静态方法实例化
    java code
public class ClientService { 
    private static ClientService clientService = new ClientService(); 
    private ClientService() {} 
    public static ClientService createInstance() { 
      return clientService; 
    }
}

xml配置


静态方法带参数(通过constructor-arg传入参数)
java code

public class ExampleBean { 
    // a private constructor 
    private ExampleBean(...) { ... } 
    // a static factory method; the arguments to this method can be 
    // considered the dependencies of the bean that is returned, 
    // regardless of how those arguments are actually used. 
    public static ExampleBean createInstance ( 
      AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { 
        ExampleBean eb = new ExampleBean (...); 
        // some other operations... return eb; 
    }
}

xml 配置


     
     
    



  • 通过普通方法实例化
    java code
public class DefaultServiceLocator { 
    private static ClientService clientService = new ClientServiceImpl(); 
    private DefaultServiceLocator() {} 
    public ClientService createClientServiceInstance() { 
        return clientService; 
    }
}

xml配置


 
    



4、依赖注入
  • 基于构造器的注入
    普通类型的参数
    java code
package x.y;
public class Foo { 
    public Foo(Bar bar, Baz baz) { 
    // ... 
    }
}

xml配置

 
     
        
        
     
     
    

基本数据类型的参数
java code

package examples;
public class ExampleBean { 
    // Number of years to calculate the Ultimate Answer 
    private int years; 
    // The Answer to Life, the Universe, and Everything 
    private String ultimateAnswer; 
    public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; this.ultimateAnswer = ultimateAnswer; 
    }
}

xml配置-type

 
     
    

xml配置-index

 
     
    

xml配置-name

 
     
    

注意,基于那么的注入,如果你的编译不是以debug方式编译,那么参数名是会变掉,这时候需要通过jdk注解指定编译后的参数名

package examples;
public class ExampleBean { 
    // Fields omitted 
    @ConstructorProperties({"years", "ultimateAnswer"})
    public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; 
      this.ultimateAnswer = ultimateAnswer; 
    }
}
  • 基于setter的注入
    java code
public class SimpleMovieLister { 
    // the SimpleMovieLister has a dependency on the MovieFinder 
    private MovieFinder movieFinder; 
    // a setter method so that the Spring container can inject a MovieFinder 
    public void setMovieFinder(MovieFinder movieFinder) { 
      this.movieFinder = movieFinder; 
    } 
    // business logic that actually uses the injected MovieFinder is omitted...
}

xml配置

 
     
     
       
    


该方式依赖注入由容器调用setter方法将匹配参数类型的bean注入进去,所以该方式注入会在其它构造器注入之后之后注入

你可能感兴趣的:(核心技术-IoC容器)