• Spring的IOC容器第一辑


    一.Spring的IOC容器概述

    Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后容器 在创建bean时注入这些依赖关系。Spring实现IOC容器的基础是org.springframework.be和org.springframework.context。  

    有关spring常用的设计模式和应用请点击查看 《spring中常用设计模式及应用》
     
    核心接口BeanFactory 接口提供了一种能够管理任何类型对象的高级配置机制。 ApplicationContext 是一个子接口BeanFactory。它增加了与Spring的AOP功能更容易的集成; BeanFactory提供了配置框架和基本功能,并ApplicationContext增加了更多的企业特定功能。
    在Spring中,构成应用程序的骨干并由Spring IoC 容器管理的对象称为bean。bean是一个实例化,组装并由Spring IoC容器管理的对象。
     
    Spring如何工作的视图:
     

    二.Spring的IOC的方式

    Spring容器xml配置管理
    通常是多个bean定义组成。基于XML的配置元数据将这些bean配置为<bean/>顶层元素内的<beans/>元素
    如下图提供spring的官方的配置文件模板(素材来源www.spring.io)
     

    <?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.xsd">
     
        <bean id="..." class="...">
            <!-- collaborators and configuration for this bean go here -->
        </bean>
     
        <bean id="..." class="...">
            <!-- collaborators and configuration for this bean go here -->
        </bean>
     
        <!-- more bean definitions go here -->
     
    </beans>

     

    1.默认构造器

    当你通过构造函数的方法创建一个bean时,所有的普通类都可以被Spring使用和兼容。也就是说,正在开发的类不需要实现任何特定的接口或以特定的方式编码。只需指定bean类就足够了。但是,根据您用于特定bean的IoC类型,需要一个默认(空)构造函数。
     
    使用基于XML的配置元数据,您可以指定您的bean类如下:
     

    <bean id="exampleBean" class="examples.ExampleBean"/>
     
    <bean name="anotherExample" class="examples.ExampleBeanTwo"/>

     
    其中 bean 中的class 是我们交于spring初始化的bean的全路径。

    2.静态工厂

    在定义使用静态工厂方法创建的bean时,可以使用该class 属性来指定包含static工厂方法的类和factory-method指定工厂方法本身的名称的属性。你应该可以调用这个方法(使用后面描述的可选参数)并返回一个实例化对象,这个实例化对象随后被视为是通过构造函数创建的。用于这种bean定义的就是被称为静态工厂方式创建。
    静态工厂的定义

    public class ClientService {
     
    private static ClientService clientService = new ClientService();
     
        private ClientService() {}
     
        public static ClientService createInstance() {
            return clientService;
        }
    }

     
    配置xml

    <bean id="clientService"
        class="examples.ClientService"
        factory-method="createInstance"/>

    这样的spring框架在调用IOC实例化的使用是通过反射创建 调用静态工厂的中的static方法去创建对象。此时在创建的对象的是在静态方法手中。

    3.实例化工厂

    实例化工厂的方式相同于静态工厂方法,只是在一个实例工厂中反射一个存在对象的非静态方法,从而去spring容器去创建实例化的bean。
    创建实例化工厂

    public class DefaultServiceLocator {
     
        private static ClientService clientService = new ClientServiceImpl();
     
        private static AccountService accountService = new AccountServiceImpl();
     
        public ClientService createClientServiceInstance() {
            return clientService;
        }
     
        public AccountService createAccountServiceInstance() {
            return accountService;
        }
    }

     
    配置xml:

    <bean id="serviceLocator" class="examples.DefaultServiceLocator">
    </bean>
     
    <bean id="clientService"
        factory-bean="serviceLocator"
        factory-method="createClientServiceInstance"/>
     
    <bean id="accountService"
        factory-bean="serviceLocator"
        factory-method="createAccountServiceInstance"/>
     

     

    .结

    上述的三种的方式就是我们的spring的提供的三种初始化bean的方式。学会了吗? 下次我们来一起学习DI的四种方式

  • 相关阅读:
    POJ1185:炮兵阵地(状压dp)
    POJ3254:Corn Fields(状压dp第一发)
    二进制x&(x-1);
    子集和问题(应用--换零钱)POJ2229:Sumsets
    JAVA math包
    UVA+POJ中大数实现的题目,持续更新(JAVA实现)
    HDU中大数实现的题目,持续更新(JAVA实现)
    SDUT中大数实现的题目,持续更新(JAVA实现)
    JAVA与ACM
    HDU3123:GCC(同余模简单题)
  • 原文地址:https://www.cnblogs.com/shsxt/p/7852538.html
Copyright © 2020-2023  润新知