Spring - IoC(1) - 初试IoC容器

Spring - IoC(1) - 初试IoC容器

  • IoC( Inverse of Control, 控制反转 ),Spring容器最重要的基础内核。

    某一接口具体实现类的选择控制权从调用类中移除,转交给第三方决定。即由Spring容器借由Bean配置来进行控制。

  • DI( Dependency Injection, 依赖注入 )

    让调用类对某一接口实现类的依赖关系由第三方(Spring容器)注入,以移除调用类对某一接口实现类的依赖。

Autowired

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {}

从源代码可以看到,Autowired注解支持构造函数,方法(setter),属性等等

不过Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".,最好使用构造函数注入。

因为Java变量的初始化顺序为:静态变量或静态语句块–>实例变量或初始化语句块–>构造方法–>@Autowired,如果在DI之前使用属性,就会空指针异常。详见@Autowired的使用:推荐对构造函数进行注释。

反射

  1. 通过ClassLoader加载class
  2. 通过clazz获取constructor,并newInstance
  3. 通过clazz获取method,并invoke
  4. 通过clazz获取field,并set

容器

  • BeanFactory
// The root interface for accessing a Spring bean container.
// This interface is implemented by objects that hold a number of bean definitions, 
// each uniquely identified by a String name.
public interface BeanFactory {
    Object getBean(...)
}

BeanFactory是Spring的核心概念,用来创建并管理Spring bean。

  • ApplicationContext
// Central interface to provide configuration for an application.
public interface ApplicationContext extends EnvironmentCapable, 
                                            ListableBeanFactory, 
                                            HierarchicalBeanFactory,
                                            MessageSource, 
                                            ApplicationEventPublisher, 
                                            ResourcePatternResolver {}

ApplicationContext是更高级的核心概念,用来管理整个Spring应用。

提供以下功能:

  1. 访问bean,来自ListableBeanFactory
  2. 加载资源文件,来自ResourceLoader;
  3. 发布事件,来自ApplicationEventPublisher;
  4. resolve messages, supporting internationalization,来自MessageSource;
  • WebApplicationContext
// Interface to provide configuration for a web application. 
public interface WebApplicationContext extends ApplicationContext {
    /**
     * Scope identifier for request scope: "request".
     * Supported in addition to the standard scopes "singleton" and "prototype".
     */
    String SCOPE_REQUEST = "request";

    /**
     * Scope identifier for session scope: "session".
     * Supported in addition to the standard scopes "singleton" and "prototype".
     */
    String SCOPE_SESSION = "session";

    /**
     * Scope identifier for global session scope: "globalSession".
     * Supported in addition to the standard scopes "singleton" and "prototype".
     */
    String SCOPE_GLOBAL_SESSION = "globalSession";

    /**
     * Scope identifier for the global web application scope: "application".
     * Supported in addition to the standard scopes "singleton" and "prototype".
     */
    String SCOPE_APPLICATION = "application";
    
    ServletContext getServletContext();
}

WebApplicationContext应对Web应用,除了基本的singleton & prototype以外,还增加Web相关的域,并且增加了ServletContext

你可能感兴趣的:(Spring - IoC(1) - 初试IoC容器)